|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Artisan; |
|
6
|
|
|
use Nwidart\Modules\Laravel\LaravelDatabaseRepository; |
|
7
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class DatabaseModuleGenerator. |
|
11
|
|
|
* @package Nwidart\Modules\Generators |
|
12
|
|
|
* @property LaravelDatabaseRepository $module |
|
13
|
|
|
*/ |
|
14
|
|
|
class DatabaseModuleGenerator extends ModuleGenerator |
|
15
|
|
|
{ |
|
16
|
|
|
protected $silentOutput = false; |
|
17
|
|
|
|
|
18
|
|
|
public function setSilentOutput($bool = true) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->silentOutput = $bool; |
|
21
|
|
|
|
|
22
|
|
|
return $this; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Generate the module. |
|
27
|
|
|
* @todo Need to generate module.json ? |
|
28
|
|
|
*/ |
|
29
|
|
|
public function generate(): int |
|
30
|
|
|
{ |
|
31
|
|
|
$name = $this->getName(); |
|
32
|
|
|
|
|
33
|
|
View Code Duplication |
if ($this->module->has($name)) { |
|
|
|
|
|
|
34
|
|
|
if ($this->force) { |
|
35
|
|
|
$this->module->delete($name); |
|
36
|
|
|
} else { |
|
37
|
|
|
if (!$this->silentOutput) { |
|
38
|
|
|
$this->console->info("Module [{$name}] already exist!"); |
|
39
|
|
|
} else { |
|
40
|
|
|
abort(400, "Module [{$name}] already exist!"); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Get data from module.json. |
|
48
|
|
|
$data = $this->getStubContents('json'); |
|
49
|
|
|
$data = json_decode($data, true); |
|
50
|
|
|
$data['path'] = $this->module->getModulePath($this->getName()); |
|
51
|
|
|
if ($this->type === 'plain') { |
|
52
|
|
|
$data['provider'] = []; |
|
53
|
|
|
} |
|
54
|
|
|
$this->module->getModel()->create($data); |
|
55
|
|
|
|
|
56
|
|
|
$success = false; |
|
57
|
|
|
// Re-check if we created successfully. |
|
58
|
|
|
if ($this->module->has($name)) { |
|
59
|
|
|
$this->generateFolders(); |
|
60
|
|
|
|
|
61
|
|
|
$this->generateModuleJsonFile(); |
|
62
|
|
|
|
|
63
|
|
|
if ($this->type !== 'plain') { |
|
64
|
|
|
$this->generateFiles(); |
|
65
|
|
|
$this->generateResources(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$success = true; |
|
69
|
|
|
if (!$this->silentOutput) { |
|
70
|
|
|
$this->console->info("Module [{$name}] created successfully."); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $success; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Generate the files. |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function generateFiles() |
|
81
|
|
|
{ |
|
82
|
|
|
foreach ($this->getFiles() as $stub => $file) { |
|
83
|
|
|
$path = $this->module->getModulePath($this->getName()) . $file; |
|
84
|
|
|
|
|
85
|
|
|
if (!$this->filesystem->isDirectory($dir = dirname($path))) { |
|
86
|
|
|
$this->filesystem->makeDirectory($dir, 0775, true); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->filesystem->put($path, $this->getStubContents($stub)); |
|
90
|
|
|
|
|
91
|
|
|
if (!$this->silentOutput) { |
|
92
|
|
|
$this->console->info("Created : {$path}"); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Generate some resources. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function generateResources() |
|
101
|
|
|
{ |
|
102
|
|
|
$moduleName = $this->getName(); |
|
103
|
|
|
|
|
104
|
|
|
if (GenerateConfigReader::read('seeder')->generate() === true) { |
|
105
|
|
|
Artisan::call('module:make-seed', [ |
|
106
|
|
|
'name' => $moduleName, |
|
107
|
|
|
'module' => $moduleName, |
|
108
|
|
|
'--master' => true, |
|
109
|
|
|
]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (GenerateConfigReader::read('provider')->generate() === true) { |
|
113
|
|
|
Artisan::call('module:make-provider', [ |
|
114
|
|
|
'name' => $moduleName . 'ServiceProvider', |
|
115
|
|
|
'module' => $moduleName, |
|
116
|
|
|
'--master' => true, |
|
117
|
|
|
]); |
|
118
|
|
|
Artisan::call('module:route-provider', [ |
|
119
|
|
|
'module' => $moduleName, |
|
120
|
|
|
]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (GenerateConfigReader::read('controller')->generate() === true) { |
|
124
|
|
|
$options = $this->type == 'api' ? ['--api' => true] : []; |
|
125
|
|
|
Artisan::call('module:make-controller', [ |
|
126
|
|
|
'controller' => $moduleName . 'Controller', |
|
127
|
|
|
'module' => $moduleName, |
|
128
|
|
|
] + $options); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Generate the module.json file |
|
134
|
|
|
*/ |
|
135
|
|
View Code Duplication |
private function generateModuleJsonFile() |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
|
|
$path = $this->module->getModulePath($this->getName()) . 'module.json'; |
|
138
|
|
|
|
|
139
|
|
|
if (!$this->filesystem->isDirectory($dir = dirname($path))) { |
|
140
|
|
|
$this->filesystem->makeDirectory($dir, 0775, true); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$this->filesystem->put($path, $this->getStubContents('json')); |
|
144
|
|
|
|
|
145
|
|
|
if (!$this->silentOutput) { |
|
146
|
|
|
$this->console->info("Created : {$path}"); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.