Passed
Push — master ( 1f7fc5...40eec0 )
by Darío
02:01
created

AbstractModule::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Mvc;
12
13
/**
14
 * AbstractModule class
15
 *
16
 * This is an abstract class required for each mvc module. The first code execution
17
 * in a route is the module, after the module loads the controller.
18
 */
19
abstract class AbstractModule
20
{
21
    /**
22
     * The module name
23
     *
24
     * @var string
25
     */
26
    protected $moduleName;
27
28
    /**
29
     * The module path
30
     *
31
     * The path where modules are located.
32
     *
33
     * @var string
34
     */
35
    protected $modulePath;
36
37
    /**
38
     * The controller path
39
     *
40
     * The path where controllers are located.
41
     *
42
     * @var string
43
     */
44
    protected $contollerPath;
45
46
    /**
47
     * The view path
48
     *
49
     * The path where views are located.
50
     *
51
     * @var string
52
     */
53
    protected $viewPath;
54
55
    /**
56
     * The Router instace
57
     *
58
     * @var string
59
     */
60
    protected $router;
61
62
    /**
63
     * Returns the moduleName attribute
64
     *
65
     * @return string
66
     */
67
    public function getModuleName()
68
    {
69
        return $this->moduleName;
70
    }
71
72
    /**
73
     * Returns the modulePath attribute
74
     *
75
     * @return string
76
     */
77
    public function getModulePath()
78
    {
79
        return $this->modulePath;
80
    }
81
82
    /**
83
     * Returns the controllerPath attribute
84
     *
85
     * @return string
86
     */
87
    public function getControllerPath()
88
    {
89
        return $this->controllerPath;
90
    }
91
92
    /**
93
     * Returns the viewPath attribute
94
     *
95
     * @return string
96
     */
97
    public function getViewPath()
98
    {
99
        return $this->viewPath;
100
    }
101
102
    /**
103
     * Returns the Router instance
104
     *
105
     * @return Router
106
     */
107
    public function getRouter()
108
    {
109
        return $this->router;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->router returns the type string which is incompatible with the documented return type Drone\Mvc\Router.
Loading history...
110
    }
111
112
    /**
113
     * Sets the moduleName attribute
114
     *
115
     * @param string $moduleName
116
     *
117
     * @return null
118
     */
119
    public function setModuleName($moduleName)
120
    {
121
        $this->moduleName = $moduleName;
122
    }
123
124
    /**
125
     * Sets the modulePath attribute
126
     *
127
     * @param string $modulePath
128
     *
129
     * @return null
130
     */
131
    public function setModulePath($modulePath)
132
    {
133
        $this->modulePath = $modulePath;
134
    }
135
136
    /**
137
     * Sets the controllerPath attribute
138
     *
139
     * @param string $controllerPath
140
     *
141
     * @return null
142
     */
143
    public function setControllerPath($controllerPath)
144
    {
145
        $this->controllerPath = $controllerPath;
0 ignored issues
show
Bug Best Practice introduced by
The property controllerPath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
146
    }
147
148
    /**
149
     * Sets the viewPath attribute
150
     *
151
     * @param string $viewPath
152
     *
153
     * @return null
154
     */
155
    public function setViewPath($viewPath)
156
    {
157
        $this->viewPath = $viewPath;
158
    }
159
160
    /**
161
     * Sets the Router instance
162
     *
163
     * @param Router $router
164
     *
165
     * @return null
166
     */
167
    public function setRouter($router)
168
    {
169
        $this->router = $router;
0 ignored issues
show
Documentation Bug introduced by
It seems like $router of type Drone\Mvc\Router is incompatible with the declared type string of property $router.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
170
    }
171
172
    /**
173
     * Constructor
174
     *
175
     * @param string             $moduleName
176
     * @param AbstractController $controller
177
     * @param Router             $router
178
     */
179
    public function __construct($moduleName, AbstractController $controller, Router $router)
180
    {
181
        $this->moduleName = $moduleName;
182
        $this->router     = $router;
0 ignored issues
show
Documentation Bug introduced by
It seems like $router of type Drone\Mvc\Router is incompatible with the declared type string of property $router.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
183
        $this->init($controller);
184
    }
185
186
    /**
187
     * Abstract method to be executed before each controller execution in each module
188
     *
189
     * @param AbstractController
190
     */
191
    public abstract function init(AbstractController $controller);
192
193
    /**
194
     * Returns an array with application settings
195
     *
196
     * @return array
197
     */
198
    public function getConfig()
199
    {
200
        return include(
201
            $this->router->getBasePath() .'/'. $this->modulePath .'/' . $this->getModuleName() . '/config/module.config.php'
202
        );
203
    }
204
205
    /**
206
     * Creates an autoloader for module classes
207
     *
208
     * @param string $name
209
     *
210
     * @return null
211
     */
212
    public static function loader($name)
213
    {
214
        $nm = explode('\\', $name);
215
        $module = array_shift($nm);
216
217
        $class = $this->router->getBasePath() .'/'. $this->modulePath ."/". $module . "/source/" . implode("/", $nm) . ".php";
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using $this inside a static method is generally not recommended and can lead to errors in newer PHP versions.
Loading history...
218
219
        if (file_exists($class))
220
            include $class;
221
    }
222
}