1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Koch Framework |
5
|
|
|
* Jens-André Koch © 2005 - onwards. |
6
|
|
|
* |
7
|
|
|
* This file is part of "Koch Framework". |
8
|
|
|
* |
9
|
|
|
* License: GNU/GPL v2 or any later version, see LICENSE file. |
10
|
|
|
* |
11
|
|
|
* This program is free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Koch\View\Helper; |
26
|
|
|
|
27
|
|
|
use Koch\View\AbstractRenderer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Koch Framework - Class for handling of Themes. |
31
|
|
|
* |
32
|
|
|
* This class provides abstracted (object) access to a theme's theme_info.xml file. |
33
|
|
|
*/ |
34
|
|
|
class Theme |
35
|
|
|
{ |
36
|
|
|
public $theme = ''; |
37
|
|
|
public $theme_info = []; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor, or what ;). |
41
|
|
|
* |
42
|
|
|
* @param string $theme Name of the Theme. |
43
|
|
|
* |
44
|
|
|
* @return \Koch\View\Helper\Theme |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
public function __construct($theme) |
47
|
|
|
{ |
48
|
|
|
$this->setThemeName($theme); |
49
|
|
|
|
50
|
|
|
$this->getInfoArray($theme); |
51
|
|
|
|
52
|
|
|
return $this; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $theme |
57
|
|
|
*/ |
58
|
|
|
public function setThemeName($theme) |
59
|
|
|
{ |
60
|
|
|
if ($theme === null) { |
61
|
|
|
throw new \InvalidArgumentException('Please provide a theme name.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->theme = $theme; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Getter for the "theme_info.xml" file of the currently activated theme. |
69
|
|
|
* |
70
|
|
|
* @return string Filepath to "theme_info.xml" of the currently activated theme. |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function getCurrentThemeInfoFile() |
73
|
|
|
{ |
74
|
|
|
// get array for frontend or backend theme |
75
|
|
|
$paths = AbstractRenderer::getThemeTemplatePaths(); |
|
|
|
|
76
|
|
|
|
77
|
|
|
foreach ($paths as $path) { |
78
|
|
|
$file = $path . DIRECTORY_SEPARATOR . 'theme_info.xml'; |
79
|
|
|
|
80
|
|
|
if (is_file($file)) { |
81
|
|
|
return $file; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Looks for the requested theme in the frontend and backend theme folder |
88
|
|
|
* and returns the theme path. |
89
|
|
|
* |
90
|
|
|
* @param string $theme Theme name. |
|
|
|
|
91
|
|
|
* |
92
|
|
|
* @return string Path to theme. |
|
|
|
|
93
|
|
|
*/ |
94
|
|
|
public function getPath($theme = null) |
95
|
|
|
{ |
96
|
|
|
if ($theme === null) { |
97
|
|
|
$theme = $this->getName(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (is_dir(APPLICATION_PATH . 'Themes') === false) { |
101
|
|
|
throw new \RuntimeException('The application themes folder was not found.'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$frontend = APPLICATION_PATH . 'Themes/frontend/' . $theme . DIRECTORY_SEPARATOR; |
105
|
|
|
if (is_dir($frontend)) { |
106
|
|
|
return $frontend; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$backend = APPLICATION_PATH . 'Themes/backend/' . $theme . DIRECTORY_SEPARATOR; |
110
|
|
|
if (is_dir($backend)) { |
111
|
|
|
return $backend; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Looks for the requested theme in the frontend and backend theme folder |
119
|
|
|
* and returns the web path of the theme. |
120
|
|
|
* |
121
|
|
|
* @param string $theme Theme name. |
|
|
|
|
122
|
|
|
* |
123
|
|
|
* @return string Webpath of theme (for usage in templates). |
|
|
|
|
124
|
|
|
*/ |
125
|
|
|
public function getWebPath($theme = null) |
126
|
|
|
{ |
127
|
|
|
if ($theme === null) { |
128
|
|
|
$theme = $this->getName(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// check absolute, return www |
132
|
|
|
if (is_dir(APPLICATION_PATH . 'Themes/frontend/' . $theme)) { |
133
|
|
|
return WWW_ROOT_THEMES_FRONTEND . $theme . '/'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// check absolute, return www |
137
|
|
|
if (is_dir(APPLICATION_PATH . 'Themes/backend/' . $theme)) { |
138
|
|
|
return WWW_ROOT_THEMES_BACKEND . $theme . '/'; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns "theme_info.xml" for the requested theme. |
144
|
|
|
* |
145
|
|
|
* @param string $theme Theme name. |
146
|
|
|
* |
147
|
|
|
* @return string File path to "theme_info.xml" file. |
148
|
|
|
* |
149
|
|
|
* @throws \Koch\Exception\Exception |
150
|
|
|
*/ |
151
|
|
|
public function getThemeInfoFile($theme) |
152
|
|
|
{ |
153
|
|
|
$file = $this->getPath($theme) . 'theme_info.xml'; |
154
|
|
|
|
155
|
|
|
if (is_file($file)) { |
156
|
|
|
return $file; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
throw new \Exception('The Theme "' . $theme . '" has no "theme_info.xml" file.'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns Theme Infos as array. |
164
|
|
|
* |
165
|
|
|
* @param string $theme Name of the Theme. |
|
|
|
|
166
|
|
|
* |
167
|
|
|
* @return array Theme_Info.xml as Array. |
168
|
|
|
*/ |
169
|
|
|
public function getInfoArray($theme = null) |
170
|
|
|
{ |
171
|
|
|
$file = $this->getThemeInfoFile($theme); |
172
|
|
|
|
173
|
|
|
$array = \Koch\Config\Adapter\XML::read($file); |
174
|
|
|
|
175
|
|
|
// when setting array as object property remove the inner theme array |
176
|
|
|
$this->theme_info = $array['theme']; |
177
|
|
|
|
178
|
|
|
return $this->theme_info; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* -------------------------------------------------------------------------------------------- |
183
|
|
|
* GETTERS |
184
|
|
|
* --------------------------------------------------------------------------------------------. |
185
|
|
|
*/ |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Gets shortname or folder name. |
189
|
|
|
* |
190
|
|
|
* @return string short name / folder name. |
191
|
|
|
*/ |
192
|
|
|
public function getName() |
193
|
|
|
{ |
194
|
|
|
return $this->theme; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getFullName() |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
return $this->theme_info['name']; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function getAuthor() |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
return $this->theme_info['authors']; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function getVersion() |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
return $this->theme_info['theme_version']; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function getRequiredVersion() |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
return $this->theme_info['required_version']; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function getDate() |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
return $this->theme_info['date']; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function getLayout() |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
return $this->theme_info['layout']; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function getCss() |
|
|
|
|
228
|
|
|
{ |
229
|
|
|
return $this->theme_info['css']; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function getLayoutFile() |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
if ($this->theme_info['layout']['mainfile'] !== null) { |
235
|
|
|
#return $this->getPath() . $this->theme_info['layout']['mainfile']; |
|
|
|
|
236
|
|
|
|
237
|
|
|
return $this->theme_info['layout']['mainfile']; |
238
|
|
|
} elseif (false === isset($this->theme_info['layout']['mainfile'])) { |
239
|
|
|
// maybe we have a main template css file named after the theme |
240
|
|
|
// $layout_file = $this->getPath() . $this->getName() . '.tpl'; |
|
|
|
|
241
|
|
|
$layout_file = $this->getName() . '.tpl'; |
|
|
|
|
242
|
|
|
|
243
|
|
|
if (is_file($layout_file)) { |
|
|
|
|
244
|
|
|
return $layout_file; |
|
|
|
|
245
|
|
|
} |
246
|
|
|
} else { // no main layout found ! |
247
|
|
|
throw new \Exception('No Layout File defined. Check ThemeInfo File of ' . $this->getName(), 9090); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function getRenderEngine() |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
return $this->theme_info['renderengine']; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function isBackendTheme() |
257
|
|
|
{ |
258
|
|
|
return (bool) $this->theme_info['backendtheme']; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function isFrontendTheme() |
262
|
|
|
{ |
263
|
|
|
return (bool) $this->theme_info['backendtheme'] === true ? false : true; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function getArray() |
267
|
|
|
{ |
268
|
|
|
return $this->theme_info; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public static function getThemeDirectories() |
272
|
|
|
{ |
273
|
|
|
return array_merge( |
274
|
|
|
self::iterateDir(APPLICATION_PATH . 'themes/frontend/', 'frontend'), |
275
|
|
|
self::iterateDir(APPLICATION_PATH . 'themes/backend/', 'backend') |
276
|
|
|
); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Iterates over a theme dir (backend / frontend) and fetches some data. |
281
|
|
|
* |
282
|
|
|
* @param string $dir APPLICATION_FRONTEND_THEMES_PATH, APPLICATION_BACKEND_THEMES_PATH |
283
|
|
|
* @param string $type 'frontend' or 'backend' |
284
|
|
|
* @param bool $only_index_name |
285
|
|
|
* |
286
|
|
|
* @return string |
|
|
|
|
287
|
|
|
*/ |
288
|
|
|
protected static function iterateDir($dir, $type, $only_index_name = true) |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
$dirs = ''; |
|
|
|
|
291
|
|
|
$dir_tmp = ''; |
|
|
|
|
292
|
|
|
$i = 0; |
293
|
|
|
$themes = []; |
294
|
|
|
|
295
|
|
|
$dirs = new \DirectoryIterator($dir); |
296
|
|
|
|
297
|
|
|
foreach ($dirs as $dir) { |
298
|
|
|
/* |
299
|
|
|
* Skip early on dots, like "." or ".." or ".svn", by cheching the first char. |
300
|
|
|
* we can not use DirectoryIterator::isDot() here, because it only checks "." and "..". |
301
|
|
|
*/ |
302
|
|
|
$dir_tmp = $dir->getFilename(); |
|
|
|
|
303
|
|
|
|
304
|
|
|
if ($dir_tmp{0} === '.') { |
|
|
|
|
305
|
|
|
continue; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/* |
309
|
|
|
* take only directories in account, which contain a "theme_info.xml" file |
310
|
|
|
*/ |
311
|
|
|
if (is_file($dir->getPathName() . DIRECTORY_SEPARATOR . 'theme_info.xml')) { |
312
|
|
|
$i = $i + 1; |
313
|
|
|
|
314
|
|
|
if ($only_index_name === false) { |
|
|
|
|
315
|
|
|
// add fullpath |
316
|
|
|
$themes[$i]['path'] = $dir->getPathName(); |
317
|
|
|
|
318
|
|
|
// set frontend as type |
319
|
|
|
$themes[$i]['type'] = $type; |
320
|
|
|
|
321
|
|
|
// add dirname |
322
|
|
|
$themes[$i]['name'] = $type . DIRECTORY_SEPARATOR . (string) $dir; |
323
|
|
|
} else { |
324
|
|
|
// add dirname |
325
|
|
|
$themes[$i] = $type . DIRECTORY_SEPARATOR . (string) $dir; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
unset($i, $dirs, $dir_tmp); |
|
|
|
|
331
|
|
|
|
332
|
|
|
return $themes; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.