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\Module; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Framework\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\Datastructures\Metadata |
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 SplFileInfo |
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
|
|
|
|
75
|
|
|
public function isValid() |
76
|
|
|
{ |
77
|
|
|
if (count($this->properties)) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getParameter() |
85
|
|
|
{ |
86
|
|
|
return $this->getDirName(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getCode() |
90
|
|
|
{ |
91
|
|
|
return strtoupper(substr(md5($this->getDirName()), 2, 7)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getChecksum() |
95
|
|
|
{ |
96
|
|
|
return md5($this->getMTime()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getProperties() |
100
|
|
|
{ |
101
|
|
|
return new SplArrayObject($this->properties); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getPresets() |
105
|
|
|
{ |
106
|
|
|
return new SplArrayObject($this->presets); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getUrl($path = null) |
110
|
|
|
{ |
111
|
|
|
return path_to_url($this->getRealPath() . $path); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getLayout($layout = null) |
115
|
|
|
{ |
116
|
|
|
if (isset($layout)) { |
117
|
|
|
if (false !== ($layout = $this->loadLayout($layout))) { |
118
|
|
|
return $layout; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($this->layout instanceof Layout) { |
123
|
|
|
return $this->layout; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function setLayout($layout) |
130
|
|
|
{ |
131
|
|
|
if (false !== ($layout = $this->loadLayout($layout))) { |
132
|
|
|
$this->layout = $layout; |
133
|
|
|
|
134
|
|
|
// add theme layout public directory |
135
|
|
|
loader()->addPublicDir($this->layout->getPath() . 'assets'); |
136
|
|
|
|
137
|
|
|
presenter()->assets->autoload( |
|
|
|
|
138
|
|
|
[ |
139
|
|
|
'css' => ['layout'], |
140
|
|
|
'js' => ['layout'], |
141
|
|
|
] |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$partials = $this->layout->getPartials()->getArrayCopy(); |
145
|
|
|
|
146
|
|
|
foreach ($partials as $offset => $partial) { |
147
|
|
|
presenter()->partials->addPartial($offset, $partial->getPathName()); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function loadLayout($layout) |
157
|
|
|
{ |
158
|
|
|
$extensions = ['.php', '.phtml', '.html', '.tpl']; |
159
|
|
|
|
160
|
|
|
if (isset($this->presets[ 'extensions' ])) { |
161
|
|
|
array_unshift($partialsExtensions, $this->presets[ 'extension' ]); |
162
|
|
|
} elseif (isset($this->presets[ 'extension' ])) { |
163
|
|
|
array_unshift($extensions, $this->presets[ 'extension' ]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
foreach ($extensions as $extension) { |
167
|
|
|
|
168
|
|
|
$extension = trim($extension, '.'); |
169
|
|
|
|
170
|
|
|
if ($layout === 'theme') { |
171
|
|
|
$layoutFilePath = $this->getRealPath() . 'theme.' . $extension; |
172
|
|
|
} else { |
173
|
|
|
$layoutFilePath = $this->getRealPath() . 'layouts' . DIRECTORY_SEPARATOR . dash($layout) . DIRECTORY_SEPARATOR . 'layout.' . $extension; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if (is_file($layoutFilePath)) { |
177
|
|
|
return new Layout($layoutFilePath); |
178
|
|
|
break; |
|
|
|
|
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
} |