Completed
Push — master ( 6f4788...09af27 )
by Arthur
14:46
created

BootstrapRegistrarService::cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
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
        '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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
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));
0 ignored issues
show
Bug Best Practice introduced by
The property boostrap does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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;
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

92
        return app()->/** @scrutinizer ignore-call */ bootstrapPath() . '/cache/' . $this->cacheFile;
Loading history...
93
    }
94
}
95