1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\View; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A view collection supporting Anax DI, store all views per region, |
10
|
|
|
* render at will. |
11
|
|
|
*/ |
12
|
|
|
class ViewCollection implements |
13
|
|
|
ContainerInjectableInterface |
14
|
|
|
{ |
15
|
|
|
use ContainerInjectableTrait; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array $views container for all views. |
21
|
|
|
*/ |
22
|
|
|
private $views = []; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array $paths where to look for template files. |
28
|
|
|
* @var string $suffix add to each template file name. |
29
|
|
|
*/ |
30
|
|
|
private $paths = []; |
31
|
|
|
private $suffix = ".php"; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set paths to search through when looking for template files. |
37
|
|
|
* |
38
|
|
|
* @param array $paths with directories to search through. |
39
|
|
|
* |
40
|
|
|
* @return self |
41
|
|
|
*/ |
42
|
1 |
|
public function setPaths(array $paths) : object |
43
|
|
|
{ |
44
|
1 |
|
foreach ($paths as $path) { |
45
|
1 |
|
if (!(is_dir($path) && is_readable($path))) { |
46
|
1 |
|
throw new Exception("Directory '$path' is not readable."); |
47
|
|
|
} |
48
|
|
|
} |
49
|
1 |
|
$this->paths = $paths; |
50
|
1 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set suffix to add last to template file givven, as a filename extension. |
57
|
|
|
* |
58
|
|
|
* @param string $suffix to use as file extension. |
59
|
|
|
* |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
1 |
|
public function setSuffix(string $suffix) : object |
63
|
|
|
{ |
64
|
1 |
|
$this->suffix = $suffix; |
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Convert template to path to template file. |
72
|
|
|
* |
73
|
|
|
* @param string $template the name of the template file to include |
74
|
|
|
* |
75
|
|
|
* @throws Anax\View\Exception when template file is missing |
76
|
|
|
* |
77
|
|
|
* @return string as path to the template file |
78
|
|
|
*/ |
79
|
|
|
public function getTemplateFile($template) |
80
|
|
|
{ |
81
|
|
|
foreach ($this->paths as $path) { |
82
|
|
|
$file = $path . "/" . $template . $this->suffix; |
83
|
|
|
if (is_file($file)) { |
84
|
|
|
return $file; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
throw new Exception("Could not find template file '$template'."); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Add (create) a view to be included, pass optional data and put the |
95
|
|
|
* view in an optional specific region (default region is "main") and |
96
|
|
|
* pass an optional sort value where the highest value is rendered first. |
97
|
|
|
* The $template can be a: |
98
|
|
|
* filename (string), |
99
|
|
|
* callback (array with key callback set to a callable array), |
100
|
|
|
* view array (key value array with template, data, region, sort) |
101
|
|
|
* |
102
|
|
|
* @param array|string $template the name of the template file to include. |
103
|
|
|
* @param array $data variables to make available to the view, |
104
|
|
|
* default is empty. |
105
|
|
|
* @param string $region which region to attach the view, default |
106
|
|
|
* is "main". |
107
|
|
|
* @param integer $sort which order to display the views. |
108
|
|
|
* |
109
|
|
|
* @return self for chaining. |
110
|
|
|
*/ |
111
|
|
|
public function add( |
112
|
|
|
$template, |
113
|
|
|
array $data = [], |
114
|
|
|
string $region = "main", |
115
|
|
|
int $sort = 0 |
116
|
|
|
) : object |
117
|
|
|
{ |
118
|
|
|
if (empty($template)) { |
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$view = new View(); |
123
|
|
|
|
124
|
|
|
if (is_string($template)) { |
125
|
|
|
$tpl = $this->getTemplateFile($template); |
126
|
|
|
$type = "file"; |
127
|
|
|
} elseif (is_array($template)) { |
128
|
|
|
// Can be array with complete view or array with callable callback |
129
|
|
|
$tpl = $template; |
130
|
|
|
$type = null; |
131
|
|
|
$region = $tpl["region"] ?? $region; |
132
|
|
|
|
133
|
|
|
if (isset($tpl["callback"])) { |
134
|
|
|
$tpl["template"] = $template; |
135
|
|
|
$tpl["type"] = "callback"; |
136
|
|
|
} elseif (isset($tpl["template"])) { |
137
|
|
|
if (!isset($tpl["type"]) || $tpl["type"] === "file") { |
138
|
|
|
$tpl["type"] = "file"; |
139
|
|
|
$tpl["template"] = $this->getTemplateFile($tpl["template"]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$view->set($tpl, $data, $sort, $type); |
|
|
|
|
145
|
|
|
$this->views[$region][] = $view; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add a callback to be rendered as a view. |
154
|
|
|
* |
155
|
|
|
* @param string $callback function to call to get the content of the view |
156
|
|
|
* @param array $data variables to make available to the view, default is empty |
157
|
|
|
* @param string $region which region to attach the view |
158
|
|
|
* @param int $sort which order to display the views |
159
|
|
|
* |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function addCallback($callback, $data = [], $region = "main", $sort = 0) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$view = new View(); |
165
|
|
|
$view->set(["callback" => $callback], $data, $sort, "callback"); |
166
|
|
|
$this->views[$region][] = $view; |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Add a string as a view. |
175
|
|
|
* |
176
|
|
|
* @param string $content the content |
177
|
|
|
* @param string $region which region to attach the view |
178
|
|
|
* @param int $sort which order to display the views |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
public function addString($content, $region = "main", $sort = 0) |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$view = new View(); |
185
|
|
|
$view->set($content, [], $sort, "string"); |
186
|
|
|
$this->views[$region][] = $view; |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Check if a region has views to render. |
195
|
|
|
* |
196
|
|
|
* @param string $region which region to check |
197
|
|
|
* |
198
|
|
|
* @return $this |
199
|
|
|
*/ |
200
|
|
|
public function hasContent($region) |
201
|
|
|
{ |
202
|
|
|
return isset($this->views[$region]); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Render all views for a specific region. |
209
|
|
|
* |
210
|
|
|
* @param string $region which region to use |
211
|
|
|
* |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
|
public function render($region = "main") |
215
|
|
|
{ |
216
|
|
|
if (!isset($this->views[$region])) { |
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
mergesort($this->views[$region], function ($viewA, $viewB) { |
221
|
|
|
$sortA = $viewA->sortOrder(); |
222
|
|
|
$sortB = $viewB->sortOrder(); |
223
|
|
|
|
224
|
|
|
if ($sortA == $sortB) { |
225
|
|
|
return 0; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $sortA < $sortB ? -1 : 1; |
229
|
|
|
}); |
230
|
|
|
|
231
|
|
|
foreach ($this->views[$region] as $view) { |
232
|
|
|
$view->render($this->di); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Render all views for a specific region and buffer the result. |
239
|
|
|
* |
240
|
|
|
* @param string $region which region to use. |
241
|
|
|
* |
242
|
|
|
* @return string with the buffered results. |
243
|
|
|
*/ |
244
|
|
|
public function renderBuffered($region = "main") |
245
|
|
|
{ |
246
|
|
|
ob_start(); |
247
|
|
|
$this->render($region); |
248
|
|
|
$res = ob_get_contents(); |
249
|
|
|
ob_end_clean(); |
250
|
|
|
return $res; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: