1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository as Config; |
6
|
|
|
use Illuminate\Console\Command as Console; |
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Pingpong\Support\Stub; |
10
|
|
|
use Nwidart\Modules\Repository; |
11
|
|
|
|
12
|
|
|
class ModuleGenerator extends Generator |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The module name will created. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The laravel config instance. |
23
|
|
|
* |
24
|
|
|
* @var Config |
25
|
|
|
*/ |
26
|
|
|
protected $config; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The laravel filesystem instance. |
30
|
|
|
* |
31
|
|
|
* @var Filesystem |
32
|
|
|
*/ |
33
|
|
|
protected $filesystem; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The laravel console instance. |
37
|
|
|
* |
38
|
|
|
* @var Console |
39
|
|
|
*/ |
40
|
|
|
protected $console; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The pingpong module instance. |
44
|
|
|
* |
45
|
|
|
* @var Module |
46
|
|
|
*/ |
47
|
|
|
protected $module; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Force status. |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $force = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Generate a plain module. |
58
|
|
|
* |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
protected $plain = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The constructor. |
65
|
|
|
* |
66
|
|
|
* @param $name |
67
|
|
|
* @param Repository $module |
68
|
|
|
* @param Config $config |
69
|
|
|
* @param Filesystem $filesystem |
70
|
|
|
* @param Console $console |
71
|
|
|
*/ |
72
|
16 |
|
public function __construct( |
73
|
|
|
$name, |
74
|
|
|
Repository $module = null, |
75
|
|
|
Config $config = null, |
76
|
|
|
Filesystem $filesystem = null, |
77
|
|
|
Console $console = null |
78
|
|
|
) { |
79
|
16 |
|
$this->name = $name; |
80
|
16 |
|
$this->config = $config; |
81
|
16 |
|
$this->filesystem = $filesystem; |
82
|
16 |
|
$this->console = $console; |
83
|
16 |
|
$this->module = $module; |
|
|
|
|
84
|
16 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set plain flag. |
88
|
|
|
* |
89
|
|
|
* @param bool $plain |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
16 |
|
public function setPlain($plain) |
94
|
|
|
{ |
95
|
16 |
|
$this->plain = $plain; |
96
|
|
|
|
97
|
16 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the name of module will created. By default in studly case. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
16 |
|
public function getName() |
106
|
|
|
{ |
107
|
16 |
|
return Str::studly($this->name); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the laravel config instance. |
112
|
|
|
* |
113
|
|
|
* @return Config |
114
|
|
|
*/ |
115
|
|
|
public function getConfig() |
116
|
|
|
{ |
117
|
|
|
return $this->config; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the laravel config instance. |
122
|
|
|
* |
123
|
|
|
* @param Config $config |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
16 |
|
public function setConfig($config) |
128
|
|
|
{ |
129
|
16 |
|
$this->config = $config; |
130
|
|
|
|
131
|
16 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the laravel filesystem instance. |
136
|
|
|
* |
137
|
|
|
* @return Filesystem |
138
|
|
|
*/ |
139
|
|
|
public function getFilesystem() |
140
|
|
|
{ |
141
|
|
|
return $this->filesystem; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the laravel filesystem instance. |
146
|
|
|
* |
147
|
|
|
* @param Filesystem $filesystem |
148
|
|
|
* |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
16 |
|
public function setFilesystem($filesystem) |
152
|
|
|
{ |
153
|
16 |
|
$this->filesystem = $filesystem; |
154
|
|
|
|
155
|
16 |
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the laravel console instance. |
160
|
|
|
* |
161
|
|
|
* @return Console |
162
|
|
|
*/ |
163
|
|
|
public function getConsole() |
164
|
|
|
{ |
165
|
|
|
return $this->console; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set the laravel console instance. |
170
|
|
|
* |
171
|
|
|
* @param Console $console |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
16 |
|
public function setConsole($console) |
176
|
|
|
{ |
177
|
16 |
|
$this->console = $console; |
178
|
|
|
|
179
|
16 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the pingpong module instance. |
184
|
|
|
* |
185
|
|
|
* @return Module |
186
|
|
|
*/ |
187
|
|
|
public function getModule() |
188
|
|
|
{ |
189
|
|
|
return $this->module; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set the pingpong module instance. |
194
|
|
|
* |
195
|
|
|
* @param mixed $module |
196
|
|
|
* |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
16 |
|
public function setModule($module) |
200
|
|
|
{ |
201
|
16 |
|
$this->module = $module; |
202
|
|
|
|
203
|
16 |
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get the list of folders will created. |
208
|
|
|
* |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
16 |
|
public function getFolders() |
212
|
|
|
{ |
213
|
16 |
|
return array_values($this->module->config('paths.generator')); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get the list of files will created. |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
16 |
|
public function getFiles() |
222
|
|
|
{ |
223
|
16 |
|
return $this->module->config('stubs.files'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Set force status. |
228
|
|
|
* |
229
|
|
|
* @param bool|int $force |
230
|
|
|
* |
231
|
|
|
* @return $this |
232
|
|
|
*/ |
233
|
16 |
|
public function setForce($force) |
234
|
|
|
{ |
235
|
16 |
|
$this->force = $force; |
|
|
|
|
236
|
|
|
|
237
|
16 |
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Generate the module. |
242
|
|
|
*/ |
243
|
16 |
|
public function generate() |
244
|
|
|
{ |
245
|
16 |
|
$name = $this->getName(); |
246
|
|
|
|
247
|
16 |
|
if ($this->module->has($name)) { |
248
|
|
|
if ($this->force) { |
249
|
|
|
$this->module->delete($name); |
250
|
|
|
} else { |
251
|
|
|
$this->console->error("Module [{$name}] already exist!"); |
252
|
|
|
|
253
|
|
|
return; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
16 |
|
$this->generateFolders(); |
258
|
|
|
|
259
|
16 |
|
$this->generateFiles(); |
260
|
|
|
|
261
|
16 |
|
if (!$this->plain) { |
262
|
16 |
|
$this->generateResources(); |
263
|
16 |
|
} |
264
|
|
|
|
265
|
16 |
|
$this->console->info("Module [{$name}] created successfully."); |
266
|
16 |
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Generate the folders. |
270
|
|
|
*/ |
271
|
16 |
|
public function generateFolders() |
272
|
|
|
{ |
273
|
16 |
|
foreach ($this->getFolders() as $folder) { |
274
|
16 |
|
$path = $this->module->getModulePath($this->getName()).'/'.$folder; |
275
|
|
|
|
276
|
16 |
|
$this->filesystem->makeDirectory($path, 0755, true); |
277
|
|
|
|
278
|
16 |
|
$this->generateGitKeep($path); |
279
|
16 |
|
} |
280
|
16 |
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Generate git keep to the specified path. |
284
|
|
|
* |
285
|
|
|
* @param string $path |
286
|
|
|
*/ |
287
|
16 |
|
public function generateGitKeep($path) |
288
|
|
|
{ |
289
|
16 |
|
$this->filesystem->put($path.'/.gitkeep', ''); |
290
|
16 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Generate the files. |
294
|
|
|
*/ |
295
|
16 |
|
public function generateFiles() |
296
|
|
|
{ |
297
|
16 |
|
foreach ($this->getFiles() as $stub => $file) { |
298
|
16 |
|
$path = $this->module->getModulePath($this->getName()).$file; |
299
|
|
|
|
300
|
16 |
|
if (!$this->filesystem->isDirectory($dir = dirname($path))) { |
301
|
16 |
|
$this->filesystem->makeDirectory($dir, 0775, true); |
302
|
16 |
|
} |
303
|
|
|
|
304
|
16 |
|
$this->filesystem->put($path, $this->getStubContents($stub)); |
305
|
|
|
|
306
|
16 |
|
$this->console->info("Created : {$path}"); |
307
|
16 |
|
} |
308
|
16 |
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Generate some resources. |
312
|
|
|
*/ |
313
|
16 |
|
public function generateResources() |
314
|
|
|
{ |
315
|
16 |
|
$this->console->call('module:make-seed', [ |
316
|
16 |
|
'name' => $this->getName(), |
317
|
16 |
|
'module' => $this->getName(), |
318
|
16 |
|
'--master' => true, |
319
|
16 |
|
]); |
320
|
|
|
|
321
|
16 |
|
$this->console->call('module:make-provider', [ |
322
|
16 |
|
'name' => $this->getName().'ServiceProvider', |
323
|
16 |
|
'module' => $this->getName(), |
324
|
16 |
|
'--master' => true, |
325
|
16 |
|
]); |
326
|
|
|
|
327
|
16 |
|
$this->console->call('module:make-controller', [ |
328
|
16 |
|
'controller' => $this->getName().'Controller', |
329
|
16 |
|
'module' => $this->getName(), |
330
|
16 |
|
]); |
331
|
16 |
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Get the contents of the specified stub file by given stub name. |
335
|
|
|
* |
336
|
|
|
* @param $stub |
337
|
|
|
* |
338
|
|
|
* @return Stub |
339
|
|
|
*/ |
340
|
16 |
|
protected function getStubContents($stub) |
341
|
|
|
{ |
342
|
16 |
|
return (new Stub( |
343
|
16 |
|
'/'.$stub.'.stub', |
344
|
16 |
|
$this->getReplacement($stub)) |
345
|
16 |
|
)->render(); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* get the list for the replacements. |
350
|
|
|
*/ |
351
|
|
|
public function getReplacements() |
352
|
|
|
{ |
353
|
|
|
return $this->module->config('stubs.replacements'); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Get array replacement for the specified stub. |
358
|
|
|
* |
359
|
|
|
* @param $stub |
360
|
|
|
* |
361
|
|
|
* @return array |
362
|
|
|
*/ |
363
|
16 |
|
protected function getReplacement($stub) |
364
|
|
|
{ |
365
|
16 |
|
$replacements = $this->module->config('stubs.replacements'); |
366
|
|
|
|
367
|
16 |
|
if (!isset($replacements[$stub])) { |
368
|
|
|
return []; |
369
|
|
|
} |
370
|
|
|
|
371
|
16 |
|
$keys = $replacements[$stub]; |
372
|
|
|
|
373
|
16 |
|
$replaces = []; |
374
|
|
|
|
375
|
16 |
|
foreach ($keys as $key) { |
376
|
16 |
|
if (method_exists($this, $method = 'get'.ucfirst(studly_case(strtolower($key))).'Replacement')) { |
377
|
16 |
|
$replaces[$key] = call_user_func([$this, $method]); |
378
|
16 |
|
} else { |
379
|
|
|
$replaces[$key] = null; |
380
|
|
|
} |
381
|
16 |
|
} |
382
|
|
|
|
383
|
16 |
|
return $replaces; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Get the module name in lower case. |
388
|
|
|
* |
389
|
|
|
* @return string |
390
|
|
|
*/ |
391
|
16 |
|
protected function getLowerNameReplacement() |
392
|
|
|
{ |
393
|
16 |
|
return strtolower($this->getName()); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Get the module name in studly case. |
398
|
|
|
* |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
16 |
|
protected function getStudlyNameReplacement() |
402
|
16 |
|
{ |
403
|
16 |
|
return $this->getName(); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Get replacement for $VENDOR$. |
408
|
|
|
* |
409
|
|
|
* @return string |
410
|
|
|
*/ |
411
|
16 |
|
protected function getVendorReplacement() |
412
|
|
|
{ |
413
|
16 |
|
return $this->module->config('composer.vendor'); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Get replacement for $MODULE_NAMESPACE$. |
418
|
|
|
* |
419
|
|
|
* @return string |
420
|
|
|
*/ |
421
|
16 |
|
protected function getModuleNamespaceReplacement() |
422
|
|
|
{ |
423
|
16 |
|
return str_replace('\\', '\\\\', $this->module->config('namespace')); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Get replacement for $AUTHOR_NAME$. |
428
|
|
|
* |
429
|
|
|
* @return string |
430
|
|
|
*/ |
431
|
16 |
|
protected function getAuthorNameReplacement() |
432
|
|
|
{ |
433
|
16 |
|
return $this->module->config('composer.author.name'); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Get replacement for $AUTHOR_EMAIL$. |
438
|
|
|
* |
439
|
|
|
* @return string |
440
|
|
|
*/ |
441
|
16 |
|
protected function getAuthorEmailReplacement() |
442
|
|
|
{ |
443
|
16 |
|
return $this->module->config('composer.author.email'); |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.