Completed
Push — 3.0 ( fea5eb...c29f0e )
by Vermeulen
02:17
created

ModuleList::toRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BFW\Core\AppSystems;
4
5
class ModuleList extends AbstractSystem
6
{
7
    /**
8
     * @var \BFW\ModuleList|null $moduleList
9
     */
10
    protected $moduleList;
11
    
12
    /**
13
     * {@inheritdoc}
14
     * 
15
     * @return \BFW\ModuleList|null
16
     */
17
    public function __invoke()
18
    {
19
        return $this->moduleList;
20
    }
21
22
    /**
23
     * Getter accessor to property moduleList
24
     * 
25
     * @return \BFW\ModuleList|null
26
     */
27
    public function getModuleList()
28
    {
29
        return $this->moduleList;
30
    }
31
    
32
    /**
33
     * {@inheritdoc}
34
     * Initialize the ModuleList system
35
     */
36
    public function init()
37
    {
38
        $this->moduleList = new \BFW\ModuleList;
39
        $this->initStatus = true;
40
    }
41
    
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function toRun()
46
    {
47
        return true;
48
    }
49
    
50
    /**
51
     * {@inheritdoc}
52
     * 
53
     * Load all modules, run all core and app modules
54
     */
55
    public function run()
56
    {
57
        $this->loadAllModules();
58
        $this->runAllCoreModules();
59
        $this->runAllAppModules();
60
        
61
        $this->runStatus = true;
62
    }
63
    
64
    /**
65
     * Read all directories in modules directory and add each module to Modules
66
     * class.
67
     * Generate the load tree.
68
     * Not initialize modules !
69
     * 
70
     * @return void
71
     */
72
    protected function loadAllModules()
73
    {
74
        $listModules = array_diff(scandir(MODULES_DIR), ['.', '..']);
75
76
        foreach ($listModules as $moduleName) {
77
            $modulePath = realpath(MODULES_DIR.$moduleName); //Symlink
78
79
            if (!is_dir($modulePath)) {
80
                continue;
81
            }
82
83
            $this->moduleList->addModule($moduleName);
84
        }
85
86
        $this->moduleList->readNeedMeDependencies();
87
        $this->moduleList->generateTree();
88
    }
89
90
    /**
91
     * Load core modules defined into config bfw file.
92
     * Only module for controller, router, database and template only.
93
     * 
94
     * @return void
95
     */
96
    protected function runAllCoreModules()
97
    {
98
        $allModules = \BFW\Application::getInstance()
0 ignored issues
show
Documentation Bug introduced by
The method getConfig does not exist on object<BFW\Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
99
            ->getConfig()
100
            ->getValue('modules', 'modules.php')
101
        ;
102
        
103
        foreach ($allModules as $moduleInfos) {
104
            $moduleName    = $moduleInfos['name'];
105
            $moduleEnabled = $moduleInfos['enabled'];
106
107
            if (empty($moduleName) || $moduleEnabled === false) {
108
                continue;
109
            }
110
111
            $this->runModule($moduleName);
112
        }
113
    }
114
115
    /**
116
     * Load all modules (except core).
117
     * Get the load tree, read him and load all modules with the order
118
     * declared into the tree.
119
     * 
120
     * @return void
121
     */
122
    protected function runAllAppModules()
123
    {
124
        $tree = $this->moduleList->getLoadTree();
125
126
        foreach ($tree as $firstLine) {
127
            foreach ($firstLine as $secondLine) {
128
                foreach ($secondLine as $moduleName) {
129
                    $this->runModule($moduleName);
130
                }
131
            }
132
        }
133
    }
134
135
    /**
136
     * Load a module
137
     * 
138
     * @param string $moduleName The module's name to load
139
     * 
140
     * @return void
141
     */
142
    protected function runModule($moduleName)
143
    {
144
        $app = \BFW\Application::getInstance();
145
        
146
        $app->getSubjectList()
0 ignored issues
show
Documentation Bug introduced by
The method getSubjectList does not exist on object<BFW\Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
147
            ->getSubjectByName('ApplicationTasks')
148
            ->sendNotify('BfwApp_run_module_'.$moduleName);
149
        
150
        $this->moduleList
151
            ->getModuleByName($moduleName)
152
            ->runModule();
153
    }
154
}
155