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 base path of the application |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $basePath; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The module path |
37
|
|
|
* |
38
|
|
|
* The path where modules are located. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $modulePath; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The class path |
46
|
|
|
* |
47
|
|
|
* The path where classes are located (often controllers and models). |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $classPath; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The view path |
55
|
|
|
* |
56
|
|
|
* The path where views are located. |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $viewPath; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Config file for the module |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $configFile; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Defines method execution in the controller |
71
|
|
|
* |
72
|
|
|
* @var boolean |
73
|
|
|
*/ |
74
|
|
|
private $executionAllowed = true; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the moduleName attribute |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getModuleName() |
82
|
|
|
{ |
83
|
|
|
return $this->moduleName; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the base path of the application |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getBasePath() |
92
|
|
|
{ |
93
|
|
|
return $this->basePath; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the modulePath attribute |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getModulePath() |
102
|
|
|
{ |
103
|
|
|
return $this->modulePath; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the classPath attribute |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getClassPath() |
112
|
|
|
{ |
113
|
|
|
return $this->classPath; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the viewPath attribute |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getViewPath() |
122
|
|
|
{ |
123
|
|
|
return $this->viewPath; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the configFile attribute |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getConfigFile() |
132
|
|
|
{ |
133
|
|
|
return $this->configFile; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Sets the moduleName attribute |
138
|
|
|
* |
139
|
|
|
* @param string $moduleName |
140
|
|
|
* |
141
|
|
|
* @return null |
142
|
|
|
*/ |
143
|
|
|
public function setModuleName($moduleName) |
144
|
|
|
{ |
145
|
|
|
$this->moduleName = $moduleName; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Sets the basePath attribute |
150
|
|
|
* |
151
|
|
|
* @param string $basePath |
152
|
|
|
* |
153
|
|
|
* @return null |
154
|
|
|
*/ |
155
|
|
|
public function setBasePath($basePath) |
156
|
|
|
{ |
157
|
|
|
$this->basePath = $basePath; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Sets the modulePath attribute |
162
|
|
|
* |
163
|
|
|
* @param string $modulePath |
164
|
|
|
* |
165
|
|
|
* @return null |
166
|
|
|
*/ |
167
|
|
|
public function setModulePath($modulePath) |
168
|
|
|
{ |
169
|
|
|
$this->modulePath = $modulePath; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Sets the classPath attribute |
174
|
|
|
* |
175
|
|
|
* @param string $classPath |
176
|
|
|
* |
177
|
|
|
* @return null |
178
|
|
|
*/ |
179
|
|
|
public function setClassPath($classPath) |
180
|
|
|
{ |
181
|
|
|
$this->classPath = $classPath; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Sets the viewPath attribute |
186
|
|
|
* |
187
|
|
|
* @param string $viewPath |
188
|
|
|
* |
189
|
|
|
* @return null |
190
|
|
|
*/ |
191
|
|
|
public function setViewPath($viewPath) |
192
|
|
|
{ |
193
|
|
|
$this->viewPath = $viewPath; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Sets the configFile attribute |
198
|
|
|
* |
199
|
|
|
* @param string $configFile |
200
|
|
|
* |
201
|
|
|
* @throws \RuntimeException |
202
|
|
|
* |
203
|
|
|
* @return null |
204
|
|
|
*/ |
205
|
|
|
public function setConfigFile($configFile) |
206
|
|
|
{ |
207
|
|
|
$_file = $this->basePath .'/'. $this->modulePath .'/'. $this->moduleName .'/'. $configFile; |
208
|
|
|
|
209
|
|
|
if (!file_exists($_file)) |
210
|
|
|
throw new \RuntimeException("The file '$_file' does not exists"); |
211
|
|
|
|
212
|
|
|
$this->configFile = |
213
|
|
|
$this->basePath .'/'. $this->modulePath .'/'. $this->moduleName .'/'. $configFile; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Constructor |
218
|
|
|
* |
219
|
|
|
* @param string $moduleName |
220
|
|
|
*/ |
221
|
|
|
public function __construct($moduleName) |
222
|
|
|
{ |
223
|
|
|
$this->moduleName = $moduleName; |
224
|
|
|
$this->init(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Abstract method to be executed before each method execution in the controller |
229
|
|
|
* |
230
|
|
|
* @param AbstractController |
231
|
|
|
*/ |
232
|
|
|
public abstract function init(); |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Checks if executionAllowed is true |
236
|
|
|
* |
237
|
|
|
* @return null |
238
|
|
|
*/ |
239
|
|
|
public function executionIsAllowed() |
240
|
|
|
{ |
241
|
|
|
return $this->executionAllowed; |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Disallow the method execution in a controller |
246
|
|
|
* |
247
|
|
|
* @return null |
248
|
|
|
*/ |
249
|
|
|
public function disallowExecution() |
250
|
|
|
{ |
251
|
|
|
$this->executionAllowed = false; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Allow the method execution in a controller |
256
|
|
|
* |
257
|
|
|
* @return null |
258
|
|
|
*/ |
259
|
|
|
public function allowExecution() |
260
|
|
|
{ |
261
|
|
|
$this->executionAllowed = true; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Returns the module configuration |
266
|
|
|
* |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
public function getConfig() |
270
|
|
|
{ |
271
|
|
|
return (array) include $this->configFile; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Creates an autoloader for module classes |
276
|
|
|
* |
277
|
|
|
* @param string $name |
278
|
|
|
* |
279
|
|
|
* @return null |
280
|
|
|
*/ |
281
|
|
|
public static function loader($name) |
282
|
|
|
{ |
283
|
|
|
$nm = explode('\\', $name); |
284
|
|
|
$module = array_shift($nm); |
285
|
|
|
|
286
|
|
|
$class = ""; |
287
|
|
|
|
288
|
|
|
if (!empty($this->basePath)) |
|
|
|
|
289
|
|
|
$class .= $this->basePath .'/'; |
290
|
|
|
|
291
|
|
|
if (!empty($this->modulePath)) |
292
|
|
|
$class .= $this->modulePath .'/'; |
293
|
|
|
|
294
|
|
|
$class .= $module ."/"; |
295
|
|
|
|
296
|
|
|
if (!empty($this->classPath)) |
297
|
|
|
$class .= $this->classPath .'/'; |
298
|
|
|
|
299
|
|
|
$class .= implode("/", $nm) . ".php"; |
300
|
|
|
|
301
|
|
|
if (file_exists($class)) |
302
|
|
|
include $class; |
303
|
|
|
} |
304
|
|
|
} |