1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CMSFactory; |
4
|
|
|
|
5
|
|
|
use CI; |
6
|
|
|
use CI_Controller; |
7
|
|
|
use CI_Input; |
8
|
|
|
use CSSmin; |
9
|
|
|
use Exception; |
|
|
|
|
10
|
|
|
use Modules; |
11
|
|
|
use MY_Controller; |
12
|
|
|
use template_manager\classes\Template; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
16
|
|
|
* @property CI_Input $input |
17
|
|
|
*/ |
18
|
|
|
class assetManager |
|
|
|
|
19
|
|
|
{ |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var assetManager |
23
|
|
|
*/ |
24
|
|
|
protected static $_BehaviorInstance; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $callMapp; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @var MY_Controller |
34
|
|
|
*/ |
35
|
|
|
protected $ci; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $module_js = 'jsLangs'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* |
44
|
|
|
* @var Template |
45
|
|
|
*/ |
46
|
|
|
protected $template; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $useCompress = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* assetManager constructor. |
55
|
|
|
*/ |
56
|
|
|
private function __construct() { |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function __clone() { |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string|array $item |
66
|
|
|
* @param string|integer|float $value |
67
|
|
|
* @return assetManager |
68
|
|
|
* @access public |
69
|
|
|
* @copyright ImageCMS (c) 2013, Roman <[email protected]> |
70
|
|
|
*/ |
71
|
|
|
public function appendData($item, $value) { |
72
|
|
|
$this->setData($item, CI_Controller::get_instance()->template->get_var($item) . $value); |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string|array $item |
78
|
|
|
* @param string|integer|float|array|boolean $value |
|
|
|
|
79
|
|
|
* @return assetManager |
80
|
|
|
* @access public |
81
|
|
|
* @author Kaero |
82
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
83
|
|
|
*/ |
84
|
|
|
public function setData($item, $value = null) { |
85
|
|
|
if ($value != null AND !is_array($item)) { |
86
|
|
|
$data[$item] = $value; |
|
|
|
|
87
|
|
|
} else { |
88
|
|
|
$data = $item; |
89
|
|
|
} |
90
|
|
|
(empty($data)) OR CI_Controller::get_instance()->template->add_array((array) $data); |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return assetManager |
96
|
|
|
* @access public |
97
|
|
|
* @author Kaero |
98
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
99
|
|
|
*/ |
100
|
|
|
public static function create() { |
101
|
|
|
(null !== self::$_BehaviorInstance) OR self::$_BehaviorInstance = new self(); |
102
|
|
|
self::$_BehaviorInstance->callMapp = debug_backtrace(); |
103
|
|
|
return self::$_BehaviorInstance; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* fetch admin view |
108
|
|
|
* @param string $tpl Template file name |
109
|
|
|
* @param boolean $fetchLangsTpl |
110
|
|
|
* @return string |
111
|
|
|
* @access public |
112
|
|
|
* @author Kaero |
113
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
114
|
|
|
*/ |
115
|
|
|
public function fetchAdminTemplate($tpl, $fetchLangsTpl = TRUE) { |
116
|
|
|
try { |
117
|
|
|
|
118
|
|
|
if ($fetchLangsTpl) { |
119
|
|
|
/** Start. Load template file */ |
120
|
|
|
$view = CI_Controller::get_instance()->template->fetch('file:' . $this->_buildTemplatePath($this->module_js)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (isset($view)) { |
124
|
|
|
return $view . CI_Controller::get_instance()->template->fetch('file:' . $this->_buildTemplatePath($tpl, null, true)); |
125
|
|
|
} else { |
126
|
|
|
return CI_Controller::get_instance()->template->fetch('file:' . $this->_buildTemplatePath($tpl, null, true)); |
127
|
|
|
} |
128
|
|
|
/** Start. Return template file */ |
129
|
|
|
} catch (Exception $exc) { |
130
|
|
|
log_message('error', $exc->getMessage()); |
131
|
|
|
show_error($exc->getMessage(), 500, 'An Template Error Was Encountered'); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* fetch public view |
137
|
|
|
* @param string $tpl Template file name |
138
|
|
|
* @param string $moduleName |
139
|
|
|
* @return string |
140
|
|
|
* @access public |
141
|
|
|
* @author Kaero |
142
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function fetchTemplate($tpl, $moduleName = null) { |
145
|
|
|
try { |
146
|
|
|
/** Start. Return template file */ |
147
|
|
|
return CI_Controller::get_instance()->template->fetch('file:' . $this->_buildTemplatePath($tpl, $moduleName)); |
148
|
|
|
} catch (Exception $exc) { |
149
|
|
|
log_message('error', $exc->getMessage()); |
150
|
|
|
show_error($exc->getMessage(), 500, 'An Template Error Was Encountered'); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $item |
156
|
|
|
* @return string|integer|float|array|boolean |
157
|
|
|
* @access public |
158
|
|
|
* @author |
159
|
|
|
* @copyright |
160
|
|
|
*/ |
161
|
|
|
public function getData($item) { |
162
|
|
|
return CI_Controller::get_instance()->template->get_var($item); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* |
167
|
|
|
* @param string $path |
168
|
|
|
* @param string $position |
169
|
|
|
* @return assetManager |
170
|
|
|
*/ |
171
|
|
|
public function registerJsFullpath($path, $position = 'after') { |
172
|
|
|
CI_Controller::get_instance()->template->registerJsFile($path, $position, false); |
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $message |
178
|
|
|
* @param string $title |
179
|
|
|
* @param string $class |
180
|
|
|
*/ |
181
|
|
|
public function registerJsMessage($message, $title, $class = '') { |
182
|
|
|
$script = showMessage($message, $title, $class, true, true); |
183
|
|
|
CI_Controller::get_instance()->template->registerJsScript($script, 'after'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @access public |
188
|
|
|
* @author a.gula |
189
|
|
|
* @param string $script |
190
|
|
|
* @param boolean $useCompress |
191
|
|
|
* @param string $position after|before |
192
|
|
|
* @param string $type |
193
|
|
|
* @return assetManager |
194
|
|
|
* @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
195
|
|
|
*/ |
196
|
|
|
public function registerJsScript($script, $useCompress = FALSE, $position = 'after', $type = 'text/javascript') { |
197
|
|
|
/** Start. Load JS script into template */ |
198
|
|
|
if ($useCompress) { |
199
|
|
|
CI_Controller::get_instance()->template->registerJsScript("<script type='$type'>" . $this->compressJs($script) . '</script>', $position); |
200
|
|
|
} else { |
201
|
|
|
CI_Controller::get_instance()->template->registerJsScript("<script type='$type'>" . $script . '</script>', $position); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return assetManager |
209
|
|
|
* @access public |
210
|
|
|
* @author Kaero |
211
|
|
|
* @param string $name |
212
|
|
|
* @param boolean $useCompress |
213
|
|
|
* @param string $position |
214
|
|
|
* @return assetManager |
|
|
|
|
215
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
216
|
|
|
*/ |
217
|
|
|
public function registerScript($name, $useCompress = FALSE, $position = 'after') { |
218
|
|
|
/** Start. Load JS file into template */ |
219
|
|
|
if ($useCompress) { |
220
|
|
|
CI_Controller::get_instance()->template->registerJsScript('<script>' . $this->compressJs(file_get_contents($this->buildScriptPath($name))) . '</script>', $position); |
221
|
|
|
} else { |
222
|
|
|
CI_Controller::get_instance()->template->registerJsFile('/' . $this->buildScriptPath($name), $position); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param string $js |
230
|
|
|
* @return string |
231
|
|
|
* @todo compress and cache |
232
|
|
|
*/ |
233
|
|
|
private function compressJs($js) { |
234
|
|
|
return $js; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Return formated path for JS - script files |
239
|
|
|
* @param string $fileName |
240
|
|
|
* @return string |
241
|
|
|
* @access private |
242
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
243
|
|
|
*/ |
244
|
|
View Code Duplication |
private function buildScriptPath($fileName) { |
245
|
|
|
$this->template = CI_Controller::get_instance()->config->item('template'); |
246
|
|
|
|
247
|
|
|
$moduleName = $this->getTrace(); |
248
|
|
|
$path = sprintf('templates/%s/%s/js/%s.js', $this->template, $moduleName, $fileName); |
249
|
|
|
if (file_exists($path)) { |
250
|
|
|
$url = $path; |
251
|
|
|
} else { |
252
|
|
|
$url = $this->getModuleFilePath( |
253
|
|
|
[ |
254
|
|
|
sprintf('%s/assets/js/%s.js', $moduleName, $fileName), |
255
|
|
|
sprintf('%s/assets/js/%s.js', CI::$APP->uri->segment(4), $fileName), |
256
|
|
|
], |
257
|
|
|
false |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return str_replace(MAINSITE, '', $url); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string $list |
266
|
|
|
* @return array |
267
|
|
|
* @access public |
268
|
|
|
* @author cutter |
269
|
|
|
*/ |
270
|
|
|
private function getTrace($list = 'first_file') { |
271
|
|
|
if ($list == 'first_file') { |
272
|
|
|
$paths = explode(DIRECTORY_SEPARATOR, $this->callMapp[0]['file']); |
273
|
|
|
return $paths[count($paths) - 2]; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if ($list == 'first') { |
277
|
|
|
return $this->callMapp[0]; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
if ($list == 'all') { |
281
|
|
|
return $this->callMapp; |
282
|
|
|
} |
283
|
|
|
if (is_numeric($list)) { |
284
|
|
|
return $this->callMapp[$list]; |
285
|
|
|
} |
286
|
|
|
return false; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Checks if file exists in any of modules dirs. If exists returns its path |
291
|
|
|
* @param string|array $files example: ['menu/assets/css/style.css'] |
292
|
|
|
* @param bool $noExt |
293
|
|
|
* @return bool|string returns file path or FALSE |
|
|
|
|
294
|
|
|
*/ |
295
|
|
|
private function getModuleFilePath($files, $noExt = true) { |
296
|
|
|
|
297
|
|
|
if (is_string($files)) { |
298
|
|
|
$files = [$files]; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
foreach (Modules::$locations as $path => $relPath) { |
302
|
|
|
foreach ($files as $fp) { |
303
|
|
|
$absPath = $path . ltrim($fp, '/'); |
304
|
|
|
if (file_exists($absPath)) { |
305
|
|
|
if ($noExt == true) { |
306
|
|
|
$absPath = explode('.', $absPath); |
307
|
|
|
array_pop($absPath); |
308
|
|
|
return implode('.', $absPath); |
309
|
|
|
} else { |
310
|
|
|
return $absPath; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
return false; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param string $name |
320
|
|
|
*/ |
321
|
|
|
public function registerScriptWithoutTemplate($name) { |
322
|
|
|
$script = '/' . $this->buildScriptPath($name); |
323
|
|
|
$this->setData([$name => $script]); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @return assetManager |
328
|
|
|
* @access public |
329
|
|
|
* @author Kaero |
330
|
|
|
* @param string $name |
331
|
|
|
* @param boolean $useCompress |
332
|
|
|
* @return assetManager |
|
|
|
|
333
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
334
|
|
|
*/ |
335
|
|
|
public function registerStyle($name, $useCompress = FALSE) { |
336
|
|
|
/** Start. Load file into template */ |
337
|
|
|
|
338
|
|
|
$path = $this->buildStylePath($name); |
339
|
|
|
if ('' !== $path) { |
340
|
|
|
if ($useCompress) { |
341
|
|
|
if ($content = file_get_contents($path)) { |
342
|
|
|
CI_Controller::get_instance()->template->registerCss('<style>' . $this->compressCss($content) . '</style>', 'before'); |
343
|
|
|
} |
344
|
|
|
} else { |
345
|
|
|
CI_Controller::get_instance()->template->registerCssFile('/' . $path, 'before'); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
return $this; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @param string $path |
354
|
|
|
* @param string $position |
355
|
|
|
*/ |
356
|
|
|
public function assetTemplateFiles($path, $position = 'before') { |
357
|
|
|
|
358
|
|
|
$template_name = config_item('template'); |
359
|
|
|
|
360
|
|
|
$path_info = pathinfo($path); |
361
|
|
|
|
362
|
|
|
$path = '/templates/' . $template_name . '/' . ltrim($path, '/'); |
363
|
|
|
|
364
|
|
|
if ($path_info['extension'] == 'css') { |
365
|
|
|
|
366
|
|
|
CI_Controller::get_instance()->template->registerCssFile($path, $position); |
367
|
|
|
} elseif ($path_info['extension'] == 'js') { |
368
|
|
|
|
369
|
|
|
CI_Controller::get_instance()->template->registerJsFile($path, $position, false); |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Compressing css file |
375
|
|
|
* @param string $css text of css file |
376
|
|
|
* @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
377
|
|
|
* @return string |
378
|
|
|
*/ |
379
|
|
|
private function compressCss($css) { |
380
|
|
|
$compressor = new CSSmin(); |
381
|
|
|
|
382
|
|
|
return $compressor->run($css); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Put css string into template |
387
|
|
|
* @return assetManager |
388
|
|
|
* @access public |
389
|
|
|
* @author a.gula |
390
|
|
|
* @param string $css |
|
|
|
|
391
|
|
|
* @param boolean $useCompress |
392
|
|
|
* @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
393
|
|
|
*/ |
394
|
|
|
public function registerStyleStr($css, $useCompress = FALSE) { |
395
|
|
|
/** Start. Load file into template */ |
396
|
|
|
if ($useCompress) { |
397
|
|
|
CI_Controller::get_instance()->template->registerCss('<style>' . $this->compressCss($css) . '</style>', 'before'); |
398
|
|
|
} else { |
399
|
|
|
CI_Controller::get_instance()->template->registerCss('<style>' . $css . '</style>', 'before'); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
return $this; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @param string $name |
407
|
|
|
*/ |
408
|
|
|
public function registerStyleWithoutTemplate($name) { |
409
|
|
|
$script = '/' . $this->buildStylePath($name); |
410
|
|
|
$this->setData([$name => $script]); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Return formated path for css |
415
|
|
|
* @param string $fileName |
416
|
|
|
* @return string |
417
|
|
|
* @access private |
418
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
419
|
|
|
*/ |
420
|
|
View Code Duplication |
private function buildStylePath($fileName) { |
421
|
|
|
if (!$this->template) { |
422
|
|
|
$this->template = CI_Controller::get_instance()->config->item('template'); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
$moduleName = $this->getTrace(); |
426
|
|
|
$path = sprintf('templates/%s/%s/css/%s.css', $this->template, $moduleName, $fileName); |
427
|
|
|
if (file_exists($path)) { |
428
|
|
|
$url = $path; |
429
|
|
|
} else { |
430
|
|
|
$url = $this->getModuleFilePath( |
431
|
|
|
[ |
432
|
|
|
sprintf('%s/assets/css/%s.css', $moduleName, $fileName), |
433
|
|
|
sprintf('%s/assets/css/%s.css', CI::$APP->uri->segment(4), $fileName), |
434
|
|
|
], |
435
|
|
|
false |
436
|
|
|
); |
437
|
|
|
} |
438
|
|
|
return str_replace(MAINSITE, '', $url); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Render public view |
443
|
|
|
* @param string $tpl Template file name |
444
|
|
|
* @param bool $ignoreWrap |
445
|
|
|
* @param bool $fetchJsTpl |
446
|
|
|
* @access public |
447
|
|
|
* @author Kaero |
448
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
449
|
|
|
*/ |
450
|
|
|
public function render($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE) { |
451
|
|
|
$this->_render($tpl, $ignoreWrap, $fetchJsTpl); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Render Admin view |
456
|
|
|
* @param string $tpl Template file name |
457
|
|
|
* @param bool $ignoreWrap |
458
|
|
|
* @param bool $fetchJsTpl |
459
|
|
|
* @access public |
460
|
|
|
* @author Kaero |
461
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
462
|
|
|
*/ |
463
|
|
|
public function renderAdmin($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE) { |
464
|
|
|
$this->_render($tpl, $ignoreWrap, $fetchJsTpl, TRUE); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* |
469
|
|
|
* @param string $tpl |
470
|
|
|
* @param boolean $ignoreWrap |
471
|
|
|
* @param boolean $fetchJsTpl |
472
|
|
|
* @param boolean $admin |
473
|
|
|
*/ |
474
|
|
|
private function _render($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE, $admin = FALSE) { |
475
|
|
|
if (CI_Controller::get_instance()->input->is_ajax_request()) { |
476
|
|
|
$ignoreWrap = TRUE; |
477
|
|
|
} |
478
|
|
|
if (CI_Controller::get_instance()->input->post('ignoreWrap')) { |
479
|
|
|
$ignoreWrap = TRUE; |
480
|
|
|
} |
481
|
|
|
if (CI_Controller::get_instance()->input->post('template')) { |
482
|
|
|
$tpl = CI_Controller::get_instance()->input->post('template'); |
483
|
|
|
} |
484
|
|
|
if (CI_Controller::get_instance()->input->get('ignoreWrap')) { |
485
|
|
|
$ignoreWrap = TRUE; |
486
|
|
|
} |
487
|
|
|
if (CI_Controller::get_instance()->input->get('template')) { |
488
|
|
|
$tpl = CI_Controller::get_instance()->input->get('template'); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
try { |
492
|
|
|
$data = []; |
493
|
|
|
if ($fetchJsTpl) { |
494
|
|
|
/** Start. If file doesn't exists thorow exception */ |
495
|
|
|
|
496
|
|
|
$js_langs_path = $this->buildTemplatePath($this->module_js); |
497
|
|
|
if (file_exists($js_langs_path . '.tpl')) { |
498
|
|
|
/** Start. Load template file */ |
499
|
|
|
if (MAINSITE) { |
500
|
|
|
$data = ['js_langs_path' => 'file:' . $js_langs_path]; |
501
|
|
|
} else { |
502
|
|
|
$data = ['js_langs_path' => 'file:./' . $js_langs_path]; |
503
|
|
|
} |
504
|
|
|
} |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** Start. Load template file */ |
508
|
|
|
CI_Controller::get_instance()->template->show('file:' . $this->_buildTemplatePath($tpl, null, $admin), !$ignoreWrap, $data); |
509
|
|
|
} catch (Exception $exc) { |
510
|
|
|
log_message('error', $exc->getMessage()); |
511
|
|
|
show_error($exc->getMessage(), 500, 'An Template Error Was Encountered'); |
512
|
|
|
} |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Return formatted path |
517
|
|
|
* @access private |
518
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
519
|
|
|
* @param string $tpl |
|
|
|
|
520
|
|
|
* @param string $moduleName |
521
|
|
|
* @return string |
522
|
|
|
*/ |
523
|
|
|
private function buildTemplatePath($tpl, $moduleName = null) { |
524
|
|
|
if (!$this->template) { |
525
|
|
|
$this->template = CI_Controller::get_instance()->config->item('template'); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
$path = 'templates/' . $this->template . '/' . $this->getTrace() . '/' . $tpl; |
529
|
|
|
$path = $this->makePath($path); |
530
|
|
|
|
531
|
|
|
if (file_exists($path . '.tpl')) { |
532
|
|
|
return $path; |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
if (!$moduleName) { |
536
|
|
|
$moduleName = $this->getTrace(); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
$modulePath = getModulePath($moduleName); |
|
|
|
|
540
|
|
|
return $this->makePath("{$modulePath}assets/$tpl"); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* |
545
|
|
|
* @param string $path |
546
|
|
|
* @return string |
547
|
|
|
*/ |
548
|
|
|
protected function makePath($path) { |
549
|
|
|
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path); |
550
|
|
|
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); |
551
|
|
|
$startSlash = strpos($path, '/') === 0 ? '/' : ''; |
552
|
|
|
|
553
|
|
|
$absolutes = []; |
554
|
|
|
foreach ($parts as $part) { |
555
|
|
|
if ('.' == $part) { |
556
|
|
|
continue; |
557
|
|
|
} |
558
|
|
|
if ('..' == $part) { |
559
|
|
|
array_pop($absolutes); |
560
|
|
|
} else { |
561
|
|
|
$absolutes[] = $part; |
562
|
|
|
} |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
return $startSlash . implode(DIRECTORY_SEPARATOR, $absolutes); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* |
570
|
|
|
* @param string $tpl |
571
|
|
|
* @param string $moduleName |
572
|
|
|
* @param boolean $admin |
573
|
|
|
* @return string |
|
|
|
|
574
|
|
|
* @throws Exception |
|
|
|
|
575
|
|
|
*/ |
576
|
|
|
private function _buildTemplatePath($tpl, $moduleName = null, $admin = FALSE) { |
577
|
|
|
if ($admin) { |
578
|
|
|
$path = $this->buildAdminTemplatePath($tpl); |
579
|
|
|
} else { |
580
|
|
|
$path = $this->buildTemplatePath($tpl, $moduleName); |
581
|
|
|
} |
582
|
|
|
/** Start. If file doesn't exists thorow exception */ |
583
|
|
|
if (!file_exists($path . '.tpl')) { |
584
|
|
|
throw new Exception("Can't load template file: <i>$path.tpl</i>"); |
585
|
|
|
} |
586
|
|
|
return $path; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Return formatted path |
591
|
|
|
* @param string $fileName |
592
|
|
|
* @return string |
|
|
|
|
593
|
|
|
* @access private |
594
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
595
|
|
|
*/ |
596
|
|
|
private function buildAdminTemplatePath($fileName) { |
597
|
|
|
$path = $this->getModuleFilePath( |
598
|
|
|
[ |
599
|
|
|
sprintf('%s/assets/admin/%s.tpl', $this->getTrace(), $fileName), |
600
|
|
|
sprintf('%s/assets/admin/%s.tpl', CI::$APP->uri->segment(4), $fileName), |
601
|
|
|
] |
602
|
|
|
); |
603
|
|
|
return $path; |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* Changing main layout file |
608
|
|
|
* @param string $mainLayout |
609
|
|
|
* @return assetManager |
610
|
|
|
*/ |
611
|
|
View Code Duplication |
public function setMainLayout($mainLayout) { |
612
|
|
|
try { |
613
|
|
|
CI_Controller::get_instance()->template->set_main_layout($mainLayout); |
614
|
|
|
} catch (Exception $exc) { |
615
|
|
|
log_message('error', $exc->getMessage()); |
616
|
|
|
show_error($exc->getMessage(), 500, 'An Template Error Was Encountered'); |
617
|
|
|
} |
618
|
|
|
return $this; |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Changing main layout file by full path |
623
|
|
|
* @param string $mainLayout |
624
|
|
|
* @return assetManager |
625
|
|
|
*/ |
626
|
|
View Code Duplication |
public function setMainLayoutByFullPath($mainLayout) { |
627
|
|
|
try { |
628
|
|
|
CI_Controller::get_instance()->template->set_main_layout_by_full_path($mainLayout); |
629
|
|
|
} catch (Exception $exc) { |
630
|
|
|
log_message('error', $exc->getMessage()); |
631
|
|
|
show_error($exc->getMessage(), 500, 'An Template Error Was Encountered'); |
632
|
|
|
} |
633
|
|
|
return $this; |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: