Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 399 | Zur aktuellen 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\Concerns\CreatesMatchingTest;
6
use Illuminate\Console\GeneratorCommand;
7
use Illuminate\Support\Str;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Input\InputOption;
10
 
11
#[AsCommand(name: 'make:model')]
12
class ModelMakeCommand extends GeneratorCommand
13
{
14
    use CreatesMatchingTest;
15
 
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'make:model';
22
 
23
    /**
24
     * The name of the console command.
25
     *
26
     * This name is used to identify the command during lazy loading.
27
     *
28
     * @var string|null
29
     *
30
     * @deprecated
31
     */
32
    protected static $defaultName = 'make:model';
33
 
34
    /**
35
     * The console command description.
36
     *
37
     * @var string
38
     */
39
    protected $description = 'Create a new Eloquent model class';
40
 
41
    /**
42
     * The type of class being generated.
43
     *
44
     * @var string
45
     */
46
    protected $type = 'Model';
47
 
48
    /**
49
     * Execute the console command.
50
     *
51
     * @return void
52
     */
53
    public function handle()
54
    {
55
        if (parent::handle() === false && ! $this->option('force')) {
56
            return false;
57
        }
58
 
59
        if ($this->option('all')) {
60
            $this->input->setOption('factory', true);
61
            $this->input->setOption('seed', true);
62
            $this->input->setOption('migration', true);
63
            $this->input->setOption('controller', true);
64
            $this->input->setOption('policy', true);
65
            $this->input->setOption('resource', true);
66
        }
67
 
68
        if ($this->option('factory')) {
69
            $this->createFactory();
70
        }
71
 
72
        if ($this->option('migration')) {
73
            $this->createMigration();
74
        }
75
 
76
        if ($this->option('seed')) {
77
            $this->createSeeder();
78
        }
79
 
80
        if ($this->option('controller') || $this->option('resource') || $this->option('api')) {
81
            $this->createController();
82
        }
83
 
84
        if ($this->option('policy')) {
85
            $this->createPolicy();
86
        }
87
    }
88
 
89
    /**
90
     * Create a model factory for the model.
91
     *
92
     * @return void
93
     */
94
    protected function createFactory()
95
    {
96
        $factory = Str::studly($this->argument('name'));
97
 
98
        $this->call('make:factory', [
99
            'name' => "{$factory}Factory",
100
            '--model' => $this->qualifyClass($this->getNameInput()),
101
        ]);
102
    }
103
 
104
    /**
105
     * Create a migration file for the model.
106
     *
107
     * @return void
108
     */
109
    protected function createMigration()
110
    {
111
        $table = Str::snake(Str::pluralStudly(class_basename($this->argument('name'))));
112
 
113
        if ($this->option('pivot')) {
114
            $table = Str::singular($table);
115
        }
116
 
117
        $this->call('make:migration', [
118
            'name' => "create_{$table}_table",
119
            '--create' => $table,
120
            '--fullpath' => true,
121
        ]);
122
    }
123
 
124
    /**
125
     * Create a seeder file for the model.
126
     *
127
     * @return void
128
     */
129
    protected function createSeeder()
130
    {
131
        $seeder = Str::studly(class_basename($this->argument('name')));
132
 
133
        $this->call('make:seeder', [
134
            'name' => "{$seeder}Seeder",
135
        ]);
136
    }
137
 
138
    /**
139
     * Create a controller for the model.
140
     *
141
     * @return void
142
     */
143
    protected function createController()
144
    {
145
        $controller = Str::studly(class_basename($this->argument('name')));
146
 
147
        $modelName = $this->qualifyClass($this->getNameInput());
148
 
149
        $this->call('make:controller', array_filter([
150
            'name' => "{$controller}Controller",
151
            '--model' => $this->option('resource') || $this->option('api') ? $modelName : null,
152
            '--api' => $this->option('api'),
153
            '--requests' => $this->option('requests') || $this->option('all'),
154
        ]));
155
    }
156
 
157
    /**
158
     * Create a policy file for the model.
159
     *
160
     * @return void
161
     */
162
    protected function createPolicy()
163
    {
164
        $policy = Str::studly(class_basename($this->argument('name')));
165
 
166
        $this->call('make:policy', [
167
            'name' => "{$policy}Policy",
168
            '--model' => $this->qualifyClass($this->getNameInput()),
169
        ]);
170
    }
171
 
172
    /**
173
     * Get the stub file for the generator.
174
     *
175
     * @return string
176
     */
177
    protected function getStub()
178
    {
179
        if ($this->option('pivot')) {
180
            return $this->resolveStubPath('/stubs/model.pivot.stub');
181
        }
182
 
183
        if ($this->option('morph-pivot')) {
184
            return $this->resolveStubPath('/stubs/model.morph-pivot.stub');
185
        }
186
 
187
        return $this->resolveStubPath('/stubs/model.stub');
188
    }
189
 
190
    /**
191
     * Resolve the fully-qualified path to the stub.
192
     *
193
     * @param  string  $stub
194
     * @return string
195
     */
196
    protected function resolveStubPath($stub)
197
    {
198
        return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
199
                        ? $customPath
200
                        : __DIR__.$stub;
201
    }
202
 
203
    /**
204
     * Get the default namespace for the class.
205
     *
206
     * @param  string  $rootNamespace
207
     * @return string
208
     */
209
    protected function getDefaultNamespace($rootNamespace)
210
    {
211
        return is_dir(app_path('Models')) ? $rootNamespace.'\\Models' : $rootNamespace;
212
    }
213
 
214
    /**
215
     * Get the console command options.
216
     *
217
     * @return array
218
     */
219
    protected function getOptions()
220
    {
221
        return [
222
            ['all', 'a', InputOption::VALUE_NONE, 'Generate a migration, seeder, factory, policy, resource controller, and form request classes for the model'],
223
            ['controller', 'c', InputOption::VALUE_NONE, 'Create a new controller for the model'],
224
            ['factory', 'f', InputOption::VALUE_NONE, 'Create a new factory for the model'],
225
            ['force', null, InputOption::VALUE_NONE, 'Create the class even if the model already exists'],
226
            ['migration', 'm', InputOption::VALUE_NONE, 'Create a new migration file for the model'],
227
            ['morph-pivot', null, InputOption::VALUE_NONE, 'Indicates if the generated model should be a custom polymorphic intermediate table model'],
228
            ['policy', null, InputOption::VALUE_NONE, 'Create a new policy for the model'],
229
            ['seed', 's', InputOption::VALUE_NONE, 'Create a new seeder for the model'],
230
            ['pivot', 'p', InputOption::VALUE_NONE, 'Indicates if the generated model should be a custom intermediate table model'],
231
            ['resource', 'r', InputOption::VALUE_NONE, 'Indicates if the generated controller should be a resource controller'],
232
            ['api', null, InputOption::VALUE_NONE, 'Indicates if the generated controller should be an API resource controller'],
233
            ['requests', 'R', InputOption::VALUE_NONE, 'Create new form request classes and use them in the resource controller'],
234
        ];
235
    }
236
}