|
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
|
|
|
'console' |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
protected $cacheFile = 'bootstrap.php'; |
|
22
|
|
|
|
|
23
|
|
|
protected $bootstrap; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->files = new \Illuminate\Filesystem\Filesystem(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function cache() |
|
31
|
|
|
{ |
|
32
|
|
|
$boostrap = $this->scanDirectories(); |
|
33
|
|
|
$this->bootstrap = $boostrap; |
|
34
|
|
|
$this->files->put($this->getCachePath(), serialize($boostrap)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function scanDirectories(): array |
|
38
|
|
|
{ |
|
39
|
|
|
$bootstrap = []; |
|
40
|
|
|
|
|
41
|
|
|
foreach (\Module::all() as $module) { |
|
42
|
|
|
foreach ($this->moduleEntityDirectories as $directory) { |
|
43
|
|
|
$directory = ucfirst($directory); |
|
44
|
|
|
$directoryPath = $module->getPath() . '/' . $directory; |
|
45
|
|
|
$namespace = 'Modules' . '\\' . $module->getName(); |
|
46
|
|
|
if (file_exists($directoryPath)) { |
|
47
|
|
|
$files = scandir($directoryPath); |
|
48
|
|
|
foreach ($files as $fileName) { |
|
49
|
|
|
if ($this->isPhpFile($fileName)) { |
|
50
|
|
|
$className = basename($fileName, '.php'); |
|
51
|
|
|
$class = $namespace . '\\' . $directory . '\\' . $className; |
|
52
|
|
|
try { |
|
53
|
|
|
if (new $class() instanceof Command) { |
|
54
|
|
|
$bootstrap['commands'][] = $class; |
|
55
|
|
|
} |
|
56
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $bootstrap; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function isPhpFile(string $fileName): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return strlen($fileName) > 5 && '.php' === ($fileName[-4] . $fileName[-3] . $fileName[-2] . $fileName[-1]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function loadBootstrapFromCache() |
|
73
|
|
|
{ |
|
74
|
|
|
if (!isset($this->bootstrap)) { |
|
75
|
|
|
$commandCachePath = $this->getCachePath(); |
|
76
|
|
|
|
|
77
|
|
|
if (!file_exists($commandCachePath)) { |
|
78
|
|
|
$this->cache(); |
|
79
|
|
|
} |
|
80
|
|
|
$this->boostrap = unserialize($this->files->get($commandCachePath)); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
return $this->bootstrap; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getCommands(): array |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->loadBootstrapFromCache()['commands']; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getCachePath(): string |
|
91
|
|
|
{ |
|
92
|
|
|
return app()->bootstrapPath() . '/cache/' . $this->cacheFile; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|