Passed
Push — master ( 8f50d0...6b8118 )
by Darío
01:40
created

AbstractModule::setModulePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
use Drone\Mvc\AbstractController;
14
15
/**
16
 * AbstractModule class
17
 *
18
 * This is an abstract class required for each mvc module. The first code execution
19
 * in a route is the module, after the module loads the controller.
20
 */
21
abstract class AbstractModule
22
{
23
    /**
24
     * The module name
25
     *
26
     * @var string
27
     */
28
    protected $moduleName;
29
30
    /**
31
     * The module path
32
     *
33
     * The path where modules are located.
34
     *
35
     * @var string
36
     */
37
    protected $modulePath;
38
39
    /**
40
     * The controller path
41
     *
42
     * The path where controllers are located.
43
     *
44
     * @var string
45
     */
46
    protected $contollerPath;
47
48
    /**
49
     * The view path
50
     *
51
     * The path where views are located.
52
     *
53
     * @var string
54
     */
55
    protected $viewPath;
56
57
    /**
58
     * Returns the moduleName attribute
59
     *
60
     * @return string
61
     */
62
    public function getModuleName()
63
    {
64
        return $this->moduleName;
65
    }
66
67
    /**
68
     * Returns the modulePath attribute
69
     *
70
     * @return string
71
     */
72
    public function getModulePath()
73
    {
74
        return $this->modulePath;
75
    }
76
77
    /**
78
     * Returns the controllerPath attribute
79
     *
80
     * @return string
81
     */
82
    public function getControllerPath()
83
    {
84
        return $this->controllerPath;
85
    }
86
87
    /**
88
     * Returns the viewPath attribute
89
     *
90
     * @return string
91
     */
92
    public function getViewPath()
93
    {
94
        return $this->viewPath;
95
    }
96
97
    /**
98
     * Sets the moduleName attribute
99
     *
100
     * @param string $moduleName
101
     *
102
     * @return null
103
     */
104
    public function setModuleName($moduleName)
105
    {
106
        $this->moduleName = $moduleName;
107
    }
108
109
    /**
110
     * Sets the modulePath attribute
111
     *
112
     * @param string $modulePath
113
     *
114
     * @return null
115
     */
116
    public function setModulePath($modulePath)
117
    {
118
        $this->modulePath = $modulePath;
119
    }
120
121
    /**
122
     * Sets the controllerPath attribute
123
     *
124
     * @param string $controllerPath
125
     *
126
     * @return null
127
     */
128
    public function setControllerPath($controllerPath)
129
    {
130
        $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...
131
    }
132
133
    /**
134
     * Sets the viewPath attribute
135
     *
136
     * @param string $viewPath
137
     *
138
     * @return null
139
     */
140
    public function setViewPath($viewPath)
141
    {
142
        $this->viewPath = $viewPath;
143
    }
144
145
    /**
146
     * Constructor
147
     *
148
     * @param string             $moduleName
149
     * @param AbstractController $controller
150
     */
151
    public function __construct($moduleName, AbstractController $controller)
152
    {
153
        $this->moduleName = $moduleName;
154
        $this->init($controller);
155
    }
156
157
    /**
158
     * Abstract method to be executed before each controller execution in each module
159
     *
160
     * @param AbstractController
161
     */
162
    public abstract function init(AbstractController $controller);
163
164
    /**
165
     * Returns an array with application settings
166
     *
167
     * @return array
168
     */
169
    public function getConfig()
170
    {
171
        return include($this->modulePath .'/' . $this->getModuleName() . '/config/module.config.php');
172
    }
173
174
    /**
175
     * Creates an autoloader for module classes
176
     *
177
     * @param string $name
178
     *
179
     * @return null
180
     */
181
    public static function loader($name)
182
    {
183
        $nm = explode('\\', $name);
184
        $module = array_shift($nm);
185
186
        $class = $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...
187
188
        if (file_exists($class))
189
            include $class;
190
    }
191
}