-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
ModuleMakeCommand.php
193 lines (163 loc) · 4.86 KB
/
ModuleMakeCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace ArtemSchander\L5Modular\Console;
use ArtemSchander\L5Modular\Traits\ConfiguresFolder;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
class ModuleMakeCommand extends GeneratorCommand
{
use ConfiguresFolder;
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:module';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new module (folder structure)';
/**
* The fully qualified module path
*
* @var string
*/
protected $path;
/**
* The current stub.
*
* @var string
*/
protected $stub;
/**
* Execute the console command >= L5.5.
*
* @return void
*/
public function handle()
{
$this->module = Str::studly($this->getNameInput());
$this->path = $this->laravel->path(str_replace('//', '/', "Modules/{$this->module}/"));
// check if module exists
if ($this->files->exists($this->path)) {
return $this->error('Module already exists!');
}
$this->generateModule();
// $this->info('Module created successfully.');
$this->line('<info>Module</info>' . " \"{$this->module}\" " . '<info>created successfully.</info>');
}
protected function generateModule()
{
$this->files->makeDirectory($this->path, 0755, true);
$components = config('modules.generate', []);
foreach ($components as $component => $active) {
if ($active) {
$method = "generate" . ucfirst($component);
if (method_exists($this, $method)) {
$this->$method();
} else {
$this->generateComponent($component);
}
}
}
}
/**
* Generate a component by executing
* the corresponding artisan make command
*
* @param string $component
*
* @return void
*/
protected function generateComponent(string $component)
{
$name = $this->module;
$options = ['name' => $name, '--module' => $this->module, '--quiet' => true];
$this->getComponentGenerationOptions($component, $options);
$this->call("make:module:{$component}", $options);
if ($component === 'migration') {
$this->info("Migration created successfully.");
}
}
/**
* Individual options for the make commands
* Reduce the cognitive complexity of generateComponent method
*
* @param string $component
*
* @return array
*/
protected function getComponentGenerationOptions(string $component, array &$options)
{
switch ($component) {
case 'controller':
$options['name'] = "{$this->module}Controller";
$options['--welcome'] = true;
break;
case 'view':
$options['name'] = 'welcome';
break;
case 'translation':
$options['name'] = 'example';
$options['--language'] = 'en';
break;
case 'seeder':
$options['name'] = "{$this->module}Seeder";
break;
case 'factory':
$options['name'] = "{$this->module}Factory";
$options['--model'] = $this->module;
break;
case 'migration':
$table = Str::plural(Str::snake($this->module));
$options['name'] = "create_{$table}_table";
$options['--create'] = $table;
break;
case 'config':
case 'helpers':
unset($options['name']);
break;
}
return $options;
}
protected function generateRoutes()
{
$types = config("modules.specific.{$this->module}.routing", config('modules.default.routing'));
$options = ['--module' => $this->module, '--quiet' => true];
$skip = true;
$allowed = [ 'web', 'api', 'simple' ];
foreach ($types as $type) {
if (in_array($type, $allowed)) {
$options["--{$type}"] = true;
$skip = false;
}
}
if (! $skip) {
$this->call("make:module:route", $options);
$this->info("Routes created successfully.");
}
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'Module name.'],
];
}
/**
* Get the stub file for the generator.
*
* @codeCoverageIgnore
* @return string
*/
protected function getStub()
{
return $this->stub;
}
}