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
|
2 |
|
protected function stubOptions(): array |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
2 |
|
'NAMESPACE' => $this->getClassNamespace(), |
54
|
2 |
|
'CLASS' => $this->getClassName(), |
55
|
2 |
|
'MONGO' => $this->isMongoModel(), |
56
|
2 |
|
'MIGRATION' => $this->needsMigration() |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the console command options. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
112 |
|
protected function setOptions(): array |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
112 |
|
['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
|
|
|
{ |
75
|
|
|
return $this->confirm('Is this model for a mongodb database?', $default); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
protected function isMongoModel(): bool |
79
|
|
|
{ |
80
|
2 |
|
return $this->getOption('mongo'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function handleMigrationOption($shortcut, $type, $question, $default) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
return $this->confirm('Do you want to create a migration for this model?', $default); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
protected function needsMigration(): bool |
89
|
|
|
{ |
90
|
2 |
|
return $this->getOption('migration'); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
public function afterGeneration(): void |
94
|
|
|
{ |
95
|
2 |
|
if ($this->needsMigration()) { |
96
|
2 |
|
if ($this->isMongoModel()) { |
97
|
1 |
|
GeneratorManager::module($this->getModuleName(), $this->isOverwriteable()) |
98
|
1 |
|
->createMigration( |
99
|
1 |
|
"Create" . ucfirst($this->getClassName()) . "Collection", |
100
|
1 |
|
strtolower(split_caps_to_underscore(Str::plural($this->getClassName()))), |
101
|
1 |
|
true); |
102
|
|
|
} else { |
103
|
1 |
|
GeneratorManager::module($this->getModuleName(), $this->isOverwriteable()) |
104
|
1 |
|
->createMigration( |
105
|
1 |
|
"Create" . ucfirst($this->getClassName() . "Table"), |
106
|
1 |
|
strtolower(split_caps_to_underscore(Str::plural($this->getClassName()))), |
107
|
1 |
|
false); |
108
|
|
|
} |
109
|
|
|
} |
110
|
2 |
|
GeneratorManager::module($this->getModuleName(), $this->isOverwriteable()) |
111
|
2 |
|
->createAttribute($this->getClassName() . "Attributes"); |
112
|
2 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
2 |
|
protected function stubName(): string |
118
|
|
|
{ |
119
|
2 |
|
if ($this->isMongoModel()) { |
120
|
1 |
|
return 'model-mongo.stub'; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return 'model.stub'; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.