Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Console;
4
 
5
use Illuminate\Console\GeneratorCommand;
6
use Symfony\Component\Console\Attribute\AsCommand;
7
use Symfony\Component\Console\Input\InputOption;
8
 
9
#[AsCommand(name: 'make:resource')]
10
class ResourceMakeCommand extends GeneratorCommand
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'make:resource';
18
 
19
    /**
20
     * The name of the console command.
21
     *
22
     * This name is used to identify the command during lazy loading.
23
     *
24
     * @var string|null
25
     *
26
     * @deprecated
27
     */
28
    protected static $defaultName = 'make:resource';
29
 
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Create a new resource';
36
 
37
    /**
38
     * The type of class being generated.
39
     *
40
     * @var string
41
     */
42
    protected $type = 'Resource';
43
 
44
    /**
45
     * Execute the console command.
46
     *
47
     * @return void
48
     */
49
    public function handle()
50
    {
51
        if ($this->collection()) {
52
            $this->type = 'Resource collection';
53
        }
54
 
55
        parent::handle();
56
    }
57
 
58
    /**
59
     * Get the stub file for the generator.
60
     *
61
     * @return string
62
     */
63
    protected function getStub()
64
    {
65
        return $this->collection()
66
                    ? $this->resolveStubPath('/stubs/resource-collection.stub')
67
                    : $this->resolveStubPath('/stubs/resource.stub');
68
    }
69
 
70
    /**
71
     * Determine if the command is generating a resource collection.
72
     *
73
     * @return bool
74
     */
75
    protected function collection()
76
    {
77
        return $this->option('collection') ||
78
               str_ends_with($this->argument('name'), 'Collection');
79
    }
80
 
81
    /**
82
     * Resolve the fully-qualified path to the stub.
83
     *
84
     * @param  string  $stub
85
     * @return string
86
     */
87
    protected function resolveStubPath($stub)
88
    {
89
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
90
                        ? $customPath
91
                        : __DIR__.$stub;
92
    }
93
 
94
    /**
95
     * Get the default namespace for the class.
96
     *
97
     * @param  string  $rootNamespace
98
     * @return string
99
     */
100
    protected function getDefaultNamespace($rootNamespace)
101
    {
102
        return $rootNamespace.'\Http\Resources';
103
    }
104
 
105
    /**
106
     * Get the console command options.
107
     *
108
     * @return array
109
     */
110
    protected function getOptions()
111
    {
112
        return [
113
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the resource already exists'],
114
            ['collection', 'c', InputOption::VALUE_NONE, 'Create a resource collection'],
115
        ];
116
    }
117
}