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
|
|
|
* Compressing css file |
354
|
|
|
* @param string $css text of css file |
355
|
|
|
* @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
356
|
|
|
* @return string |
357
|
|
|
*/ |
358
|
|
|
private function compressCss($css) { |
359
|
|
|
$compressor = new CSSmin(); |
360
|
|
|
|
361
|
|
|
return $compressor->run($css); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Put css string into template |
366
|
|
|
* @return assetManager |
367
|
|
|
* @access public |
368
|
|
|
* @author a.gula |
369
|
|
|
* @param string $css |
|
|
|
|
370
|
|
|
* @param boolean $useCompress |
371
|
|
|
* @copyright ImageCMS (c) 2013, a.gula <[email protected]> |
372
|
|
|
*/ |
373
|
|
|
public function registerStyleStr($css, $useCompress = FALSE) { |
374
|
|
|
/** Start. Load file into template */ |
375
|
|
|
if ($useCompress) { |
376
|
|
|
CI_Controller::get_instance()->template->registerCss('<style>' . $this->compressCss($css) . '</style>', 'before'); |
377
|
|
|
} else { |
378
|
|
|
CI_Controller::get_instance()->template->registerCss('<style>' . $css . '</style>', 'before'); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @param string $name |
386
|
|
|
*/ |
387
|
|
|
public function registerStyleWithoutTemplate($name) { |
388
|
|
|
$script = '/' . $this->buildStylePath($name); |
389
|
|
|
$this->setData([$name => $script]); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Return formated path for css |
394
|
|
|
* @param string $fileName |
395
|
|
|
* @return string |
396
|
|
|
* @access private |
397
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
398
|
|
|
*/ |
399
|
|
View Code Duplication |
private function buildStylePath($fileName) { |
400
|
|
|
if (!$this->template) { |
401
|
|
|
$this->template = CI_Controller::get_instance()->config->item('template'); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
$moduleName = $this->getTrace(); |
405
|
|
|
$path = sprintf('templates/%s/%s/css/%s.css', $this->template, $moduleName, $fileName); |
406
|
|
|
if (file_exists($path)) { |
407
|
|
|
$url = $path; |
408
|
|
|
} else { |
409
|
|
|
$url = $this->getModuleFilePath( |
410
|
|
|
[ |
411
|
|
|
sprintf('%s/assets/css/%s.css', $moduleName, $fileName), |
412
|
|
|
sprintf('%s/assets/css/%s.css', CI::$APP->uri->segment(4), $fileName), |
413
|
|
|
], |
414
|
|
|
false |
415
|
|
|
); |
416
|
|
|
} |
417
|
|
|
return str_replace(MAINSITE, '', $url); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Render public view |
422
|
|
|
* @param string $tpl Template file name |
423
|
|
|
* @param bool $ignoreWrap |
424
|
|
|
* @param bool $fetchJsTpl |
425
|
|
|
* @access public |
426
|
|
|
* @author Kaero |
427
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
428
|
|
|
*/ |
429
|
|
|
public function render($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE) { |
430
|
|
|
$this->_render($tpl, $ignoreWrap, $fetchJsTpl); |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Render Admin view |
435
|
|
|
* @param string $tpl Template file name |
436
|
|
|
* @param bool $ignoreWrap |
437
|
|
|
* @param bool $fetchJsTpl |
438
|
|
|
* @access public |
439
|
|
|
* @author Kaero |
440
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
441
|
|
|
*/ |
442
|
|
|
public function renderAdmin($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE) { |
443
|
|
|
$this->_render($tpl, $ignoreWrap, $fetchJsTpl, TRUE); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* |
448
|
|
|
* @param string $tpl |
449
|
|
|
* @param boolean $ignoreWrap |
450
|
|
|
* @param boolean $fetchJsTpl |
451
|
|
|
* @param boolean $admin |
452
|
|
|
*/ |
453
|
|
|
private function _render($tpl, $ignoreWrap = FALSE, $fetchJsTpl = TRUE, $admin = FALSE) { |
454
|
|
|
if (CI_Controller::get_instance()->input->is_ajax_request()) { |
455
|
|
|
$ignoreWrap = TRUE; |
456
|
|
|
} |
457
|
|
|
if (CI_Controller::get_instance()->input->post('ignoreWrap')) { |
458
|
|
|
$ignoreWrap = TRUE; |
459
|
|
|
} |
460
|
|
|
if (CI_Controller::get_instance()->input->post('template')) { |
461
|
|
|
$tpl = CI_Controller::get_instance()->input->post('template'); |
462
|
|
|
} |
463
|
|
|
if (CI_Controller::get_instance()->input->get('ignoreWrap')) { |
464
|
|
|
$ignoreWrap = TRUE; |
465
|
|
|
} |
466
|
|
|
if (CI_Controller::get_instance()->input->get('template')) { |
467
|
|
|
$tpl = CI_Controller::get_instance()->input->get('template'); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
try { |
471
|
|
|
$data = []; |
472
|
|
|
if ($fetchJsTpl) { |
473
|
|
|
/** Start. If file doesn't exists thorow exception */ |
474
|
|
|
|
475
|
|
|
$js_langs_path = $this->buildTemplatePath($this->module_js); |
476
|
|
|
if (file_exists($js_langs_path . '.tpl')) { |
477
|
|
|
/** Start. Load template file */ |
478
|
|
|
if (MAINSITE) { |
479
|
|
|
$data = ['js_langs_path' => 'file:' . $js_langs_path]; |
480
|
|
|
} else { |
481
|
|
|
$data = ['js_langs_path' => 'file:./' . $js_langs_path]; |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** Start. Load template file */ |
487
|
|
|
CI_Controller::get_instance()->template->show('file:' . $this->_buildTemplatePath($tpl, null, $admin), !$ignoreWrap, $data); |
488
|
|
|
} catch (Exception $exc) { |
489
|
|
|
log_message('error', $exc->getMessage()); |
490
|
|
|
show_error($exc->getMessage(), 500, 'An Template Error Was Encountered'); |
491
|
|
|
} |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Return formatted path |
496
|
|
|
* @access private |
497
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
498
|
|
|
* @param string $tpl |
|
|
|
|
499
|
|
|
* @param string $moduleName |
|
|
|
|
500
|
|
|
* @return string |
501
|
|
|
*/ |
502
|
|
|
private function buildTemplatePath($tpl, $moduleName = null) { |
503
|
|
|
if (!$this->template) { |
504
|
|
|
$this->template = CI_Controller::get_instance()->config->item('template'); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
$path = 'templates/' . $this->template . '/' . $this->getTrace() . '/' . $tpl; |
508
|
|
|
$path = $this->makePath($path); |
509
|
|
|
|
510
|
|
|
if (file_exists($path . '.tpl')) { |
511
|
|
|
return $path; |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
if (!$moduleName) { |
515
|
|
|
$moduleName = $this->getTrace(); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
$modulePath = getModulePath($moduleName); |
|
|
|
|
519
|
|
|
return $this->makePath("{$modulePath}assets/$tpl"); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* |
524
|
|
|
* @param string $path |
525
|
|
|
* @return string |
526
|
|
|
*/ |
527
|
|
|
protected function makePath($path) { |
528
|
|
|
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path); |
529
|
|
|
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); |
530
|
|
|
$startSlash = strpos($path, '/') === 0 ? '/' : ''; |
531
|
|
|
|
532
|
|
|
$absolutes = []; |
533
|
|
|
foreach ($parts as $part) { |
534
|
|
|
if ('.' == $part) { |
535
|
|
|
continue; |
536
|
|
|
} |
537
|
|
|
if ('..' == $part) { |
538
|
|
|
array_pop($absolutes); |
539
|
|
|
} else { |
540
|
|
|
$absolutes[] = $part; |
541
|
|
|
} |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
return $startSlash . implode(DIRECTORY_SEPARATOR, $absolutes); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* |
549
|
|
|
* @param string $tpl |
550
|
|
|
* @param string $moduleName |
|
|
|
|
551
|
|
|
* @param boolean $admin |
552
|
|
|
* @return string |
|
|
|
|
553
|
|
|
* @throws Exception |
|
|
|
|
554
|
|
|
*/ |
555
|
|
|
private function _buildTemplatePath($tpl, $moduleName = null, $admin = FALSE) { |
556
|
|
|
if ($admin) { |
557
|
|
|
$path = $this->buildAdminTemplatePath($tpl); |
558
|
|
|
} else { |
559
|
|
|
$path = $this->buildTemplatePath($tpl, $moduleName); |
560
|
|
|
} |
561
|
|
|
/** Start. If file doesn't exists thorow exception */ |
562
|
|
|
if (!file_exists($path . '.tpl')) { |
563
|
|
|
throw new Exception("Can't load template file: <i>$path.tpl</i>"); |
564
|
|
|
} |
565
|
|
|
return $path; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Return formatted path |
570
|
|
|
* @param string $fileName |
571
|
|
|
* @return string |
|
|
|
|
572
|
|
|
* @access private |
573
|
|
|
* @copyright ImageCMS (c) 2013, Kaero <[email protected]> |
574
|
|
|
*/ |
575
|
|
|
private function buildAdminTemplatePath($fileName) { |
576
|
|
|
$path = $this->getModuleFilePath( |
577
|
|
|
[ |
578
|
|
|
sprintf('%s/assets/admin/%s.tpl', $this->getTrace(), $fileName), |
579
|
|
|
sprintf('%s/assets/admin/%s.tpl', CI::$APP->uri->segment(4), $fileName), |
580
|
|
|
] |
581
|
|
|
); |
582
|
|
|
return $path; |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
/** |
586
|
|
|
* Changing main layout file |
587
|
|
|
* @param string $mainLayout |
588
|
|
|
* @return assetManager |
589
|
|
|
*/ |
590
|
|
View Code Duplication |
public function setMainLayout($mainLayout) { |
591
|
|
|
try { |
592
|
|
|
CI_Controller::get_instance()->template->set_main_layout($mainLayout); |
593
|
|
|
} catch (Exception $exc) { |
594
|
|
|
log_message('error', $exc->getMessage()); |
595
|
|
|
show_error($exc->getMessage(), 500, 'An Template Error Was Encountered'); |
596
|
|
|
} |
597
|
|
|
return $this; |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Changing main layout file by full path |
602
|
|
|
* @param string $mainLayout |
603
|
|
|
* @return assetManager |
604
|
|
|
*/ |
605
|
|
View Code Duplication |
public function setMainLayoutByFullPath($mainLayout) { |
606
|
|
|
try { |
607
|
|
|
CI_Controller::get_instance()->template->set_main_layout_by_full_path($mainLayout); |
608
|
|
|
} catch (Exception $exc) { |
609
|
|
|
log_message('error', $exc->getMessage()); |
610
|
|
|
show_error($exc->getMessage(), 500, 'An Template Error Was Encountered'); |
611
|
|
|
} |
612
|
|
|
return $this; |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
} |
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: