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