|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Flextype (http://flextype.org) |
|
7
|
|
|
* Founded by Sergey Romanenko and maintained by Flextype Community. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Flextype; |
|
11
|
|
|
|
|
12
|
|
|
use Flextype\Component\Filesystem\Filesystem; |
|
|
|
|
|
|
13
|
|
|
use RuntimeException; |
|
14
|
|
|
use function array_merge; |
|
15
|
|
|
use function array_replace_recursive; |
|
16
|
|
|
use function count; |
|
17
|
|
|
use function filemtime; |
|
18
|
|
|
use function is_array; |
|
19
|
|
|
use function md5; |
|
20
|
|
|
|
|
21
|
|
|
class Themes |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Flextype Dependency Container |
|
25
|
|
|
*/ |
|
26
|
|
|
private $flextype; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Private construct method to enforce singleton behavior. |
|
30
|
|
|
* |
|
31
|
|
|
* @access private |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($flextype) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->flextype = $flextype; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Init themes |
|
40
|
|
|
*/ |
|
41
|
|
|
public function init($flextype, $app) : void |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
// Set empty themes list item |
|
44
|
|
|
$this->flextype['registry']->set('themes', []); |
|
45
|
|
|
|
|
46
|
|
|
// Get themes list |
|
47
|
|
|
$themes_list = $this->getThemes(); |
|
48
|
|
|
|
|
49
|
|
|
// If Themes List isnt empty then continue |
|
50
|
|
|
if (! is_array($themes_list) || count($themes_list) <= 0) { |
|
|
|
|
|
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Get themes cache ID |
|
55
|
|
|
$themes_cache_id = $this->getThemesCacheID($themes_list); |
|
56
|
|
|
|
|
57
|
|
|
// Get themes list from cache or scan themes folder and create new themes cache item in the registry |
|
58
|
|
|
if ($this->flextype['cache']->contains($themes_cache_id)) { |
|
59
|
|
|
$this->flextype['registry']->set('themes', $this->flextype['cache']->fetch($themes_cache_id)); |
|
60
|
|
|
} else { |
|
61
|
|
|
$themes = []; |
|
62
|
|
|
$themes_settings = []; |
|
|
|
|
|
|
63
|
|
|
$themes_manifest = []; |
|
|
|
|
|
|
64
|
|
|
$default_theme_settings = []; |
|
65
|
|
|
$site_theme_settings = []; |
|
|
|
|
|
|
66
|
|
|
$default_theme_manifest = []; |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
// Go through the themes list... |
|
69
|
|
|
foreach ($themes_list as $theme) { |
|
70
|
|
|
|
|
71
|
|
|
// Set custom theme directory |
|
72
|
|
|
$custom_theme_settings_dir = PATH['site'] . '/config/themes/' . $theme['dirname']; |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
// Set default theme settings and manifest files |
|
75
|
|
|
$default_theme_settings_file = PATH['site'] . '/themes/' . $theme['dirname'] . '/settings.yaml'; |
|
76
|
|
|
$default_theme_manifest_file = PATH['site'] . '/themes/' . $theme['dirname'] . '/theme.yaml'; |
|
77
|
|
|
|
|
78
|
|
|
// Set custom theme settings and manifest files |
|
79
|
|
|
$custom_theme_settings_file = PATH['site'] . '/config/themes/' . $theme['dirname'] . '/settings.yaml'; |
|
80
|
|
|
|
|
81
|
|
|
// Create custom theme settings directory |
|
82
|
|
|
! Filesystem::has($custom_theme_settings_dir) and Filesystem::createDir($custom_theme_settings_dir); |
|
83
|
|
|
|
|
84
|
|
|
// Check if default theme settings file exists |
|
85
|
|
|
if (! Filesystem::has($default_theme_settings_file)) { |
|
86
|
|
|
throw new RuntimeException('Load ' . $theme['dirname'] . ' theme settings - failed!'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Get default theme manifest content |
|
90
|
|
|
$default_theme_settings_file_content = Filesystem::read($default_theme_settings_file); |
|
91
|
|
|
if (trim($default_theme_settings_file_content) === '') { |
|
92
|
|
|
$default_theme_settings = []; |
|
93
|
|
|
} else { |
|
94
|
|
|
$default_theme_settings = $this->flextype['parser']->decode($default_theme_settings_file_content, 'yaml'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Create custom theme settings file |
|
98
|
|
|
! Filesystem::has($custom_theme_settings_file) and Filesystem::write($custom_theme_settings_file, $default_theme_settings_file_content); |
|
99
|
|
|
|
|
100
|
|
|
// Get custom theme settings content |
|
101
|
|
|
$custom_theme_settings_file_content = Filesystem::read($custom_theme_settings_file); |
|
102
|
|
|
if (trim($custom_theme_settings_file_content) === '') { |
|
103
|
|
|
$custom_theme_settings = []; |
|
104
|
|
|
} else { |
|
105
|
|
|
$custom_theme_settings = $this->flextype['parser']->decode($custom_theme_settings_file_content, 'yaml'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Check if default theme manifest file exists |
|
109
|
|
|
if (! Filesystem::has($default_theme_manifest_file)) { |
|
110
|
|
|
RuntimeException('Load ' . $theme['dirname'] . ' theme manifest - failed!'); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Get default theme manifest content |
|
114
|
|
|
$default_theme_manifest_file_content = Filesystem::read($default_theme_manifest_file); |
|
115
|
|
|
$default_theme_manifest = $this->flextype['parser']->decode($default_theme_manifest_file_content, 'yaml'); |
|
116
|
|
|
|
|
117
|
|
|
// Merge theme settings and manifest data |
|
118
|
|
|
$themes[$theme['dirname']]['manifest'] = $default_theme_manifest; |
|
119
|
|
|
$themes[$theme['dirname']]['settings'] = array_replace_recursive($default_theme_settings, $custom_theme_settings); |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// Save parsed themes list in the registry themes |
|
124
|
|
|
$this->flextype['registry']->set('themes', $themes); |
|
125
|
|
|
|
|
126
|
|
|
// Save parsed themes list in the cache |
|
127
|
|
|
$this->flextype['cache']->save($themes_cache_id, $themes); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// Emit onThemesInitialized |
|
131
|
|
|
$this->flextype['emitter']->emit('onThemesInitialized'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get Themes Cache ID |
|
136
|
|
|
* |
|
137
|
|
|
* @param array $themes_list Themes list |
|
138
|
|
|
* |
|
139
|
|
|
* @access protected |
|
140
|
|
|
*/ |
|
141
|
|
|
private function getThemesCacheID(array $themes_list) : string |
|
142
|
|
|
{ |
|
143
|
|
|
// Themes Cache ID |
|
144
|
|
|
$_themes_cache_id = ''; |
|
145
|
|
|
|
|
146
|
|
|
// Go through... |
|
147
|
|
|
if (is_array($themes_list) && count($themes_list) > 0) { |
|
148
|
|
|
foreach ($themes_list as $theme) { |
|
149
|
|
|
$default_theme_settings_file = PATH['site'] . '/themes/' . $theme['dirname'] . '/settings.yaml'; |
|
|
|
|
|
|
150
|
|
|
$default_theme_manifest_file = PATH['site'] . '/themes/' . $theme['dirname'] . '/theme.yaml'; |
|
151
|
|
|
$site_theme_settings_file = PATH['site'] . '/config/themes/' . $theme['dirname'] . '/settings.yaml'; |
|
152
|
|
|
|
|
153
|
|
|
$f1 = Filesystem::has($default_theme_settings_file) ? filemtime($default_theme_settings_file) : ''; |
|
154
|
|
|
$f2 = Filesystem::has($default_theme_manifest_file) ? filemtime($default_theme_manifest_file) : ''; |
|
155
|
|
|
$f3 = Filesystem::has($site_theme_settings_file) ? filemtime($site_theme_settings_file) : ''; |
|
156
|
|
|
|
|
157
|
|
|
$_themes_cache_id .= $f1 . $f2 . $f3; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
// Create Unique Cache ID for Themes |
|
162
|
|
|
$themes_cache_id = md5('themes' . PATH['site'] . '/themes/' . $_themes_cache_id); |
|
163
|
|
|
|
|
164
|
|
|
// Return themes cache id |
|
165
|
|
|
return $themes_cache_id; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Get list of themes |
|
170
|
|
|
* |
|
171
|
|
|
* @return array |
|
172
|
|
|
* |
|
173
|
|
|
* @access public |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getThemes() : array |
|
176
|
|
|
{ |
|
177
|
|
|
// Init themes list |
|
178
|
|
|
$themes_list = []; |
|
179
|
|
|
|
|
180
|
|
|
// Get themes list |
|
181
|
|
|
$_themes_list = Filesystem::listContents(PATH['site'] . '/themes'); |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
// Go through founded themes |
|
184
|
|
|
foreach ($_themes_list as $theme) { |
|
185
|
|
|
if ($theme['type'] !== 'dir' || ! Filesystem::has($theme['path'] . '/' . 'theme.yaml')) { |
|
186
|
|
|
continue; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$themes_list[] = $theme; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $themes_list; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Get partials for theme |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $theme Theme id |
|
199
|
|
|
* |
|
200
|
|
|
* @return array |
|
201
|
|
|
* |
|
202
|
|
|
* @access public |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getPartials(string $theme) : array |
|
205
|
|
|
{ |
|
206
|
|
|
// Init partials list |
|
207
|
|
|
$partials_list = []; |
|
208
|
|
|
|
|
209
|
|
|
// Get partials files |
|
210
|
|
|
$_partials_list = Filesystem::listContents(PATH['site'] . '/themes/' . $theme . '/templates/partials/'); |
|
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
// If there is any partials file then go... |
|
213
|
|
|
if (count($_partials_list) > 0) { |
|
214
|
|
|
foreach ($_partials_list as $partial) { |
|
215
|
|
|
if ($partial['type'] !== 'file' || $partial['extension'] !== 'html') { |
|
216
|
|
|
continue; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$partials_list[] = $partial; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
// return partials |
|
224
|
|
|
return $partials_list; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Get templates for theme |
|
229
|
|
|
* |
|
230
|
|
|
* @param string $theme Theme id |
|
231
|
|
|
* |
|
232
|
|
|
* @return array |
|
233
|
|
|
* |
|
234
|
|
|
* @access public |
|
235
|
|
|
*/ |
|
236
|
|
|
public function getTemplates(string $theme) : array |
|
237
|
|
|
{ |
|
238
|
|
|
// Init templates list |
|
239
|
|
|
$templates_list = []; |
|
240
|
|
|
|
|
241
|
|
|
// Get templates files |
|
242
|
|
|
$_templates_list = Filesystem::listContents(PATH['site'] . '/themes/' . $theme . '/templates/'); |
|
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
// If there is any template file then go... |
|
245
|
|
|
if (count($_templates_list) > 0) { |
|
246
|
|
|
foreach ($_templates_list as $template) { |
|
247
|
|
|
if ($template['type'] !== 'file' || $template['extension'] !== 'html') { |
|
248
|
|
|
continue; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
$templates_list[] = $template; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
// return templates |
|
256
|
|
|
return $templates_list; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths