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.css'); |
181
|
|
|
presenter()->assets->loadJs('theme.js'); |
182
|
|
|
|
183
|
|
|
// Autoload default theme layout |
184
|
|
|
$this->loadLayout(); |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// ------------------------------------------------------------------------ |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Theme::setLayout |
193
|
|
|
* |
194
|
|
|
* @param string $layout |
195
|
|
|
* |
196
|
|
|
* @return static |
197
|
|
|
*/ |
198
|
|
|
public function setLayout($layout) |
199
|
|
|
{ |
200
|
|
|
$extensions = ['.php', '.phtml', '.html', '.tpl']; |
201
|
|
|
|
202
|
|
|
if (isset($this->presets[ 'extensions' ])) { |
203
|
|
|
array_unshift($partialsExtensions, $this->presets[ 'extension' ]); |
204
|
|
|
} elseif (isset($this->presets[ 'extension' ])) { |
205
|
|
|
array_unshift($extensions, $this->presets[ 'extension' ]); |
206
|
|
|
} |
207
|
|
|
|
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
|
|
|
$this->layout = new Theme\Layout($layoutFilePath); |
219
|
|
|
break; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// ------------------------------------------------------------------------ |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Theme::getLayout |
230
|
|
|
* |
231
|
|
|
* @return Theme\Layout |
232
|
|
|
*/ |
233
|
|
|
public function getLayout() |
234
|
|
|
{ |
235
|
|
|
return $this->layout; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
// ------------------------------------------------------------------------ |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Theme::loadLayout |
242
|
|
|
* |
243
|
|
|
* @return static |
244
|
|
|
*/ |
245
|
|
|
protected function loadLayout() |
246
|
|
|
{ |
247
|
|
|
if ($this->layout instanceof Theme\Layout) { |
|
|
|
|
248
|
|
|
// add theme layout public directory |
249
|
|
|
loader()->addPublicDir($this->layout->getPath() . 'assets'); |
250
|
|
|
|
251
|
|
|
presenter()->assets->autoload( |
252
|
|
|
[ |
253
|
|
|
'css' => ['layout'], |
254
|
|
|
'js' => ['layout'], |
255
|
|
|
] |
256
|
|
|
); |
257
|
|
|
|
258
|
|
|
$partials = $this->layout->getPartials()->getArrayCopy(); |
259
|
|
|
|
260
|
|
|
foreach ($partials as $offset => $partial) { |
261
|
|
|
if ($partial instanceof SplFileInfo) { |
262
|
|
|
presenter()->partials->addPartial($offset, $partial->getPathName()); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $this; |
268
|
|
|
} |
269
|
|
|
} |