1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: tony |
5
|
|
|
* Date: 12.03.19 |
6
|
|
|
* Time: 16:01 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Foundation\Generator\Generators; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Foundation\Core\Larapi; |
13
|
|
|
use Foundation\Core\Module; |
14
|
|
|
use Foundation\Generator\Managers\GeneratorManager; |
15
|
|
|
use Illuminate\Filesystem\Filesystem; |
16
|
|
|
use Illuminate\Support\Str; |
17
|
|
|
use Nwidart\Modules\Exceptions\FileAlreadyExistException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class User. |
21
|
|
|
* |
22
|
|
|
* @method void addMigration(string $name, string $table, bool $mongo) |
23
|
|
|
* @method void addController(string $name) |
24
|
|
|
* @method void addPolicy(string $name) |
25
|
|
|
* @method void addEvent(string $name) |
26
|
|
|
* @method void addNotification(string $name) |
27
|
|
|
* @method void addServiceProvider(string $name) |
28
|
|
|
* @method void addSeeder(string $name) |
29
|
|
|
* @method void addMiddleware(string $name) |
30
|
|
|
* @method void addRequest(string $name) |
31
|
|
|
* @method void addRule(string $name) |
32
|
|
|
* @method void addRoute() |
33
|
|
|
* @method void addComposer() |
34
|
|
|
* @method void addTest(string $name, string $type) |
35
|
|
|
* @method void addFactory(string $name) |
36
|
|
|
* @method void addTransformer(string $name, string $model) |
37
|
|
|
* @method void addListener(string $name, string $event, bool $queued = false) |
38
|
|
|
* @method void addJob(string $name, bool $sync = false) |
39
|
|
|
* @method void addCommand(string $name, ?string $commandName = null) |
40
|
|
|
* @method void addModel(string $name, bool $mongo = false, bool $migration = true) |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
class ModuleGenerator |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The module that will created. |
48
|
|
|
* |
49
|
|
|
* @var Module |
50
|
|
|
*/ |
51
|
|
|
protected $module; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The laravel filesystem instance. |
55
|
|
|
* |
56
|
|
|
* @var Filesystem |
57
|
|
|
*/ |
58
|
|
|
protected $filesystem; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Array of resources that will be generated upon execution. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $pipeline = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The constructor. |
69
|
|
|
* @param string $moduleName |
70
|
|
|
* @throws FileAlreadyExistException |
71
|
|
|
*/ |
72
|
|
|
public function __construct(string $moduleName) |
73
|
|
|
{ |
74
|
|
|
$this->module = $this->createModule($moduleName); |
75
|
|
|
$this->filesystem = new Filesystem(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function createModule($moduleName): Module |
79
|
|
|
{ |
80
|
|
|
$moduleName = Str::studly($moduleName); |
81
|
|
|
$path = Larapi::getModulesBasePath() . '/' . $moduleName; |
82
|
|
|
if (file_exists($path)) { |
83
|
|
|
throw new FileAlreadyExistException("Module exists already. Please remove the directory first"); |
84
|
|
|
} |
85
|
|
|
return new Module($moduleName, Larapi::getModulesBasePath() . '/' . $moduleName); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function __call($name, $arguments) |
89
|
|
|
{ |
90
|
|
|
if (Str::startsWith($name, 'add')) |
91
|
|
|
$this->pipeline[] = [ |
92
|
|
|
"name" => Str::replaceFirst('add', '', $name), |
93
|
|
|
"arguments" => $arguments |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function generate() |
99
|
|
|
{ |
100
|
|
|
foreach ($this->pipeline as $command) { |
101
|
|
|
$manager = GeneratorManager::module($this->getModule()->getName()); |
102
|
|
|
$method = 'create' . $command['name']; |
103
|
|
|
call_user_func_array(array($manager, $method), $command['arguments']); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the name of module will created. By default in studly case. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function getName() |
113
|
|
|
{ |
114
|
|
|
return $this->module->getName(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the laravel filesystem instance. |
119
|
|
|
* |
120
|
|
|
* @return Filesystem |
121
|
|
|
*/ |
122
|
|
|
protected function getFilesystem() |
123
|
|
|
{ |
124
|
|
|
return $this->filesystem; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return Module |
129
|
|
|
*/ |
130
|
|
|
public function getModule(): Module |
131
|
|
|
{ |
132
|
|
|
return $this->module; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getPipeline(): array |
139
|
|
|
{ |
140
|
|
|
return $this->pipeline; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|