|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Framework\Containers\Modules\DataStructures; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Spl\DataStructures\SplArrayObject; |
|
19
|
|
|
use O2System\Spl\Info\SplDirectoryInfo; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Module |
|
23
|
|
|
* |
|
24
|
|
|
* @package O2System\Framework\Containers\Modules\DataStructures |
|
25
|
|
|
*/ |
|
26
|
|
|
class Module extends SplDirectoryInfo |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Module::$type |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $type = 'MODULE'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Module::$namespace |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $namespace; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Module::$segments |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $segments; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Module::$parentSegments |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $parentSegments; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Module::$properties |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $properties = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Module::$presets |
|
65
|
|
|
* |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $presets = []; |
|
69
|
|
|
|
|
70
|
|
|
// ------------------------------------------------------------------------ |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Module::__construct |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $dir |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct($dir) |
|
78
|
|
|
{ |
|
79
|
|
|
parent::__construct($dir); |
|
80
|
|
|
|
|
81
|
|
|
$this->namespace = prepare_namespace(str_replace(PATH_ROOT, '', $dir), false); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// ------------------------------------------------------------------------ |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Module::getSegments |
|
88
|
|
|
* |
|
89
|
|
|
* @param bool $returnArray |
|
90
|
|
|
* |
|
91
|
|
|
* @return array|string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getSegments($returnArray = true) |
|
94
|
|
|
{ |
|
95
|
|
|
if ($returnArray) { |
|
96
|
|
|
return explode('/', $this->segments); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->segments; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// ------------------------------------------------------------------------ |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Module::setSegments |
|
106
|
|
|
* |
|
107
|
|
|
* @param array|string $segments |
|
108
|
|
|
* |
|
109
|
|
|
* @return static |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setSegments($segments) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->segments = is_array($segments) ? implode('/', $segments) : $segments; |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// ------------------------------------------------------------------------ |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Module::getParentSegments |
|
122
|
|
|
* |
|
123
|
|
|
* @param bool $returnArray |
|
124
|
|
|
* |
|
125
|
|
|
* @return array|string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getParentSegments($returnArray = true) |
|
128
|
|
|
{ |
|
129
|
|
|
if ($returnArray) { |
|
130
|
|
|
return explode('/', $this->parentSegments); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $this->parentSegments; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// ------------------------------------------------------------------------ |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Module::setParentSegments |
|
140
|
|
|
* |
|
141
|
|
|
* @param array|string $parentSegments |
|
142
|
|
|
* |
|
143
|
|
|
* @return static |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setParentSegments($parentSegments) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->parentSegments = is_array($parentSegments) ? implode('/', $parentSegments) : $parentSegments; |
|
148
|
|
|
|
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// ------------------------------------------------------------------------ |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Module::getCode |
|
156
|
|
|
* |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getCode() |
|
160
|
|
|
{ |
|
161
|
|
|
return strtoupper(substr(md5(spl_object_hash($this)), 2, 7)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// ------------------------------------------------------------------------ |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Module::getChecksum |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getChecksum() |
|
172
|
|
|
{ |
|
173
|
|
|
return md5($this->getMTime()); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
// ------------------------------------------------------------------------ |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Module::getProperties |
|
180
|
|
|
* |
|
181
|
|
|
* @return \O2System\Spl\DataStructures\SplArrayObject |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getProperties() |
|
184
|
|
|
{ |
|
185
|
|
|
return new SplArrayObject($this->properties); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// ------------------------------------------------------------------------ |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Module::setProperties |
|
192
|
|
|
* |
|
193
|
|
|
* @param array $properties |
|
194
|
|
|
* |
|
195
|
|
|
* @return static |
|
196
|
|
|
*/ |
|
197
|
|
|
public function setProperties(array $properties) |
|
198
|
|
|
{ |
|
199
|
|
|
if (isset($properties[ 'presets' ])) { |
|
200
|
|
|
$this->setPresets($properties[ 'presets' ]); |
|
201
|
|
|
|
|
202
|
|
|
unset($properties[ 'presets' ]); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$this->properties = $properties; |
|
206
|
|
|
|
|
207
|
|
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// ------------------------------------------------------------------------ |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Module::getPresets |
|
214
|
|
|
* |
|
215
|
|
|
* @return \O2System\Spl\DataStructures\SplArrayObject |
|
216
|
|
|
*/ |
|
217
|
|
|
public function getPresets() |
|
218
|
|
|
{ |
|
219
|
|
|
return new SplArrayObject($this->presets); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
// ------------------------------------------------------------------------ |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Module::setPresets |
|
226
|
|
|
* |
|
227
|
|
|
* @param array $presets |
|
228
|
|
|
* |
|
229
|
|
|
* @return static |
|
230
|
|
|
*/ |
|
231
|
|
|
public function setPresets(array $presets) |
|
232
|
|
|
{ |
|
233
|
|
|
$this->presets = $presets; |
|
234
|
|
|
|
|
235
|
|
|
return $this; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
// ------------------------------------------------------------------------ |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Module::getDefaultControllerClassName |
|
242
|
|
|
* |
|
243
|
|
|
* @return string |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getDefaultControllerClassName() |
|
246
|
|
|
{ |
|
247
|
|
|
return $this->getNamespace() . 'Controllers\\' . $this->getDirName(); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
// ------------------------------------------------------------------------ |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Module::getNamespace |
|
254
|
|
|
* |
|
255
|
|
|
* @return string |
|
256
|
|
|
*/ |
|
257
|
|
|
public function getNamespace() |
|
258
|
|
|
{ |
|
259
|
|
|
return $this->namespace; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
// ------------------------------------------------------------------------ |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Module::setNamespace |
|
266
|
|
|
* |
|
267
|
|
|
* @param string $namespace |
|
268
|
|
|
* |
|
269
|
|
|
* @return static |
|
270
|
|
|
*/ |
|
271
|
|
|
public function setNamespace($namespace) |
|
272
|
|
|
{ |
|
273
|
|
|
$this->namespace = trim($namespace, '\\') . '\\'; |
|
274
|
|
|
|
|
275
|
|
|
return $this; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
// ------------------------------------------------------------------------ |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Module::getThemes |
|
282
|
|
|
* |
|
283
|
|
|
* @return array |
|
284
|
|
|
*/ |
|
285
|
|
|
public function getThemes() |
|
286
|
|
|
{ |
|
287
|
|
|
$directory = new SplDirectoryInfo($this->getResourcesDir() . 'themes' . DIRECTORY_SEPARATOR); |
|
288
|
|
|
|
|
289
|
|
|
$themes = []; |
|
290
|
|
|
foreach ($directory->getTree() as $themeName => $themeTree) { |
|
291
|
|
|
if (($theme = $this->getTheme($themeName)) instanceof Module\Theme) { |
|
292
|
|
|
$themes[ $themeName ] = $theme; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
return $themes; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
// ------------------------------------------------------------------------ |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Module::getResourcesDir |
|
303
|
|
|
* |
|
304
|
|
|
* @return string |
|
305
|
|
|
*/ |
|
306
|
|
|
public function getResourcesDir() |
|
307
|
|
|
{ |
|
308
|
|
|
if ($this->getParameter() === DIR_APP) { |
|
|
|
|
|
|
309
|
|
|
return PATH_RESOURCES; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
return PATH_RESOURCES . $this->getParameter() . DIRECTORY_SEPARATOR; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
// ------------------------------------------------------------------------ |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Module::getTheme |
|
319
|
|
|
* |
|
320
|
|
|
* @param string $theme |
|
321
|
|
|
* @param bool $failover |
|
322
|
|
|
* |
|
323
|
|
|
* @return bool|Module\Theme |
|
324
|
|
|
*/ |
|
325
|
|
|
public function getTheme($theme, $failover = true) |
|
326
|
|
|
{ |
|
327
|
|
|
$theme = dash($theme); |
|
328
|
|
|
|
|
329
|
|
|
if ($failover === false) { |
|
330
|
|
|
if (is_dir($themePath = $this->getResourcesDir() . 'themes' . DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR)) { |
|
331
|
|
|
$themeObject = new Module\Theme($themePath); |
|
332
|
|
|
|
|
333
|
|
|
if ($themeObject->isValid()) { |
|
334
|
|
|
return $themeObject; |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
} else { |
|
338
|
|
|
foreach (modules() as $module) { |
|
339
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
|
340
|
|
|
continue; |
|
341
|
|
|
} elseif ($themeObject = $module->getTheme($theme, false)) { |
|
342
|
|
|
return $themeObject; |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
return false; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
// ------------------------------------------------------------------------ |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Module::getPublicDir |
|
354
|
|
|
* |
|
355
|
|
|
* @return string |
|
356
|
|
|
*/ |
|
357
|
|
|
public function getPublicDir() |
|
358
|
|
|
{ |
|
359
|
|
|
return PATH_PUBLIC . strtolower(str_replace(PATH_APP, '', $this->getRealPath())); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
// ------------------------------------------------------------------------ |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Module::getType |
|
366
|
|
|
* |
|
367
|
|
|
* @return string |
|
368
|
|
|
*/ |
|
369
|
|
|
public function getType() |
|
370
|
|
|
{ |
|
371
|
|
|
return $this->type; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
// ------------------------------------------------------------------------ |
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* Module::setType |
|
378
|
|
|
* |
|
379
|
|
|
* @param string $type |
|
380
|
|
|
* |
|
381
|
|
|
* @return static |
|
382
|
|
|
*/ |
|
383
|
|
|
public function setType($type) |
|
384
|
|
|
{ |
|
385
|
|
|
$this->type = strtoupper($type); |
|
386
|
|
|
|
|
387
|
|
|
return $this; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
// ------------------------------------------------------------------------ |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* Module::getDir |
|
394
|
|
|
* |
|
395
|
|
|
* @param string $dirName |
|
396
|
|
|
* @param bool $psrDir |
|
397
|
|
|
* |
|
398
|
|
|
* @return bool|string |
|
399
|
|
|
*/ |
|
400
|
|
|
public function getDir($dirName, $psrDir = false) |
|
401
|
|
|
{ |
|
402
|
|
|
$dirName = $psrDir === true ? prepare_class_name($dirName) : $dirName; |
|
403
|
|
|
$dirName = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $dirName); |
|
404
|
|
|
|
|
405
|
|
|
if (is_dir($dirPath = $this->getRealPath() . $dirName)) { |
|
406
|
|
|
return $dirPath . DIRECTORY_SEPARATOR; |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
return false; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
// ------------------------------------------------------------------------ |
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* Module::hasTheme |
|
416
|
|
|
* |
|
417
|
|
|
* @param string $theme |
|
418
|
|
|
* |
|
419
|
|
|
* @return bool |
|
420
|
|
|
*/ |
|
421
|
|
|
public function hasTheme($theme) |
|
422
|
|
|
{ |
|
423
|
|
|
if (is_dir($this->getThemesPath() . $theme)) { |
|
424
|
|
|
return true; |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
return false; |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
// ------------------------------------------------------------------------ |
|
431
|
|
|
|
|
432
|
|
|
/** |
|
433
|
|
|
* Module::getThemesPath |
|
434
|
|
|
* |
|
435
|
|
|
* @return string |
|
436
|
|
|
*/ |
|
437
|
|
|
public function getThemesPath() |
|
438
|
|
|
{ |
|
439
|
|
|
if ($this->getParameter() === DIR_APP) { |
|
|
|
|
|
|
440
|
|
|
return PATH_RESOURCES . 'themes' . DIRECTORY_SEPARATOR; |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
return PATH_RESOURCES . $this->getParameter() . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR; |
|
444
|
|
|
|
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
|
|
// ------------------------------------------------------------------------ |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* Module::getParameter |
|
451
|
|
|
* |
|
452
|
|
|
* @return string |
|
453
|
|
|
*/ |
|
454
|
|
|
public function getParameter() |
|
455
|
|
|
{ |
|
456
|
|
|
return snakecase($this->getDirName()); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
// ------------------------------------------------------------------------ |
|
460
|
|
|
|
|
461
|
|
|
/** |
|
462
|
|
|
* Module::loadModel |
|
463
|
|
|
*/ |
|
464
|
|
|
public function loadModel() |
|
465
|
|
|
{ |
|
466
|
|
|
$modelClassName = $this->namespace . 'Models\Base'; |
|
467
|
|
|
|
|
468
|
|
|
if (class_exists($modelClassName)) { |
|
469
|
|
|
models()->load($modelClassName, strtolower($this->type)); |
|
|
|
|
|
|
470
|
|
|
} |
|
471
|
|
|
} |
|
472
|
|
|
} |