|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Nwidart\Modules\Support\Stub; |
|
7
|
|
|
use Nwidart\Modules\Commands\GeneratorCommand; |
|
8
|
|
|
use Nwidart\Modules\Console\Traits\Definitions; |
|
9
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
|
10
|
|
|
|
|
11
|
|
|
abstract class Command extends GeneratorCommand |
|
12
|
|
|
{ |
|
13
|
|
|
use Definitions; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The name to be appended to the generated resources. |
|
17
|
|
|
* |
|
18
|
|
|
* @var null|string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $appendable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Stub file of the resource |
|
24
|
|
|
* |
|
25
|
|
|
* @var null|string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $stubFile; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Specifies default path. |
|
32
|
|
|
* |
|
33
|
|
|
* @var null|string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $defaultPath; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Destination file extension |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $outputExtension = 'php'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Generator paths key |
|
46
|
|
|
* |
|
47
|
|
|
* @see config/modules.php |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $generatorPathsKey = 'generator.paths'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Generator config key |
|
54
|
|
|
* |
|
55
|
|
|
* @see config/modules.php |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $generatorConfigKey = ''; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Generator config prefix for the $generatorConfigKey |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $generatorConfigPrefix; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var string $configKeySeparator |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $configKeySeparator = '-'; |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Getter for appendable |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function appendable() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->appendable; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Getter for the stub file |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
public function stubFile() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->stubFile; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Replacements for the stub file |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
public function replaces() |
|
99
|
|
|
{ |
|
100
|
|
|
return [ |
|
101
|
|
|
//... |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get stub file contents |
|
107
|
|
|
* |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function getTemplateContents() |
|
111
|
|
|
{ |
|
112
|
|
|
return (new Stub($this->stubFile(), $this->replaces()))->render(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get destination file path. |
|
117
|
|
|
* |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getDestinationFilePath() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->getModulePath() . "/" . |
|
|
|
|
|
|
123
|
|
|
GenerateConfigReader::read($this->getGeneratorConfigKey())->getPath() . "/" . |
|
124
|
|
|
$this->resolveFilename() . '.' . $this->outputExtension ?: 'php'; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Resolves the filename so that it always starts with capital letter |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
private function resolveFilename() |
|
133
|
|
|
{ |
|
134
|
|
|
$filename = str_replace('/', ' ', $this->getFileName()); |
|
135
|
|
|
$filename = ucwords(str_replace('\\', ' ', $filename)); |
|
136
|
|
|
return trim(str_replace(' ', '/', $filename)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Method to apply necessary functionality |
|
141
|
|
|
* before console command gets executed |
|
142
|
|
|
* |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
|
public function before() |
|
146
|
|
|
{ |
|
147
|
|
|
// ... |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Method to apply necessary functionality |
|
152
|
|
|
* after console command has executed. |
|
153
|
|
|
* |
|
154
|
|
|
* @return void |
|
155
|
|
|
*/ |
|
156
|
|
|
public function after() |
|
157
|
|
|
{ |
|
158
|
|
|
// ... |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Execute the console command |
|
163
|
|
|
* |
|
164
|
|
|
* @return int |
|
165
|
|
|
*/ |
|
166
|
|
|
public function handle(): int |
|
167
|
|
|
{ |
|
168
|
|
|
$this->before(); |
|
169
|
|
|
|
|
170
|
|
|
if (parent::handle() != E_ERROR) { |
|
171
|
|
|
$this->after(); |
|
172
|
|
|
} |
|
173
|
|
|
return 0; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get and resolve the filename. |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function getFileName(): string |
|
182
|
|
|
{ |
|
183
|
|
|
|
|
184
|
|
|
$name = Str::studly($this->argument($this->argumentName)); |
|
|
|
|
|
|
185
|
|
|
if ($this->appendable() && !Str::contains(strtolower($name), strtolower($this->appendable()))) { |
|
|
|
|
|
|
186
|
|
|
$name .= Str::studly($this->appendable()); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return Str::singular(Str::studly($name)); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Get default namespace. |
|
194
|
|
|
* |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getDefaultNamespace(): string |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->getModules()->config( |
|
200
|
|
|
$this->generatorPathsKey . '.' . $this->getGeneratorConfigKey() . '.namespace' |
|
201
|
|
|
) ?: $this->getModules()->config( |
|
202
|
|
|
$this->generatorPathsKey . '.' . $this->getGeneratorConfigKey() . '.path', |
|
203
|
|
|
$this->defaultPath ?: '' |
|
204
|
|
|
); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Get configurator config key |
|
209
|
|
|
* |
|
210
|
|
|
* @return string |
|
211
|
|
|
*/ |
|
212
|
|
|
private function getGeneratorConfigKey() |
|
213
|
|
|
{ |
|
214
|
|
|
return ($this->generatorConfigPrefix ? $this->generatorConfigPrefix . $this->configKeySeparator : '') . |
|
215
|
|
|
$this->generatorConfigKey; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: