|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Foundation\Generator\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Foundation\Generator\Abstracts\AbstractGeneratorCommand; |
|
6
|
|
|
use Foundation\Generator\Abstracts\ClassGeneratorCommand; |
|
7
|
|
|
use Foundation\Generator\Events\ModelGeneratedEvent; |
|
8
|
|
|
use Foundation\Generator\Managers\GeneratorManager; |
|
9
|
|
|
use Illuminate\Support\Str; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
|
|
12
|
|
|
class ModelMakeCommand extends ClassGeneratorCommand |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The console command name. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $name = 'larapi:make:model'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The console command description. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $description = 'Create a new model for the specified module.'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The name of the generated resource. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $generatorName = 'model'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The file path. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $filePath = '/Entities'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The event that will fire when the file is created. |
|
45
|
|
|
* |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $event = ModelGeneratedEvent::class; |
|
49
|
|
|
|
|
50
|
|
|
protected function stubOptions(): array |
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
|
|
'NAMESPACE' => $this->getClassNamespace(), |
|
54
|
|
|
'CLASS' => $this->getClassName(), |
|
55
|
|
|
'MONGO' => $this->isMongoModel(), |
|
56
|
|
|
'MIGRATION' => $this->needsMigration() |
|
57
|
|
|
]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the console command options. |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function setOptions(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
|
|
['mongo', null, InputOption::VALUE_OPTIONAL, 'Mongo model.', false], |
|
69
|
|
|
['migration', null, InputOption::VALUE_OPTIONAL, 'Create migration for the model.', true], |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function handleMongoOption($shortcut, $type, $question, $default){ |
|
|
|
|
|
|
74
|
|
|
return $this->confirm('Is this model for a mongodb database?', $default); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function isMongoModel(): bool |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->getOption('mongo'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function handleMigrationOption($shortcut, $type, $question, $default){ |
|
|
|
|
|
|
83
|
|
|
return $this->confirm('Do you want to create a migration for this model?', $default); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function needsMigration(): bool |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->getOption('migration'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function afterGeneration(): void |
|
92
|
|
|
{ |
|
93
|
|
|
if ($this->needsMigration()) { |
|
94
|
|
|
if ($this->isMongoModel()) { |
|
95
|
|
|
GeneratorManager::module($this->getModuleName(), $this->isOverwriteable())->createMigration( |
|
96
|
|
|
"Create" . ucfirst($this->getClassName()) . "Collection", |
|
97
|
|
|
strtolower(split_caps_to_underscore(Str::plural($this->getClassName()))), |
|
98
|
|
|
true); |
|
99
|
|
|
} else { |
|
100
|
|
|
GeneratorManager::module($this->getModuleName(), $this->isOverwriteable())->createMigration( |
|
101
|
|
|
"Create" . ucfirst($this->getClassName() . "Table"), |
|
102
|
|
|
strtolower(split_caps_to_underscore(Str::plural($this->getClassName()))), |
|
103
|
|
|
false); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function stubName(): string |
|
112
|
|
|
{ |
|
113
|
|
|
if ($this->isMongoModel()) { |
|
114
|
|
|
return 'model-mongo.stub'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return 'model.stub'; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.