|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jaxon\Utils\View; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Utils\Config\Config; |
|
6
|
|
|
|
|
7
|
|
|
use stdClass; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Closure; |
|
10
|
|
|
|
|
11
|
|
|
class Manager |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The default namespace |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $sDefaultNamespace = ''; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The view renderers |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $aRenderers = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The view namespaces |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $aNamespaces = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get the default namespace |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getDefaultNamespace() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->sDefaultNamespace; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the view renderers |
|
46
|
|
|
* |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getRenderers() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->aRenderers; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the view namespaces |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getNamespaces() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->aNamespaces; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Add a view namespace, and set the corresponding renderer. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $sNamespace The namespace name |
|
68
|
|
|
* @param string $sDirectory The namespace directory |
|
69
|
|
|
* @param string $sExtension The extension to append to template names |
|
70
|
|
|
* @param string $sRenderer The corresponding renderer name |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
public function addNamespace($sNamespace, $sDirectory, $sExtension, $sRenderer) |
|
75
|
|
|
{ |
|
76
|
|
|
$aNamespace = [ |
|
77
|
|
|
'namespace' => $sNamespace, |
|
78
|
|
|
'directory' => $sDirectory, |
|
79
|
|
|
'extension' => $sExtension, |
|
80
|
|
|
]; |
|
81
|
|
|
if(key_exists($sRenderer, $this->aNamespaces)) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->aNamespaces[$sRenderer][] = $aNamespace; |
|
84
|
|
|
} |
|
85
|
|
|
else |
|
86
|
|
|
{ |
|
87
|
|
|
$this->aNamespaces[$sRenderer] = [$aNamespace]; |
|
88
|
|
|
} |
|
89
|
|
|
$this->aRenderers[$sNamespace] = $sRenderer; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set the view namespaces. |
|
94
|
|
|
* |
|
95
|
|
|
* @param Config $xAppConfig The application config options |
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
public function addNamespaces($xAppConfig) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->sDefaultNamespace = $xAppConfig->getOption('options.views.default', false); |
|
102
|
|
|
if(is_array($namespaces = $xAppConfig->getOptionNames('views'))) |
|
103
|
|
|
{ |
|
104
|
|
|
foreach($namespaces as $namespace => $option) |
|
105
|
|
|
{ |
|
106
|
|
|
// If no default namespace is defined, use the first one as default. |
|
107
|
|
|
if($this->sDefaultNamespace == false) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->sDefaultNamespace = $namespace; |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
// Save the namespace |
|
112
|
|
|
$directory = $xAppConfig->getOption($option . '.directory'); |
|
113
|
|
|
$extension = $xAppConfig->getOption($option . '.extension', ''); |
|
114
|
|
|
$renderer = $xAppConfig->getOption($option . '.renderer', 'jaxon'); |
|
115
|
|
|
$this->addNamespace($namespace, $directory, $extension, $renderer); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the view renderer facade |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $sId The unique identifier of the view renderer |
|
124
|
|
|
* |
|
125
|
|
|
* @return object The view renderer |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getRenderer($sId = '') |
|
128
|
|
|
{ |
|
129
|
|
|
if(!$sId) |
|
130
|
|
|
{ |
|
131
|
|
|
// Return the view renderer facade |
|
132
|
|
|
return jaxon()->di()->get(\Jaxon\Utils\View\Renderer::class); |
|
133
|
|
|
} |
|
134
|
|
|
// Return the view renderer with the given id |
|
135
|
|
|
return jaxon()->di()->get('jaxon.app.view.' . $sId); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Add a view renderer with an id |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $sId The unique identifier of the view renderer |
|
142
|
|
|
* @param Closure $xClosure A closure to create the view instance |
|
143
|
|
|
* |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
|
|
public function addRenderer($sId, $xClosure) |
|
147
|
|
|
{ |
|
148
|
|
|
// Return the non-initialiazed view renderer |
|
149
|
|
|
jaxon()->di()->set('jaxon.app.view.base.' . $sId, $xClosure); |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
// Return the initialized view renderer |
|
152
|
|
|
jaxon()->di()->set('jaxon.app.view.' . $sId, function ($c) use ($sId) { |
|
|
|
|
|
|
153
|
|
|
// Get the defined renderer |
|
154
|
|
|
$renderer = $c['jaxon.app.view.base.' . $sId]; |
|
155
|
|
|
|
|
156
|
|
|
// Init the renderer with the template namespaces |
|
157
|
|
|
if(key_exists($sId, $this->aNamespaces)) |
|
158
|
|
|
{ |
|
159
|
|
|
foreach($this->aNamespaces[$sId] as $ns) |
|
160
|
|
|
{ |
|
161
|
|
|
$renderer->addNamespace($ns['namespace'], $ns['directory'], $ns['extension']); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
return $renderer; |
|
165
|
|
|
}); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.