|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Namespaces.php - A trait for managing namespaces in view/template renderers. |
|
5
|
|
|
* |
|
6
|
|
|
* @package jaxon-core |
|
7
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
8
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
10
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Jaxon\Features\View; |
|
14
|
|
|
|
|
15
|
|
|
trait Namespaces |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The template directories |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $aDirectories = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The directory of the current template |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $sDirectory = ''; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The extension of the current template |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $sExtension = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Add a namespace to this template renderer |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $sNamespace The namespace name |
|
42
|
|
|
* @param string $sDirectory The namespace directory |
|
43
|
|
|
* @param string $sExtension The extension to append to template names |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
|
|
public function addNamespace($sNamespace, $sDirectory, $sExtension = '') |
|
48
|
|
|
{ |
|
49
|
|
|
$this->aDirectories[$sNamespace] = ['path' => $sDirectory, 'ext' => $sExtension]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Find the namespace of the template being rendered |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $sNamespace The namespace name |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setCurrentNamespace($sNamespace) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->sDirectory = ''; |
|
62
|
|
|
$this->sExtension = ''; |
|
63
|
|
|
if(key_exists($sNamespace, $this->aDirectories)) |
|
64
|
|
|
{ |
|
65
|
|
|
// Make sure there's only one '/' at the end of the string |
|
66
|
|
|
$this->sDirectory = rtrim($this->aDirectories[$sNamespace]['path'], '/') . '/'; |
|
67
|
|
|
// Make sure there's only one '.' at the beginning of the string |
|
68
|
|
|
$this->sExtension = '.' . ltrim($this->aDirectories[$sNamespace]['ext'], '.'); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|