|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Foundation\Generator\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
|
7
|
|
|
use Nwidart\Modules\Support\Stub; |
|
8
|
|
|
use Nwidart\Modules\Traits\ModuleCommandTrait; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
|
|
12
|
|
|
class ModelMakeCommand extends \Nwidart\Modules\Commands\ModelMakeCommand |
|
13
|
|
|
{ |
|
14
|
|
|
use ModuleCommandTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The name of argument name. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $argumentName = 'model'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The console command name. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $name = 'larapi:make-model'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The console command description. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $description = 'Create a new model for the specified module.'; |
|
36
|
|
|
|
|
37
|
|
|
public function handle() |
|
38
|
|
|
{ |
|
39
|
|
|
parent::handle(); |
|
40
|
|
|
|
|
41
|
|
|
$this->handleOptionalMigrationOption(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Create a proper migration name: |
|
46
|
|
|
* ProductDetail: product_details |
|
47
|
|
|
* Product: products |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
private function createMigrationName() |
|
51
|
|
|
{ |
|
52
|
|
|
$pieces = preg_split('/(?=[A-Z])/', $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$string = ''; |
|
55
|
|
|
foreach ($pieces as $i => $piece) { |
|
56
|
|
|
if ($i+1 < count($pieces)) { |
|
57
|
|
|
$string .= strtolower($piece) . '_'; |
|
58
|
|
|
} else { |
|
59
|
|
|
$string .= Str::plural(strtolower($piece)); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $string; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the console command arguments. |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getArguments() |
|
72
|
|
|
{ |
|
73
|
|
|
return [ |
|
74
|
|
|
['model', InputArgument::REQUIRED, 'The name of model will be created.'], |
|
75
|
|
|
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get the console command options. |
|
81
|
|
|
* |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function getOptions() |
|
85
|
|
|
{ |
|
86
|
|
|
return [ |
|
87
|
|
|
['fillable', null, InputOption::VALUE_OPTIONAL, 'The fillable attributes.', null], |
|
88
|
|
|
['migration', 'm', InputOption::VALUE_NONE, 'Flag to create associated migrations', null], |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Create the migration file with the given model if migration flag was used |
|
94
|
|
|
*/ |
|
95
|
|
|
private function handleOptionalMigrationOption() |
|
96
|
|
|
{ |
|
97
|
|
|
if ($this->option('migration') === true) { |
|
98
|
|
|
$migrationName = 'create_' . $this->createMigrationName() . '_table'; |
|
99
|
|
|
$this->call('module:make-migration', ['name' => $migrationName, 'module' => $this->argument('module')]); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getTemplateContents() |
|
107
|
|
|
{ |
|
108
|
|
|
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); |
|
109
|
|
|
|
|
110
|
|
|
return (new Stub('/model.stub', [ |
|
111
|
|
|
'NAME' => $this->getModelName(), |
|
112
|
|
|
'FILLABLE' => $this->getFillable(), |
|
113
|
|
|
'NAMESPACE' => $this->getClassNamespace($module), |
|
114
|
|
|
'CLASS' => $this->getClass(), |
|
115
|
|
|
'LOWER_NAME' => $module->getLowerName(), |
|
116
|
|
|
'MODULE' => $this->getModuleName(), |
|
117
|
|
|
'STUDLY_NAME' => $module->getStudlyName(), |
|
118
|
|
|
'MODULE_NAMESPACE' => $this->laravel['modules']->config('namespace'), |
|
119
|
|
|
]))->render(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getDestinationFilePath() |
|
126
|
|
|
{ |
|
127
|
|
|
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); |
|
128
|
|
|
|
|
129
|
|
|
$modelPath = GenerateConfigReader::read('model'); |
|
130
|
|
|
|
|
131
|
|
|
return $path . $modelPath->getPath() . '/' . $this->getModelName() . '.php'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return mixed|string |
|
136
|
|
|
*/ |
|
137
|
|
|
private function getModelName() |
|
138
|
|
|
{ |
|
139
|
|
|
return Str::studly($this->argument('model')); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
private function getFillable() |
|
146
|
|
|
{ |
|
147
|
|
|
$fillable = $this->option('fillable'); |
|
148
|
|
|
|
|
149
|
|
|
if (!is_null($fillable)) { |
|
150
|
|
|
$arrays = explode(',', $fillable); |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
return json_encode($arrays); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return '[]'; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get default namespace. |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getDefaultNamespace() : string |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->laravel['modules']->config('paths.generator.model.path', 'Entities'); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|