1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP 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\Datastructures; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Framework\Datastructures\Module\Theme; |
19
|
|
|
use O2System\Spl\Datastructures\SplArrayObject; |
20
|
|
|
use O2System\Spl\Info\SplDirectoryInfo; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Module |
24
|
|
|
* |
25
|
|
|
* @package O2System\Framework\Datastructures |
26
|
|
|
*/ |
27
|
|
|
class Module extends SplDirectoryInfo |
28
|
|
|
{ |
29
|
|
|
private $type = 'MODULE'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Module Namespace |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $namespace; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Module Segments |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $segments; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Module Parent Segments |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $parentSegments; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Module Properties |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $properties = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Module Config |
61
|
|
|
* |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
private $config = []; |
65
|
|
|
|
66
|
|
|
public function __construct($dir) |
67
|
|
|
{ |
68
|
|
|
parent::__construct($dir); |
69
|
|
|
|
70
|
|
|
$this->namespace = prepare_namespace(str_replace(PATH_ROOT, '', $dir), false); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getSegments($returnArray = true) |
74
|
|
|
{ |
75
|
|
|
if ($returnArray) { |
76
|
|
|
return explode('/', $this->segments); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->segments; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function setSegments($segments) |
83
|
|
|
{ |
84
|
|
|
$this->segments = is_array($segments) ? implode('/', $segments) : $segments; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getParentSegments($returnArray = true) |
90
|
|
|
{ |
91
|
|
|
if ($returnArray) { |
92
|
|
|
return explode('/', $this->parentSegments); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->parentSegments; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setParentSegments($parentSegments) |
99
|
|
|
{ |
100
|
|
|
$this->parentSegments = is_array($parentSegments) ? implode('/', $parentSegments) : $parentSegments; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getParameter() |
106
|
|
|
{ |
107
|
|
|
return snakecase($this->getDirName()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getCode() |
111
|
|
|
{ |
112
|
|
|
return strtoupper(substr(md5($this->getDirName()), 2, 7)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getChecksum() |
116
|
|
|
{ |
117
|
|
|
return md5($this->getMTime()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getProperties() |
121
|
|
|
{ |
122
|
|
|
return new SplArrayObject($this->properties); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function setProperties(array $properties) |
126
|
|
|
{ |
127
|
|
|
$this->properties = $properties; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getConfig() |
133
|
|
|
{ |
134
|
|
|
return new SplArrayObject($this->config); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function setConfig(array $config) |
138
|
|
|
{ |
139
|
|
|
$this->config = $config; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function getNamespace() |
145
|
|
|
{ |
146
|
|
|
return $this->namespace; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function setNamespace($namespace) |
150
|
|
|
{ |
151
|
|
|
$this->namespace = trim($namespace, '\\') . '\\'; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getDefaultControllerClassName() |
157
|
|
|
{ |
158
|
|
|
return $this->getNamespace() . 'Controllers\\' . $this->getDirName(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getThemes() |
162
|
|
|
{ |
163
|
|
|
$directory = new SplDirectoryInfo($this->getResourcesDir() . 'themes' . DIRECTORY_SEPARATOR); |
164
|
|
|
|
165
|
|
|
$themes = []; |
166
|
|
|
foreach ($directory->getTree() as $themeName => $themeTree) { |
167
|
|
|
if (($theme = $this->getTheme($themeName)) instanceof Theme) { |
168
|
|
|
$themes[ $themeName ] = $theme; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $themes; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getPublicDir() |
176
|
|
|
{ |
177
|
|
|
return PATH_PUBLIC . strtolower(str_replace(PATH_APP, '', $this->getRealPath())); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getResourcesDir() |
181
|
|
|
{ |
182
|
|
|
return PATH_RESOURCES . strtolower(str_replace(PATH_APP, '', $this->getRealPath())); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function getTheme($theme, $failover = true) |
186
|
|
|
{ |
187
|
|
|
$theme = dash($theme); |
188
|
|
|
|
189
|
|
|
if ($failover === false) { |
190
|
|
|
if (is_dir($themePath = $this->getResourcesDir() . 'themes' . DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR)) { |
191
|
|
|
$themeObject = new Theme($themePath); |
192
|
|
|
|
193
|
|
|
if ($themeObject->isValid()) { |
194
|
|
|
return $themeObject; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} else { |
198
|
|
|
foreach (modules() as $module) { |
199
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
200
|
|
|
continue; |
201
|
|
|
} elseif ($themeObject = $module->getTheme($theme, false)) { |
202
|
|
|
return $themeObject; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getType() |
211
|
|
|
{ |
212
|
|
|
return $this->type; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function setType($type) |
216
|
|
|
{ |
217
|
|
|
$this->type = strtoupper($type); |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function getDir($dirName, $psrDir = false) |
223
|
|
|
{ |
224
|
|
|
$dirName = $psrDir === true ? prepare_class_name($dirName) : $dirName; |
225
|
|
|
$dirName = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $dirName); |
226
|
|
|
|
227
|
|
|
if (is_dir($dirPath = $this->getRealPath() . $dirName)) { |
228
|
|
|
return $dirPath . DIRECTORY_SEPARATOR; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return false; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function hasTheme($theme) |
235
|
|
|
{ |
236
|
|
|
if (is_dir($this->getThemesPath() . $theme)) { |
237
|
|
|
return true; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function getThemesPath() |
244
|
|
|
{ |
245
|
|
|
return PATH_RESOURCES . DIRECTORY_SEPARATOR . $this->getParameter() . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function loadModel() |
249
|
|
|
{ |
250
|
|
|
$modelClassName = $this->namespace . 'Base\\Model'; |
251
|
|
|
|
252
|
|
|
if (class_exists($modelClassName)) { |
253
|
|
|
models()->load($modelClassName, strtolower($this->type)); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |