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