|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\Ui\View; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Di\Container; |
|
6
|
|
|
use Jaxon\Utils\Config\Config; |
|
7
|
|
|
|
|
8
|
|
|
use Closure; |
|
9
|
|
|
|
|
10
|
|
|
use function array_filter; |
|
11
|
|
|
use function is_array; |
|
12
|
|
|
use function rtrim; |
|
13
|
|
|
|
|
14
|
|
|
class ViewManager |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Container |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $di; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The view namespaces |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $aNamespaces = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The class constructor |
|
30
|
|
|
* |
|
31
|
|
|
* @param Container $di |
|
|
|
|
|
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(Container $di) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$this->di = $di; |
|
36
|
|
|
} |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Add a view namespace, and set the corresponding 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
|
|
|
* @param string $sRenderer The corresponding renderer name |
|
|
|
|
|
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function addNamespace(string $sNamespace, string $sDirectory, string $sExtension, string $sRenderer) |
|
49
|
|
|
{ |
|
50
|
|
|
$aNamespace = [ |
|
|
|
|
|
|
51
|
|
|
'directory' => $sDirectory, |
|
52
|
|
|
'extension' => $sExtension, |
|
53
|
|
|
'renderer' => $sRenderer, |
|
54
|
|
|
]; |
|
55
|
|
|
$this->aNamespaces[$sNamespace] = $aNamespace; |
|
56
|
|
|
} |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set the view namespaces. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Config $xAppConfig The config options provided in the library |
|
|
|
|
|
|
62
|
|
|
* @param Config|null $xUserConfig The config options provided in the app section of the global config file. |
|
|
|
|
|
|
63
|
|
|
* |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
public function addNamespaces(Config $xAppConfig, ?Config $xUserConfig = null) |
|
67
|
|
|
{ |
|
68
|
|
|
if(empty($aNamespaces = $xAppConfig->getOptionNames('views'))) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
$sPackage = $xAppConfig->getOption('package', ''); |
|
73
|
|
|
foreach($aNamespaces as $sNamespace => $sOption) |
|
74
|
|
|
{ |
|
75
|
|
|
// Save the namespace |
|
76
|
|
|
$aNamespace = $xAppConfig->getOption($sOption); |
|
|
|
|
|
|
77
|
|
|
$aNamespace['package'] = $sPackage; |
|
78
|
|
|
if(!isset($aNamespace['renderer'])) |
|
79
|
|
|
{ |
|
80
|
|
|
$aNamespace['renderer'] = 'jaxon'; // 'jaxon' is the default renderer. |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// If the lib config has defined a template option, then its value must be |
|
84
|
|
|
// read from the app config. |
|
85
|
|
|
if($xUserConfig !== null && isset($aNamespace['template']) && is_array($aNamespace['template'])) |
|
86
|
|
|
{ |
|
87
|
|
|
$sTemplateOption = $xAppConfig->getOption($sOption . '.template.option'); |
|
|
|
|
|
|
88
|
|
|
$sTemplateDefault = $xAppConfig->getOption($sOption . '.template.default'); |
|
|
|
|
|
|
89
|
|
|
$sTemplate = $xUserConfig->getOption($sTemplateOption, $sTemplateDefault); |
|
|
|
|
|
|
90
|
|
|
$aNamespace['directory'] = rtrim($aNamespace['directory'], '/') . '/' . $sTemplate; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->aNamespaces[$sNamespace] = $aNamespace; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the view renderer |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $sId The unique identifier of the view renderer |
|
|
|
|
|
|
101
|
|
|
* |
|
102
|
|
|
* @return ViewInterface |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getRenderer(string $sId): ViewInterface |
|
105
|
|
|
{ |
|
106
|
|
|
// Return the view renderer with the given id |
|
107
|
|
|
return $this->di->g('jaxon.app.view.' . $sId); |
|
108
|
|
|
} |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Add a view renderer with an id |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $sId The unique identifier of the view renderer |
|
|
|
|
|
|
114
|
|
|
* @param Closure $xClosure A closure to create the view instance |
|
|
|
|
|
|
115
|
|
|
* |
|
116
|
|
|
* @return void |
|
117
|
|
|
*/ |
|
118
|
|
|
public function addRenderer(string $sId, Closure $xClosure) |
|
119
|
|
|
{ |
|
120
|
|
|
// Return the initialized view renderer |
|
121
|
|
|
$this->di->set('jaxon.app.view.' . $sId, function($di) use ($sId, $xClosure) { |
|
122
|
|
|
// Get the defined renderer |
|
123
|
|
|
$xRenderer = $xClosure($di); |
|
124
|
|
|
// Init the renderer with the template namespaces |
|
125
|
|
|
$aNamespaces = array_filter($this->aNamespaces, function($aNamespace) use($sId) { |
|
126
|
|
|
return $aNamespace['renderer'] === $sId; |
|
127
|
|
|
}); |
|
128
|
|
|
foreach($aNamespaces as $sNamespace => $aNamespace) |
|
129
|
|
|
{ |
|
130
|
|
|
$xRenderer->addNamespace($sNamespace, $aNamespace['directory'], $aNamespace['extension']); |
|
131
|
|
|
} |
|
132
|
|
|
return $xRenderer; |
|
133
|
|
|
}); |
|
134
|
|
|
} |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the view renderer for a given namespace |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $sNamespace The namespace name |
|
|
|
|
|
|
140
|
|
|
* |
|
141
|
|
|
* @return ViewInterface|null |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getNamespaceRenderer(string $sNamespace): ?ViewInterface |
|
144
|
|
|
{ |
|
145
|
|
|
if(!isset($this->aNamespaces[$sNamespace])) |
|
146
|
|
|
{ |
|
147
|
|
|
return null; |
|
148
|
|
|
} |
|
149
|
|
|
// Return the view renderer with the configured id |
|
150
|
|
|
return $this->getRenderer($this->aNamespaces[$sNamespace]['renderer']); |
|
151
|
|
|
} |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|