Passed
Push — master ( 7af546...dfd36b )
by Arthur
04:54 queued 15s
created

loadBootstrapFromCache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 0
b 0
f 0
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 7
    public function __construct()
31
    {
32 7
        $this->files = new \Illuminate\Filesystem\Filesystem();
33 7
    }
34
35 7
    public function recache()
36
    {
37 7
        $this->buildBootstrapData();
38 7
        $this->storeInCache($this->bootstrap);
39 7
    }
40
41 7
    private function storeInCache($data)
42
    {
43 7
        file_put_contents($this->getCachePath(), '<?php return '.var_export($data, true).';');
44 7
    }
45
46 1
    public function readFromCache()
47
    {
48 1
        return include $this->getCachePath();
49
    }
50
51 3
    public function clearCache()
52
    {
53 3
        unlink($this->getCachePath());
54 3
    }
55
56 4
    public function cacheExists()
57
    {
58 4
        return file_exists($this->getCachePath());
59
    }
60
61 7
    private function getCachePath(): string
62
    {
63 7
        return app()->bootstrapPath().'/cache/'.$this->cacheFile;
1 ignored issue
show
introduced by
The method bootstrapPath() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        return app()->/** @scrutinizer ignore-call */ bootstrapPath().'/cache/'.$this->cacheFile;
Loading history...
64
    }
65
66 7
    private function buildEmptyBootstrapArray()
67
    {
68 7
        $bootstrapArray = [];
69 7
        foreach ($this->moduleEntityDirectories as $key => $directory) {
70 7
            $bootstrapArray[$key] = [];
71
        }
72
73 7
        return $bootstrapArray;
74
    }
75
76 7
    private function buildBootstrapData()
77
    {
78 7
        $bootstrap = $this->buildEmptyBootstrapArray();
79 7
        foreach (\Module::all() as $module) {
80 7
            foreach ($this->moduleEntityDirectories as $key => $directory) {
81 7
                $directory = ucfirst($directory);
82 7
                $directoryPath = $module->getPath().'/'.$directory;
83 7
                $namespace = 'Modules'.'\\'.$module->getName();
84 7
                if (file_exists($directoryPath)) {
85 7
                    $files = scandir($directoryPath);
86 7
                    foreach ($files as $fileName) {
87 7
                        if ($this->hasPhpExtension($fileName)) {
88 7
                            $className = basename($fileName, '.php');
89 7
                            $class = $namespace.'\\'.str_replace('/', '\\', $directory).'\\'.$className;
90 7
                            switch ($key) {
91 7
                                case 'commands':
92
                                    try {
93 7
                                        $command = new $class();
94 7
                                        if ($command instanceof Command) {
95 7
                                            $bootstrap[$key][] = $class;
96
                                        }
97
                                    } catch (\Exception $e) {
98
                                        break;
99
                                    }
100 7
                                    break;
101 7
                                case 'routes':
102 7
                                    $bootstrap[$key][] = $this->buildRouteArray($directoryPath.'/'.$fileName, $namespace);
103 7
                                    break;
104 7
                                case 'configs':
105 7
                                    $bootstrap[$key][] = $this->buildConfigArray($directoryPath.'/'.$fileName, $module->getName());
106 7
                                    break;
107 7
                                case 'factories':
108 7
                                    $bootstrap[$key][] = $this->buildDirectoryPathArray($directoryPath);
109 7
                                    break;
110 7
                                case 'migrations':
111 7
                                    $bootstrap[$key][] = $this->buildDirectoryPathArray($directoryPath);
112 7
                                    break;
113 7
                                case 'seeders':
114 7
                                    $bootstrap[$key][] = $class;
115 7
                                    break;
116
                                default:
117 7
                                    break;
118
                            }
119
                        }
120
                    }
121
                }
122
            }
123
        }
124
125 7
        $this->bootstrap = $bootstrap;
126 7
    }
127
128 7
    private function hasPhpExtension(string $fileName): bool
129
    {
130 7
        return strlen($fileName) > 4 && '.php' === ($fileName[-4].$fileName[-3].$fileName[-2].$fileName[-1]);
131
    }
132
133 7
    public function loadBootstrapFromCache()
134
    {
135 7
        if (!isset($this->bootstrap)) {
136 1
            if ($this->cacheExists()) {
137 1
                $this->bootstrap = $this->readFromCache();
138
            } else {
139
                $this->recache();
140
            }
141
        }
142
143 7
        return $this->bootstrap;
144
    }
145
146 7
    private function buildRouteArray($path, $namespace)
147
    {
148 7
        $apiDomain = strtolower(env('API_URL'));
149 7
        $apiDomain = str_replace('http://', '', $apiDomain);
150 7
        $apiDomain = str_replace('https://', '', $apiDomain);
151 7
        $moduleNamespace = $namespace;
152 7
        $moduleName = explode('\\', $moduleNamespace)[1];
153 7
        $controllerNamespace = $moduleNamespace.'\\'.'Http\\Controllers';
154 7
        $modelNameSpace = $moduleNamespace.'\\'.'Entities\\'.$moduleName;
155
156
        return [
157 7
            'path'       => $path,
158 7
            'namespace'  => $namespace,
159 7
            'module'     => strtolower($moduleName),
160 7
            'domain'     => $apiDomain,
161 7
            'controller' => $controllerNamespace,
162 7
            'model'      => $modelNameSpace,
163
        ];
164
    }
165
166 7
    private function buildConfigArray($path, $module)
167
    {
168
        return [
169 7
            'path'   => $path,
170 7
            'module' => strtolower($module),
171
        ];
172
    }
173
174 7
    private function buildDirectoryPathArray($path)
175
    {
176
        return [
177 7
            'path' => $path,
178
        ];
179
    }
180
181 7
    public function getCommands(): array
182
    {
183 7
        return $this->loadBootstrapFromCache()['commands'] ?? [];
184
    }
185
186 7
    public function getRoutes(): array
187
    {
188 7
        return $this->loadBootstrapFromCache()['routes'] ?? [];
189
    }
190
191 7
    public function getConfigs(): array
192
    {
193 7
        return $this->loadBootstrapFromCache()['configs'] ?? [];
194
    }
195
196 7
    public function getFactories(): array
197
    {
198 7
        return $this->loadBootstrapFromCache()['factories'] ?? [];
199
    }
200
201 7
    public function getMigrations(): array
202
    {
203 7
        return $this->loadBootstrapFromCache()['migrations'] ?? [];
204
    }
205
206
    public function getSeeders(): array
207
    {
208
        return $this->loadBootstrapFromCache()['seeders'] ?? [];
209
    }
210
}
211