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
|
4 |
|
public function __construct() |
31
|
|
|
{ |
32
|
4 |
|
$this->files = new \Illuminate\Filesystem\Filesystem(); |
33
|
4 |
|
} |
34
|
|
|
|
35
|
4 |
|
public function cache() |
36
|
|
|
{ |
37
|
4 |
|
$this->buildBootstrapArray(); |
38
|
4 |
|
$this->files->put($this->getCachePath(), serialize($this->bootstrap)); |
39
|
4 |
|
} |
40
|
|
|
|
41
|
4 |
|
public function buildBootstrapArray() |
42
|
|
|
{ |
43
|
4 |
|
$bootstrap = []; |
44
|
4 |
|
foreach (\Module::all() as $module) { |
45
|
4 |
|
foreach ($this->moduleEntityDirectories as $key => $directory) { |
46
|
4 |
|
$directory = ucfirst($directory); |
47
|
4 |
|
$directoryPath = $module->getPath().'/'.$directory; |
48
|
4 |
|
$namespace = 'Modules'.'\\'.$module->getName(); |
49
|
4 |
|
if (file_exists($directoryPath)) { |
50
|
4 |
|
$files = scandir($directoryPath); |
51
|
4 |
|
foreach ($files as $fileName) { |
52
|
4 |
|
if ($this->hasPhpExtension($fileName)) { |
53
|
4 |
|
$className = basename($fileName, '.php'); |
54
|
4 |
|
$class = $namespace.'\\'.str_replace('/', '\\', $directory).'\\'.$className; |
55
|
4 |
|
switch ($key) { |
56
|
4 |
|
case 'commands': |
57
|
|
|
try { |
58
|
4 |
|
$command = new $class(); |
59
|
4 |
|
if ($command instanceof Command) { |
60
|
4 |
|
$bootstrap[$key][] = $class; |
61
|
|
|
} |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
break; |
64
|
|
|
} |
65
|
4 |
|
break; |
66
|
4 |
|
case 'routes': |
67
|
4 |
|
$bootstrap[$key][] = [$directoryPath.'/'.$fileName, $namespace]; |
68
|
4 |
|
break; |
69
|
4 |
|
case 'configs': |
70
|
4 |
|
$bootstrap[$key][] = [$directoryPath.'/'.$fileName, strtolower($module->getName())]; |
71
|
4 |
|
break; |
72
|
4 |
|
case 'factories': |
73
|
4 |
|
$bootstrap[$key][] = $directoryPath; |
74
|
4 |
|
break; |
75
|
4 |
|
case 'migrations': |
76
|
4 |
|
$bootstrap[$key][] = $directoryPath; |
77
|
4 |
|
break; |
78
|
4 |
|
case 'seeders': |
79
|
4 |
|
$bootstrap[$key][] = $class; |
80
|
4 |
|
break; |
81
|
|
|
default: |
82
|
4 |
|
break; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
$this->bootstrap = $bootstrap; |
91
|
4 |
|
} |
92
|
|
|
|
93
|
4 |
|
private function hasPhpExtension(string $fileName): bool |
94
|
|
|
{ |
95
|
4 |
|
return strlen($fileName) > 4 && '.php' === ($fileName[-4].$fileName[-3].$fileName[-2].$fileName[-1]); |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
private function loadBootstrapFromCache() |
99
|
|
|
{ |
100
|
4 |
|
if (!isset($this->bootstrap)) { |
101
|
|
|
if (file_exists($this->getCachePath())) { |
102
|
|
|
$this->bootstrap = unserialize($this->files->get($this->getCachePath())); |
103
|
|
|
} else { |
104
|
|
|
$this->cache(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
4 |
|
return $this->bootstrap; |
109
|
|
|
} |
110
|
|
|
|
111
|
4 |
|
public function getCommands(): array |
112
|
|
|
{ |
113
|
4 |
|
return $this->loadBootstrapFromCache()['commands'] ?? []; |
114
|
|
|
} |
115
|
|
|
|
116
|
4 |
|
public function getRoutes(): array |
117
|
|
|
{ |
118
|
4 |
|
return $this->loadBootstrapFromCache()['routes'] ?? []; |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
public function getConfigs(): array |
122
|
|
|
{ |
123
|
4 |
|
return $this->loadBootstrapFromCache()['configs'] ?? []; |
124
|
|
|
} |
125
|
|
|
|
126
|
4 |
|
public function getFactories(): array |
127
|
|
|
{ |
128
|
4 |
|
return $this->loadBootstrapFromCache()['factories'] ?? []; |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
public function getMigrations(): array |
132
|
|
|
{ |
133
|
4 |
|
return $this->loadBootstrapFromCache()['migrations'] ?? []; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getSeeders(): array |
137
|
|
|
{ |
138
|
|
|
return $this->loadBootstrapFromCache()['seeders'] ?? []; |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
public function getCachePath(): string |
142
|
|
|
{ |
143
|
4 |
|
return app()->bootstrapPath().'/cache/'.$this->cacheFile; |
|
|
|
|
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|