Completed
Push — master ( 2f8bc7...807794 )
by Mihail
02:57
created

BootManager::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
namespace Ffcms\Core\Managers;
4
5
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\Controller;
8
use Ffcms\Core\Helper\FileSystem\Directory;
9
use Ffcms\Core\Helper\FileSystem\File;
10
use Ffcms\Core\Helper\Type\Obj;
11
use Ffcms\Core\Helper\Type\Str;
12
13
/**
14
 * Class BootManager. Manage auto executed boot methods in widgets and applications.
15
 * @package Ffcms\Core\Managers
16
 */
17
class BootManager
18
{
19
    CONST CACHE_TREE_TIME = 120;
20
21
    private $loader;
22
    private $appRoots = [];
23
    private $widgetRoots = [];
24
25
    private $objects = [];
26
27
    /**
28
     * BootManager constructor. Pass composer loader inside
29
     * @param bool $loader
30
     */
31
    public function __construct($loader = false)
32
    {
33
        // pass loader inside
34
        $this->loader = $loader;
35
36
        if ($this->loader !== false) {
37
            $this->parseComposerLoader();
38
        }
39
40
        // try to get bootable class map from cache, or initialize parsing
41
        if (App::$Cache->get('boot.class.map') !== null) {
42
            $this->objects = App::$Cache->get('boot.class.map');
43
        } else {
44
            $this->compileBootableClasses();
45
            App::$Cache->set('boot.class.map', $this->objects, static::CACHE_TREE_TIME);
46
        }
47
    }
48
49
    /**
50
     * Find app's and widgets root directories over composer psr loader
51
     */
52
    private function parseComposerLoader()
53
    {
54
        // get composer autoload map
55
        $map = $this->loader->getPrefixes();
0 ignored issues
show
Bug introduced by
The method getPrefixes cannot be called on $this->loader (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
56
        if (Obj::isArray($map)) {
57
            // get all available apps root dirs by psr loader for apps
58
            if (array_key_exists('Apps\\', $map)) {
59
                foreach ($map['Apps\\'] as $appPath) {
60
                    $this->appRoots[] = $appPath;
61
                }
62
            }
63
64
            // get Widgets map
65
            if (array_key_exists('Widgets\\', $map)) {
66
                // get all available root dirs by psr loader for widgets
67
                foreach ($map['Widgets\\'] as $widgetPath) {
68
                    $this->widgetRoots[] = $widgetPath;
69
                }
70
            }
71
        }
72
73
        // set default root path if not found anything else
74
        if (count($this->appRoots) < 1) {
75
            $this->appRoots = [root];
76
        }
77
78
        if (count($this->widgetRoots) < 1) {
79
            $this->widgetRoots = [root];
80
        }
81
    }
82
83
    /**
84
     * Find all bootatble instances and set it to object map
85
     */
86
    public function compileBootableClasses()
87
    {
88
        // list app root's
89 View Code Duplication
        foreach ($this->appRoots as $app) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
            $app .= '/Apps/Controller/' . env_name;
91
            $files = File::listFiles($app, ['.php'], true);
92
            foreach ($files as $file) {
93
                // define full class name with namespace
94
                $class = 'Apps\Controller\\' . env_name . '\\' . Str::cleanExtension($file);
95
                // check if class exists (must be loaded over autoloader), boot method exist and this is controller instanceof
96
                if (class_exists($class) && method_exists($class, 'boot') && is_a($class, 'Ffcms\Core\Arch\Controller', true)) {
97
                    $this->objects[] = $class;
98
                }
99
            }
100
        }
101
102
        // list widget root's
103 View Code Duplication
        foreach ($this->widgetRoots as $widget) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
            $widget .= '/Widgets/' . env_name;
105
            // widgets are packed in directory, classname should be the same with root directory name
106
            $dirs = Directory::scan($widget, GLOB_ONLYDIR, true);
107
            foreach ($dirs as $instance) {
0 ignored issues
show
Bug introduced by
The expression $dirs of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
108
                $class = 'Widgets\\' . env_name . '\\' . $instance . '\\' . $instance;
109
                if (class_exists($class) && method_exists($class, 'boot') && is_a($class, 'Ffcms\Core\Arch\Widget', true)) {
110
                    $this->objects[] = $class;
111
                }
112
            }
113
        }
114
    }
115
116
    /**
117
     * Call bootable methods in apps and widgets
118
     * @return bool
119
     */
120
    public function run()
121
    {
122
        if (!Obj::isArray($this->objects)) {
123
            return false;
124
        }
125
126
        foreach ($this->objects as $class) {
127
            forward_static_call([$class, 'boot']);
128
        }
129
        return true;
130
    }
131
}