|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nwidart\Modules\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Artisan; |
|
6
|
|
|
use Nwidart\Modules\Support\Config\GenerateConfigReader; |
|
7
|
|
|
|
|
8
|
|
|
class DatabaseModuleGenerator extends ModuleGenerator |
|
9
|
|
|
{ |
|
10
|
|
|
protected $silentOutput = false; |
|
11
|
|
|
|
|
12
|
|
|
public function setSilentOutput($bool = true) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->silentOutput = $bool; |
|
15
|
|
|
|
|
16
|
|
|
return $this; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Generate the module. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function generate(): int |
|
23
|
|
|
{ |
|
24
|
|
|
$name = $this->getName(); |
|
25
|
|
|
|
|
26
|
|
View Code Duplication |
if ($this->module->has($name)) { |
|
|
|
|
|
|
27
|
|
|
if ($this->force) { |
|
28
|
|
|
$this->module->delete($name); |
|
|
|
|
|
|
29
|
|
|
} else { |
|
30
|
|
|
if (!$this->silentOutput) { |
|
31
|
|
|
$this->console->info("Module [{$name}] already exist!"); |
|
32
|
|
|
return false; |
|
33
|
|
|
} else { |
|
34
|
|
|
abort(400, "Module [{$name}] already exist!"); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// Get data from module.json. |
|
40
|
|
|
$data = $this->getStubContents('json'); |
|
41
|
|
|
$data = json_decode($data, true); |
|
42
|
|
|
$data['path'] = $this->module->getModulePath($this->getName()); |
|
|
|
|
|
|
43
|
|
|
if ($this->type === 'plain') { |
|
44
|
|
|
$data['provider'] = []; |
|
45
|
|
|
} |
|
46
|
|
|
$this->module->getModel()->create($data); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$success = false; |
|
49
|
|
|
// Re-check if we created successfully. |
|
50
|
|
|
if ($this->module->has($name)) { |
|
|
|
|
|
|
51
|
|
|
$this->generateFolders(); |
|
52
|
|
|
|
|
53
|
|
|
if ($this->type !== 'plain') { |
|
54
|
|
|
$this->generateFiles(); |
|
55
|
|
|
$this->generateResources(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$success = true; |
|
59
|
|
|
if (!$this->silentOutput) { |
|
60
|
|
|
$this->console->info("Module [{$name}] created successfully."); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $success; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Generate the files. |
|
69
|
|
|
*/ |
|
70
|
|
View Code Duplication |
public function generateFiles() |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($this->getFiles() as $stub => $file) { |
|
73
|
|
|
$path = $this->module->getModulePath($this->getName()) . $file; |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
if (!$this->filesystem->isDirectory($dir = dirname($path))) { |
|
76
|
|
|
$this->filesystem->makeDirectory($dir, 0775, true); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->filesystem->put($path, $this->getStubContents($stub)); |
|
80
|
|
|
|
|
81
|
|
|
if (!$this->silentOutput) { |
|
82
|
|
|
$this->console->info("Created : {$path}"); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Generate some resources. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function generateResources() |
|
91
|
|
|
{ |
|
92
|
|
View Code Duplication |
if (GenerateConfigReader::read('seeder')->generate() === true) { |
|
|
|
|
|
|
93
|
|
|
Artisan::call('module:make-seed', [ |
|
94
|
|
|
'name' => $this->getName(), |
|
95
|
|
|
'module' => $this->getName(), |
|
96
|
|
|
'--master' => true, |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (GenerateConfigReader::read('provider')->generate() === true) { |
|
101
|
|
|
Artisan::call('module:make-provider', [ |
|
102
|
|
|
'name' => $this->getName() . 'ServiceProvider', |
|
103
|
|
|
'module' => $this->getName(), |
|
104
|
|
|
'--master' => true, |
|
105
|
|
|
]); |
|
106
|
|
|
Artisan::call('module:route-provider', [ |
|
107
|
|
|
'module' => $this->getName(), |
|
108
|
|
|
]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
View Code Duplication |
if (GenerateConfigReader::read('controller')->generate() === true) { |
|
|
|
|
|
|
112
|
|
|
$options = $this->type == 'api' ? ['--api' => true] : []; |
|
113
|
|
|
Artisan::call('module:make-controller', [ |
|
114
|
|
|
'controller' => $this->getName() . 'Controller', |
|
115
|
|
|
'module' => $this->getName(), |
|
116
|
|
|
] + $options); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: