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