1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: arthur |
5
|
|
|
* Date: 04.10.18 |
6
|
|
|
* Time: 02:13. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Foundation\Services; |
10
|
|
|
|
11
|
|
|
use Illuminate\Console\Command; |
12
|
|
|
|
13
|
|
|
class BootstrapRegistrarService |
14
|
|
|
{ |
15
|
|
|
protected $files; |
16
|
|
|
|
17
|
|
|
protected $moduleEntityDirectories = [ |
18
|
|
|
'commands' => 'Console', |
19
|
|
|
'routes' => 'Routes', |
20
|
|
|
'configs' => 'Config', |
21
|
|
|
'factories' => 'Database/factories', |
22
|
|
|
'migrations' => 'Database/Migrations', |
23
|
|
|
'seeders' => 'Database/Seeders', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
protected $cacheFile = 'bootstrap.php'; |
27
|
|
|
|
28
|
|
|
protected $bootstrap; |
29
|
|
|
|
30
|
5 |
|
public function __construct() |
31
|
|
|
{ |
32
|
5 |
|
$this->files = new \Illuminate\Filesystem\Filesystem(); |
33
|
5 |
|
} |
34
|
|
|
|
35
|
5 |
|
public function recache() |
36
|
|
|
{ |
37
|
5 |
|
$this->buildBootstrapData(); |
38
|
5 |
|
$this->storeInCache($this->bootstrap); |
39
|
5 |
|
} |
40
|
|
|
|
41
|
5 |
|
private function storeInCache($data) |
42
|
|
|
{ |
43
|
5 |
|
file_put_contents($this->getCachePath(), '<?php return ' . var_export($data, true) . ';'); |
44
|
5 |
|
} |
45
|
|
|
|
46
|
|
|
public function readFromCache() |
47
|
|
|
{ |
48
|
|
|
return include $this->getCachePath(); |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
public function clearCache() |
52
|
|
|
{ |
53
|
2 |
|
unlink($this->getCachePath()); |
54
|
2 |
|
} |
55
|
|
|
|
56
|
2 |
|
public function cacheExists() |
57
|
|
|
{ |
58
|
2 |
|
return file_exists($this->getCachePath()); |
59
|
|
|
} |
60
|
|
|
|
61
|
5 |
|
private function getCachePath(): string |
62
|
|
|
{ |
63
|
5 |
|
return app()->bootstrapPath() . '/cache/' . $this->cacheFile; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
private function buildEmptyBootstrapArray() |
67
|
|
|
{ |
68
|
5 |
|
$bootstrapArray = []; |
69
|
5 |
|
foreach ($this->moduleEntityDirectories as $key => $directory) { |
70
|
5 |
|
$bootstrapArray[$key] = []; |
71
|
|
|
} |
72
|
5 |
|
return $bootstrapArray; |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
private function buildBootstrapData() |
76
|
|
|
{ |
77
|
5 |
|
$bootstrap = $this->buildEmptyBootstrapArray(); |
78
|
5 |
|
foreach (\Module::all() as $module) { |
79
|
5 |
|
foreach ($this->moduleEntityDirectories as $key => $directory) { |
80
|
5 |
|
$directory = ucfirst($directory); |
81
|
5 |
|
$directoryPath = $module->getPath() . '/' . $directory; |
82
|
5 |
|
$namespace = 'Modules' . '\\' . $module->getName(); |
83
|
5 |
|
if (file_exists($directoryPath)) { |
84
|
5 |
|
$files = scandir($directoryPath); |
85
|
5 |
|
foreach ($files as $fileName) { |
86
|
5 |
|
if ($this->hasPhpExtension($fileName)) { |
87
|
5 |
|
$className = basename($fileName, '.php'); |
88
|
5 |
|
$class = $namespace . '\\' . str_replace('/', '\\', $directory) . '\\' . $className; |
89
|
5 |
|
switch ($key) { |
90
|
5 |
|
case 'commands': |
91
|
|
|
try { |
92
|
5 |
|
$command = new $class(); |
93
|
5 |
|
if ($command instanceof Command) { |
94
|
5 |
|
$bootstrap[$key][] = $class; |
95
|
|
|
} |
96
|
|
|
} catch (\Exception $e) { |
97
|
|
|
break; |
98
|
|
|
} |
99
|
5 |
|
break; |
100
|
5 |
|
case 'routes': |
101
|
5 |
|
$bootstrap[$key][] = $this->buildRouteArray($directoryPath . '/' . $fileName, $namespace); |
102
|
5 |
|
break; |
103
|
5 |
|
case 'configs': |
104
|
5 |
|
$bootstrap[$key][] = $this->buildConfigArray($directoryPath . '/' . $fileName, $module->getName()); |
105
|
5 |
|
break; |
106
|
5 |
|
case 'factories': |
107
|
5 |
|
$bootstrap[$key][] = $this->buildDirectoryPathArray($directoryPath); |
108
|
5 |
|
break; |
109
|
5 |
|
case 'migrations': |
110
|
5 |
|
$bootstrap[$key][] = $this->buildDirectoryPathArray($directoryPath); |
111
|
5 |
|
break; |
112
|
5 |
|
case 'seeders': |
113
|
5 |
|
$bootstrap[$key][] = $class; |
114
|
5 |
|
break; |
115
|
|
|
default: |
116
|
5 |
|
break; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
5 |
|
$this->bootstrap = $bootstrap; |
125
|
5 |
|
} |
126
|
|
|
|
127
|
5 |
|
private function hasPhpExtension(string $fileName): bool |
128
|
|
|
{ |
129
|
5 |
|
return strlen($fileName) > 4 && '.php' === ($fileName[-4] . $fileName[-3] . $fileName[-2] . $fileName[-1]); |
130
|
|
|
} |
131
|
|
|
|
132
|
5 |
|
public function loadBootstrapFromCache() |
133
|
|
|
{ |
134
|
5 |
|
if (!isset($this->bootstrap)) { |
135
|
|
|
if ($this->cacheExists()) { |
136
|
|
|
$this->bootstrap = $this->readFromCache(); |
137
|
|
|
} else { |
138
|
|
|
$this->recache(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
5 |
|
return $this->bootstrap; |
142
|
|
|
} |
143
|
|
|
|
144
|
5 |
|
private function buildRouteArray($path, $namespace) |
145
|
|
|
{ |
146
|
5 |
|
$apiDomain = strtolower(env('API_URL')); |
147
|
5 |
|
$apiDomain = str_replace('http://', '', $apiDomain); |
148
|
5 |
|
$apiDomain = str_replace('https://', '', $apiDomain); |
149
|
5 |
|
$moduleNamespace = $namespace; |
150
|
5 |
|
$moduleName = explode('\\', $moduleNamespace)[1]; |
151
|
5 |
|
$controllerNamespace = $moduleNamespace . '\\' . 'Http\\Controllers'; |
152
|
5 |
|
$modelNameSpace = $moduleNamespace . '\\' . 'Entities\\' . $moduleName; |
153
|
|
|
return [ |
154
|
5 |
|
"path" => $path, |
155
|
5 |
|
"namespace" => $namespace, |
156
|
5 |
|
"module" => strtolower($moduleName), |
157
|
5 |
|
"domain" => $apiDomain, |
158
|
5 |
|
"controller" => $controllerNamespace, |
159
|
5 |
|
"model" => $modelNameSpace, |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
|
163
|
5 |
|
private function buildConfigArray($path, $module) |
164
|
|
|
{ |
165
|
|
|
return [ |
166
|
5 |
|
"path" => $path, |
167
|
5 |
|
"module" => strtolower($module) |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
|
171
|
5 |
|
private function buildDirectoryPathArray($path) |
172
|
|
|
{ |
173
|
|
|
return [ |
174
|
5 |
|
"path" => $path |
175
|
|
|
]; |
176
|
|
|
} |
177
|
|
|
|
178
|
5 |
|
public function getCommands(): array |
179
|
|
|
{ |
180
|
5 |
|
return $this->loadBootstrapFromCache()['commands'] ?? []; |
181
|
|
|
} |
182
|
|
|
|
183
|
5 |
|
public function getRoutes(): array |
184
|
|
|
{ |
185
|
5 |
|
return $this->loadBootstrapFromCache()['routes'] ?? []; |
186
|
|
|
} |
187
|
|
|
|
188
|
5 |
|
public function getConfigs(): array |
189
|
|
|
{ |
190
|
5 |
|
return $this->loadBootstrapFromCache()['configs'] ?? []; |
191
|
|
|
} |
192
|
|
|
|
193
|
5 |
|
public function getFactories(): array |
194
|
|
|
{ |
195
|
5 |
|
return $this->loadBootstrapFromCache()['factories'] ?? []; |
196
|
|
|
} |
197
|
|
|
|
198
|
5 |
|
public function getMigrations(): array |
199
|
|
|
{ |
200
|
5 |
|
return $this->loadBootstrapFromCache()['migrations'] ?? []; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getSeeders(): array |
204
|
|
|
{ |
205
|
|
|
return $this->loadBootstrapFromCache()['seeders'] ?? []; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|