Completed
Push — master ( d737b5...538bd2 )
by Afshin
02:57
created

ModuleInitializer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 9.4285
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: afshin
5
 * Date: 12/12/17
6
 * Time: 5:30 PM
7
 */
8
9
namespace Core;
10
use Composer\Autoload\ClassLoader;
11
12
13
/**
14
 * This will load modules
15
 */
16
class ModuleInitializer
17
{
18
19
    /**
20
     * @var Slim
0 ignored issues
show
Bug introduced by
The type Core\Slim was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
     */
22
    protected $app;
23
24
    /**
25
     * @var array
26
     */
27
    protected $initializerSettings;
28
29
    /**
30
     * @var array
31
     */
32
    protected $moduleInstances = [];
33
34
    public function __construct($app, $modules=array())
35
    {
36
        $this->app = $app;
37
        // build an class map of [[module => moduleClassPath], ..]
38
        foreach ($modules as $module) {
39
            $moduleClassName = sprintf('%s\Module', $module);
40
41
            $this->moduleInstances[$module] = new $moduleClassName();
42
        }
43
    }
44
45
    /**
46
     * Load the module. This will run for all modules, use for routes mainly
47
     * @param string $moduleName Module name
48
     */
49
    public function initModules()
50
    {
51
        $moduleInstances = $this->moduleInstances;
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleInstances is dead and can be removed.
Loading history...
52
        $app = $this->app;
53
        $container = $app->getContainer();
54
55
        // $this->initClassLoader($classLoader);
56
        $this->initModuleConfig();
57
        $this->initDependencies($container);
0 ignored issues
show
Unused Code introduced by
The call to Core\ModuleInitializer::initDependencies() has too many arguments starting with $container. ( Ignorable by Annotation )

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

57
        $this->/** @scrutinizer ignore-call */ 
58
               initDependencies($container);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
58
        $this->initMiddleware($app);
0 ignored issues
show
Unused Code introduced by
The call to Core\ModuleInitializer::initMiddleware() has too many arguments starting with $app. ( Ignorable by Annotation )

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

58
        $this->/** @scrutinizer ignore-call */ 
59
               initMiddleware($app);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
59
        $this->initRoutes($app);
0 ignored issues
show
Unused Code introduced by
The call to Core\ModuleInitializer::initRoutes() has too many arguments starting with $app. ( Ignorable by Annotation )

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

59
        $this->/** @scrutinizer ignore-call */ 
60
               initRoutes($app);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
60
61
    }
62
63
    /**
64
     * Load the module. This will run for all modules, use for routes mainly
65
     * @param string $moduleName Module name
66
     */
67
    public function getModuleConfig()
68
    {
69
        $moduleInstances = $this->moduleInstances;
70
71
        $allModules = [];
72
        foreach ($moduleInstances as $moduleName => $module) {
73
            $moduleSettings = $module->getModuleConfig();
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleSettings is dead and can be removed.
Loading history...
74
            $allModules[$moduleName] = $module->getModuleConfig();
75
        }
76
77
        return $allModules;
78
    }
79
80
    /**
81
     * Load the module. This will run for all modules, use for routes mainly
82
     * @param string $moduleName Module name
83
     */
84
    public function initModuleConfig()
85
    {
86
        $app = $this->app;
87
        $container = $app->getContainer();
88
89
        $allSettings = $container['settings']->all();
90
        if (!isset($allSettings['modules']) or !is_array($allSettings['modules'])) {
91
            $allSettings['modules'] = [];
92
        }
93
94
        $allSettings['modules'] = array_merge_recursive($allSettings['modules'], $this->getModuleConfig());
95
        $container['settings']->__construct( $allSettings );
96
    }
97
98
    /**
99
     * Load the module. This will run for all modules, use for routes mainly
100
     * @param string $moduleName Module name
101
     */
102
    public function initDependencies()
103
    {
104
        $moduleInstances = $this->moduleInstances;
105
        $app = $this->app;
106
        $container = $app->getContainer();
107
108
        // next, init dependencies of all modules now that we have settings, class maps etc
109
        foreach ($moduleInstances as $module) {
110
            $module->initDependencies($container);
111
        }
112
    }
113
114
    /**
115
     * Load the module. This will run for all modules, use for routes mainly
116
     * @param string $moduleName Module name
117
     */
118
    public function initMiddleware()
119
    {
120
        $moduleInstances = $this->moduleInstances;
121
        $app = $this->app;
122
123
        // next, init app middleware of all modules now that we have settings, class maps, dependencies etc
124
        foreach ($moduleInstances as $module) {
125
            $module->initMiddleware($app);
126
        }
127
    }
128
129
    /**
130
     * Load the module. This will run for all modules, use for routes mainly
131
     * @param string $moduleName Module name
132
     */
133
    public function initRoutes()
134
    {
135
        $moduleInstances = $this->moduleInstances;
136
        $app = $this->app;
137
138
        // lastly, routes
139
        foreach ($moduleInstances as $module) {
140
            $module->initRoutes($app);
141
        }
142
    }
143
}
144
145
146
147