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\Module; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Framework\Containers\Modules\DataStructures\Module\Theme\Layout; |
19
|
|
|
use O2System\Spl\DataStructures\SplArrayObject; |
20
|
|
|
use O2System\Spl\Info\SplDirectoryInfo; |
21
|
|
|
use O2System\Spl\Info\SplFileInfo; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Theme |
25
|
|
|
* |
26
|
|
|
* @package O2System\Framework\Containers\Modules\DataStructures\Module |
27
|
|
|
*/ |
28
|
|
|
class Theme extends SplDirectoryInfo |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Theme Properties |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $properties = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Theme Presets |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $presets = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Theme Layout |
46
|
|
|
* |
47
|
|
|
* @var Theme\Layout |
48
|
|
|
*/ |
49
|
|
|
private $layout; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Theme::__construct |
53
|
|
|
* |
54
|
|
|
* @param string $dir |
55
|
|
|
*/ |
56
|
|
|
public function __construct($dir) |
57
|
|
|
{ |
58
|
|
|
parent::__construct($dir); |
59
|
|
|
|
60
|
|
|
// Set Theme Properties |
61
|
|
|
if (is_file($propFilePath = $dir . 'theme.json')) { |
62
|
|
|
$properties = json_decode(file_get_contents($propFilePath), true); |
63
|
|
|
|
64
|
|
|
if (json_last_error() === JSON_ERROR_NONE) { |
65
|
|
|
if (isset($properties[ 'config' ])) { |
66
|
|
|
$this->presets = $properties[ 'presets' ]; |
67
|
|
|
unset($properties[ 'presets' ]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->properties = $properties; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Set Default Theme Layout |
75
|
|
|
$this->setLayout('theme'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// ------------------------------------------------------------------------ |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Theme::isValid |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function isValid() |
86
|
|
|
{ |
87
|
|
|
if (count($this->properties)) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// ------------------------------------------------------------------------ |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Theme::getParameter |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getParameter() |
102
|
|
|
{ |
103
|
|
|
return $this->getDirName(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// ------------------------------------------------------------------------ |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Theme::getCode |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getCode() |
114
|
|
|
{ |
115
|
|
|
return strtoupper(substr(md5($this->getDirName()), 2, 7)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// ------------------------------------------------------------------------ |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Theme::getChecksum |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getChecksum() |
126
|
|
|
{ |
127
|
|
|
return md5($this->getMTime()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// ------------------------------------------------------------------------ |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Theme::getProperties |
134
|
|
|
* |
135
|
|
|
* @return \O2System\Spl\DataStructures\SplArrayObject |
136
|
|
|
*/ |
137
|
|
|
public function getProperties() |
138
|
|
|
{ |
139
|
|
|
return new SplArrayObject($this->properties); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// ------------------------------------------------------------------------ |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Theme::getPresets |
146
|
|
|
* |
147
|
|
|
* @return \O2System\Spl\DataStructures\SplArrayObject |
148
|
|
|
*/ |
149
|
|
|
public function getPresets() |
150
|
|
|
{ |
151
|
|
|
return new SplArrayObject($this->presets); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// ------------------------------------------------------------------------ |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Theme::getUrl |
158
|
|
|
* |
159
|
|
|
* @param string|null $path |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getUrl($path = null) |
164
|
|
|
{ |
165
|
|
|
return path_to_url($this->getRealPath() . $path); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// ------------------------------------------------------------------------ |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Theme::load |
172
|
|
|
* |
173
|
|
|
* @return static |
174
|
|
|
*/ |
175
|
|
|
public function load() |
176
|
|
|
{ |
177
|
|
|
if ($this->getPresets()->offsetExists('assets')) { |
178
|
|
|
presenter()->assets->autoload($this->getPresets()->offsetGet('assets')); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
presenter()->assets->loadCss('theme'); |
182
|
|
|
presenter()->assets->loadJs('theme'); |
183
|
|
|
|
184
|
|
|
// Autoload default theme layout |
185
|
|
|
$this->loadLayout(); |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// ------------------------------------------------------------------------ |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Theme::hasLayout |
194
|
|
|
* |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
|
|
public function hasLayout($layout) |
198
|
|
|
{ |
199
|
|
|
$extensions = ['.php', '.phtml', '.html', '.tpl']; |
200
|
|
|
|
201
|
|
|
if (isset($this->presets[ 'extensions' ])) { |
202
|
|
|
array_unshift($partialsExtensions, $this->presets[ 'extension' ]); |
203
|
|
|
} elseif (isset($this->presets[ 'extension' ])) { |
204
|
|
|
array_unshift($extensions, $this->presets[ 'extension' ]); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$found = false; |
208
|
|
|
foreach ($extensions as $extension) { |
209
|
|
|
$extension = trim($extension, '.'); |
210
|
|
|
|
211
|
|
|
if ($layout === 'theme') { |
212
|
|
|
$layoutFilePath = $this->getRealPath() . 'theme.' . $extension; |
213
|
|
|
} else { |
214
|
|
|
$layoutFilePath = $this->getRealPath() . 'layouts' . DIRECTORY_SEPARATOR . dash($layout) . DIRECTORY_SEPARATOR . 'layout.' . $extension; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if (is_file($layoutFilePath)) { |
218
|
|
|
$found = true; |
219
|
|
|
break; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return (bool)$found; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// ------------------------------------------------------------------------ |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Theme::setLayout |
230
|
|
|
* |
231
|
|
|
* @param string $layout |
232
|
|
|
* |
233
|
|
|
* @return static |
234
|
|
|
*/ |
235
|
|
|
public function setLayout($layout) |
236
|
|
|
{ |
237
|
|
|
$this->layout = $this->getLayout($layout); |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
// ------------------------------------------------------------------------ |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Theme::getLayout |
246
|
|
|
* |
247
|
|
|
* @param string $layout |
248
|
|
|
* |
249
|
|
|
* @return Theme\Layout |
250
|
|
|
*/ |
251
|
|
|
public function getLayout($layout = null) |
252
|
|
|
{ |
253
|
|
|
if (isset($layout)) { |
254
|
|
|
$extensions = ['.php', '.phtml', '.html', '.tpl']; |
255
|
|
|
|
256
|
|
|
if (isset($this->presets[ 'extensions' ])) { |
257
|
|
|
array_unshift($partialsExtensions, $this->presets[ 'extension' ]); |
258
|
|
|
} elseif (isset($this->presets[ 'extension' ])) { |
259
|
|
|
array_unshift($extensions, $this->presets[ 'extension' ]); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
foreach ($extensions as $extension) { |
263
|
|
|
$extension = trim($extension, '.'); |
264
|
|
|
|
265
|
|
|
if ($layout === 'theme') { |
266
|
|
|
$layoutFilePath = $this->getRealPath() . 'theme.' . $extension; |
267
|
|
|
} else { |
268
|
|
|
$layoutFilePath = $this->getRealPath() . 'layouts' . DIRECTORY_SEPARATOR . dash($layout) . DIRECTORY_SEPARATOR . 'layout.' . $extension; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
if (is_file($layoutFilePath)) { |
272
|
|
|
return new Theme\Layout($layoutFilePath); |
273
|
|
|
break; |
|
|
|
|
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return false; |
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $this->layout; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// ------------------------------------------------------------------------ |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Theme::loadLayout |
287
|
|
|
*/ |
288
|
|
|
protected function loadLayout() |
289
|
|
|
{ |
290
|
|
|
if ($this->layout instanceof Theme\Layout) { |
|
|
|
|
291
|
|
|
|
292
|
|
|
// load parent theme layout |
293
|
|
|
if($this->layout->getFilename() !== 'theme') { |
294
|
|
|
$themeLayout = $this->getLayout('theme'); |
295
|
|
|
|
296
|
|
|
// add theme layout public directory |
297
|
|
|
loader()->addPublicDir($themeLayout->getPath() . 'assets'); |
298
|
|
|
|
299
|
|
|
presenter()->assets->autoload( |
300
|
|
|
[ |
301
|
|
|
'css' => ['layout'], |
302
|
|
|
'js' => ['layout'], |
303
|
|
|
] |
304
|
|
|
); |
305
|
|
|
|
306
|
|
|
$partials = $themeLayout->getPartials()->getArrayCopy(); |
307
|
|
|
|
308
|
|
|
foreach ($partials as $offset => $partial) { |
309
|
|
|
if ($partial instanceof SplFileInfo) { |
310
|
|
|
presenter()->partials->addPartial($offset, $partial->getPathName()); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$this->loadChildLayout(); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
// ------------------------------------------------------------------------ |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Theme::loadChildLayout |
323
|
|
|
*/ |
324
|
|
|
protected function loadChildLayout() |
325
|
|
|
{ |
326
|
|
|
if ($this->layout instanceof Theme\Layout) { |
|
|
|
|
327
|
|
|
|
328
|
|
|
// add theme layout public directory |
329
|
|
|
loader()->addPublicDir($this->layout->getPath() . 'assets'); |
330
|
|
|
|
331
|
|
|
presenter()->assets->autoload( |
332
|
|
|
[ |
333
|
|
|
'css' => ['layout'], |
334
|
|
|
'js' => ['layout'], |
335
|
|
|
] |
336
|
|
|
); |
337
|
|
|
|
338
|
|
|
$partials = $this->layout->getPartials()->getArrayCopy(); |
339
|
|
|
|
340
|
|
|
foreach ($partials as $offset => $partial) { |
341
|
|
|
if ($partial instanceof SplFileInfo) { |
342
|
|
|
presenter()->partials->addPartial($offset, $partial->getPathName()); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
} |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.