1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Caffeinated\Modules\Console\Generators; |
4
|
|
|
|
5
|
|
|
use Caffeinated\Modules\Modules; |
6
|
|
|
use Illuminate\Console\Command as CommandGenerator; |
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
|
9
|
|
|
class MakeCommand extends CommandGenerator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Module folders to be created. |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $listFolders = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Module files to be created. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $listFiles = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Module signature option. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $signOption = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Module stubs used to populate defined files. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $listStubs = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The modules instance. |
41
|
|
|
* |
42
|
|
|
* @var Modules |
43
|
|
|
*/ |
44
|
|
|
protected $module; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The modules path. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $modulePath; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The modules info. |
55
|
|
|
* |
56
|
|
|
* @var Illuminate\Support\Collection; |
57
|
|
|
*/ |
58
|
|
|
protected $moduleInfo; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The filesystem instance. |
62
|
|
|
* |
63
|
|
|
* @var Filesystem |
64
|
|
|
*/ |
65
|
|
|
protected $files; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Array to store the configuration details. |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
protected $container; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* String to store the command type. |
76
|
|
|
* |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
protected $type; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create a new command instance. |
83
|
|
|
* |
84
|
|
|
* @param Filesystem $files |
85
|
|
|
* @param Modules $module |
86
|
|
|
*/ |
87
|
|
|
public function __construct(Filesystem $files, Modules $module) |
88
|
|
|
{ |
89
|
|
|
parent::__construct(); |
90
|
|
|
|
91
|
|
|
$this->files = $files; |
92
|
|
|
$this->module = $module; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Execute the console command. |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function fire() |
101
|
|
|
{ |
102
|
|
|
$slug = $this->parseSlug($this->argument('slug')); |
103
|
|
|
$name = $this->parseName($this->argument('name')); |
104
|
|
|
|
105
|
|
|
if ($this->module->exists($slug)) { |
106
|
|
|
$this->modulePath = $this->module->getPath(); |
107
|
|
|
$this->moduleInfo = collect($this->module->where('slug', $slug)->first()); |
108
|
|
|
|
109
|
|
|
$this->container['slug'] = $slug; |
110
|
|
|
$this->container['name'] = $name; |
111
|
|
|
|
112
|
|
|
return $this->generate(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->error('Module '.$this->container['slug'].' does not exist.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* generate the console command. |
120
|
|
|
* |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
|
|
protected function generate() |
124
|
|
|
{ |
125
|
|
|
foreach ($this->listFiles as $key => $file) { |
126
|
|
|
$filePath = $this->makeFilePath($this->listFolders[$key], $this->container['name']); |
127
|
|
|
|
128
|
|
|
$this->resolveByPath( $filePath ); |
129
|
|
|
|
130
|
|
|
$file = $this->formatContent($file); |
131
|
|
|
$filePath = str_replace(basename($filePath), '', $filePath); |
132
|
|
|
$filePath = $filePath.$file; |
133
|
|
|
|
134
|
|
|
if ($this->files->exists($filePath)) { |
135
|
|
|
return $this->error($this->type.' already exists!'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->makeDirectory($filePath); |
139
|
|
|
|
140
|
|
|
foreach ($this->signOption as $option) { |
141
|
|
|
if ($this->option($option)) { |
142
|
|
|
$stubFile = $this->listStubs[$option][$key]; |
143
|
|
|
|
144
|
|
|
$this->resolveByOption( $this->option($option) ); |
145
|
|
|
|
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if (! isset($stubFile)) { |
151
|
|
|
$stubFile = $this->listStubs['default'][$key]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->files->put( |
155
|
|
|
$filePath, |
156
|
|
|
$this->getStubContent($stubFile) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->info($this->type.' created successfully.'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Resolve Container after getting file path |
165
|
|
|
* |
166
|
|
|
* @param string $FilePath |
|
|
|
|
167
|
|
|
* @return Array |
168
|
|
|
*/ |
169
|
|
|
protected function resolveByPath($filePath) |
170
|
|
|
{ |
171
|
|
|
// |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Resolve Container after getting input option |
176
|
|
|
* |
177
|
|
|
* @param string $option |
178
|
|
|
* @return Array |
179
|
|
|
*/ |
180
|
|
|
protected function resolveByOption($option) |
181
|
|
|
{ |
182
|
|
|
// |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Parse slug name of the module. |
187
|
|
|
* |
188
|
|
|
* @param string $slug |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
protected function parseSlug($slug) |
192
|
|
|
{ |
193
|
|
|
$slug = studly_case($slug); |
194
|
|
|
|
195
|
|
|
if (str_contains($slug, '/')) { |
196
|
|
|
$slug = str_replace('/', '', $slug); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if (str_contains($slug, '\\')) { |
200
|
|
|
$slug = str_replace('\\', '', $slug); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return strtolower($slug); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Parse class name of the module. |
208
|
|
|
* |
209
|
|
|
* @param string $slug |
|
|
|
|
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
protected function parseName($name) |
213
|
|
|
{ |
214
|
|
|
if (str_contains($name, '\\')) { |
215
|
|
|
$name = str_replace('\\', '/', $name); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if (str_contains($name, '/')) { |
219
|
|
|
$formats = collect( explode('/', $name) )->map(function($name){ |
220
|
|
|
return studly_case($name); |
221
|
|
|
}); |
222
|
|
|
|
223
|
|
|
$name = $formats->implode('/'); |
224
|
|
|
} else { |
225
|
|
|
$name = studly_case($name); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $name; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Make FilePath |
233
|
|
|
* |
234
|
|
|
* @param string $folder |
235
|
|
|
* @param string $name |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
|
protected function makeFilePath($folder, $name) |
239
|
|
|
{ |
240
|
|
|
$folder = ltrim($folder, '\/'); |
241
|
|
|
$folder = rtrim($folder, '\/'); |
242
|
|
|
|
243
|
|
|
$name = ltrim($name, '\/'); |
244
|
|
|
$name = rtrim($name, '\/'); |
245
|
|
|
|
246
|
|
|
return |
247
|
|
|
$this->modulePath.DIRECTORY_SEPARATOR. |
248
|
|
|
$this->moduleInfo->get('namespace').DIRECTORY_SEPARATOR. |
249
|
|
|
$folder.DIRECTORY_SEPARATOR.$name; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Make FileName |
254
|
|
|
* |
255
|
|
|
* @param string $filePath |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
|
|
protected function makeFileName($filePath) |
259
|
|
|
{ |
260
|
|
|
return basename($filePath); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Build the directory for the class if necessary. |
265
|
|
|
* |
266
|
|
|
* @param string $path |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
|
|
protected function makeDirectory($path) |
270
|
|
|
{ |
271
|
|
|
if (! $this->files->isDirectory(dirname($path))) { |
272
|
|
|
$this->files->makeDirectory(dirname($path), 0777, true, true); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Get Namespace of the current file |
278
|
|
|
* |
279
|
|
|
* @param string $file |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
|
|
protected function getNamespace($file) |
283
|
|
|
{ |
284
|
|
|
$namespace = str_replace($this->modulePath, '', $file); |
285
|
|
|
$namespace = str_replace(basename($namespace), '', $namespace); |
286
|
|
|
$namespace = ltrim($namespace, '\/'); |
287
|
|
|
$namespace = rtrim($namespace, '\/'); |
288
|
|
|
|
289
|
|
|
return str_replace('/', '\\', $namespace); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Get the configured module base namespace. |
294
|
|
|
* |
295
|
|
|
* @return string |
296
|
|
|
*/ |
297
|
|
|
protected function getBaseNamespace() |
298
|
|
|
{ |
299
|
|
|
return $this->module->getNamespace(); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Get stub content by key. |
304
|
|
|
* |
305
|
|
|
* @param int $key |
|
|
|
|
306
|
|
|
* @return string |
307
|
|
|
*/ |
308
|
|
|
protected function getStubContent($stubName) |
309
|
|
|
{ |
310
|
|
|
$stubPath = __DIR__.'/../../../resources/stubs/'; |
311
|
|
|
|
312
|
|
|
return $this->formatContent($this->files->get($stubPath.$stubName)); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Replace placeholder text with correct values. |
317
|
|
|
* |
318
|
|
|
* @return string |
319
|
|
|
*/ |
320
|
|
|
protected function formatContent($content) |
321
|
|
|
{ |
322
|
|
|
// |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.