1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Nwidart\Modules\Contracts\ActivatorInterface; |
7
|
|
|
use Nwidart\Modules\Generators\ModuleGenerator; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
|
11
|
|
|
class ModuleMakeCommand extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The console command name. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name = 'module:make'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Create a new module.'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Execute the console command. |
29
|
|
|
*/ |
30
|
130 |
|
public function handle() : int |
31
|
|
|
{ |
32
|
130 |
|
$names = $this->argument('name'); |
33
|
130 |
|
$success = true; |
34
|
|
|
|
35
|
130 |
|
foreach ($names as $name) { |
|
|
|
|
36
|
130 |
|
$code = with(new ModuleGenerator($name)) |
37
|
130 |
|
->setFilesystem($this->laravel['files']) |
38
|
130 |
|
->setModule($this->laravel['modules']) |
39
|
130 |
|
->setConfig($this->laravel['config']) |
40
|
130 |
|
->setActivator($this->laravel[ActivatorInterface::class]) |
41
|
130 |
|
->setConsole($this) |
42
|
130 |
|
->setForce($this->option('force')) |
43
|
130 |
|
->setType($this->getModuleType()) |
44
|
130 |
|
->setActive(!$this->option('disabled')) |
45
|
130 |
|
->generate(); |
46
|
|
|
|
47
|
130 |
|
if ($code === E_ERROR) { |
48
|
1 |
|
$success = false; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
130 |
|
return $success ? 0 : E_ERROR; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the console command arguments. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
130 |
|
protected function getArguments() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
130 |
|
['name', InputArgument::IS_ARRAY, 'The names of modules will be created.'], |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
130 |
|
protected function getOptions() |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
130 |
|
['plain', 'p', InputOption::VALUE_NONE, 'Generate a plain module (without some resources).'], |
71
|
|
|
['api', null, InputOption::VALUE_NONE, 'Generate an api module.'], |
72
|
|
|
['web', null, InputOption::VALUE_NONE, 'Generate a web module.'], |
73
|
|
|
['disabled', 'd', InputOption::VALUE_NONE, 'Do not enable the module at creation.'], |
74
|
|
|
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when the module already exists.'], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get module type . |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
private function getModuleType(){ |
84
|
|
|
|
85
|
|
|
$isPlain = $this->option('plain'); |
86
|
|
|
$isApi = $this->option('api'); |
87
|
|
|
|
88
|
|
|
if( $isPlain && $isApi ){ |
89
|
|
|
return 'web'; |
90
|
|
|
} |
91
|
|
|
if( $isPlain ){ |
92
|
|
|
return 'plain'; |
93
|
|
|
} |
94
|
|
|
elseif( $isApi ){ |
95
|
|
|
return 'api'; |
96
|
|
|
} |
97
|
|
|
else{ |
98
|
|
|
return 'web'; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.