1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Faulancer\View; |
4
|
|
|
|
5
|
|
|
use Faulancer\Event\Observer; |
6
|
|
|
use Faulancer\Event\Type\OnPostRender; |
7
|
|
|
use Faulancer\Event\Type\OnRender; |
8
|
|
|
use Faulancer\Exception\Exception; |
9
|
|
|
use Faulancer\Exception\FileNotFoundException; |
10
|
|
|
use Faulancer\Exception\ServiceNotFoundException; |
11
|
|
|
use Faulancer\Exception\TemplateException; |
12
|
|
|
use Faulancer\Exception\ViewHelperException; |
13
|
|
|
use Faulancer\Service\Config; |
14
|
|
|
use Faulancer\ServiceLocator\ServiceLocator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ViewController | ViewController.php |
18
|
|
|
* |
19
|
|
|
* @package Faulancer\View |
20
|
|
|
* @author Florian Knapp <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class ViewController |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Config |
27
|
|
|
*/ |
28
|
|
|
private $config; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Holds the view variables |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $variable = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Holds the view template |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $template = ''; |
41
|
|
|
|
42
|
|
|
/**The template path without filename |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $templatePath = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Holds the registered view helpers |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
//private $viewHelpers = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Holds the parent template |
55
|
|
|
* @var ViewController |
56
|
|
|
*/ |
57
|
|
|
private $parentTemplate = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* ViewController constructor. |
61
|
|
|
*/ |
62
|
|
|
public function __construct() |
63
|
|
|
{ |
64
|
|
|
$this->config = ServiceLocator::instance()->get(Config::class); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set template for this view |
69
|
|
|
* |
70
|
|
|
* @param string $template |
71
|
|
|
* @return self |
72
|
|
|
* |
73
|
|
|
* @throws FileNotFoundException |
74
|
|
|
*/ |
75
|
|
|
public function setTemplate(string $template = '') |
76
|
|
|
{ |
77
|
|
|
if (empty($this->templatePath) && strpos($template, $this->config->get('viewsRoot')) === false) { |
78
|
|
|
$template = $this->config->get('viewsRoot') . $template; |
79
|
|
|
} else { |
80
|
|
|
$template = $this->templatePath . $template; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (empty($template) || !file_exists($template) || is_dir($template)) { |
84
|
|
|
throw new FileNotFoundException('Template "' . $template . '" not found'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->template = $template; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the template path |
94
|
|
|
* |
95
|
|
|
* @param string $path |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
|
|
public function setTemplatePath(string $path = '') |
99
|
|
|
{ |
100
|
|
|
$this->templatePath = $path; |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the template path |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getTemplatePath() |
110
|
|
|
{ |
111
|
|
|
return $this->templatePath; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Add javascript from outside |
116
|
|
|
* |
117
|
|
|
* @param string $file |
118
|
|
|
* @return self |
119
|
|
|
*/ |
120
|
|
|
public function addScript(string $file) |
121
|
|
|
{ |
122
|
|
|
$this->variable['assetsJs'][] = $file; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Add stylesheet from outside |
128
|
|
|
* |
129
|
|
|
* @param string $file |
130
|
|
|
* @return self |
131
|
|
|
*/ |
132
|
|
|
public function addStylesheet(string $file) |
133
|
|
|
{ |
134
|
|
|
$this->variable['assetsCss'][] = $file; |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Return current template |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getTemplate() :string |
144
|
|
|
{ |
145
|
|
|
return (string)$this->template; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set a single variable |
150
|
|
|
* |
151
|
|
|
* @param string $key |
152
|
|
|
* @param string|array $value |
153
|
|
|
*/ |
154
|
|
|
public function setVariable(string $key = '', $value = '') |
155
|
|
|
{ |
156
|
|
|
$this->variable[$key] = $value; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get a single variable |
161
|
|
|
* |
162
|
|
|
* @param string $key |
163
|
|
|
* @return string|array |
164
|
|
|
*/ |
165
|
|
|
public function getVariable(string $key) |
166
|
|
|
{ |
167
|
|
|
if(isset($this->variable[$key])) { |
168
|
|
|
return $this->variable[$key]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return ''; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Check if variable exists |
176
|
|
|
* |
177
|
|
|
* @param string $key |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function hasVariable(string $key) :bool |
181
|
|
|
{ |
182
|
|
|
if(isset($this->variable[$key])) { |
183
|
|
|
return true; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set many variables at once |
191
|
|
|
* |
192
|
|
|
* @param array $variables |
193
|
|
|
* @return self |
194
|
|
|
*/ |
195
|
|
|
public function setVariables(array $variables = []) |
196
|
|
|
{ |
197
|
|
|
foreach($variables AS $key=>$value) { |
198
|
|
|
$this->setVariable($key, $value); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get all variables |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function getVariables() :array |
210
|
|
|
{ |
211
|
|
|
return $this->variable; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Define parent template |
216
|
|
|
* |
217
|
|
|
* @param ViewController $view |
218
|
|
|
*/ |
219
|
|
|
public function setParentTemplate(ViewController $view) |
220
|
|
|
{ |
221
|
|
|
$this->parentTemplate = $view; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get parent template |
226
|
|
|
* |
227
|
|
|
* @return ViewController |
228
|
|
|
*/ |
229
|
|
|
public function getParentTemplate() |
230
|
|
|
{ |
231
|
|
|
return $this->parentTemplate; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Strip spaces and tabs from output |
236
|
|
|
* |
237
|
|
|
* @param $output |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
|
|
private function _cleanOutput($output) :string |
241
|
|
|
{ |
242
|
|
|
if (getenv('APPLICATION_ENV') === 'prod') { |
243
|
|
|
return preg_replace('/(\s{2,}|\t|\r|\n)/', ' ', trim($output)); |
244
|
|
|
} else { |
245
|
|
|
return str_replace(["\t", "\r", "\n\n\n"], ' ', trim($output)); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Render the current view |
251
|
|
|
* |
252
|
|
|
* @return string |
253
|
|
|
* @throws ServiceNotFoundException |
254
|
|
|
* @throws Exception |
255
|
|
|
* @throws TemplateException |
256
|
|
|
*/ |
257
|
|
|
public function render() |
258
|
|
|
{ |
259
|
|
|
Observer::instance()->trigger(new OnRender($this)); |
260
|
|
|
|
261
|
|
|
extract($this->variable); |
262
|
|
|
|
263
|
|
|
try { |
264
|
|
|
|
265
|
|
|
ob_start(); |
266
|
|
|
|
267
|
|
|
include $this->getTemplate(); |
268
|
|
|
$content = ob_get_contents(); |
269
|
|
|
|
270
|
|
|
} catch (\Exception $e) { |
271
|
|
|
ob_end_clean(); |
272
|
|
|
throw new TemplateException($e->getMessage(), $e->getCode(), $e->getFile(), $e->getLine(), $e->getPrevious()); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
if (ob_get_length() >= 0) { |
276
|
|
|
ob_end_clean(); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
Observer::instance()->trigger(new OnPostRender($this)); |
280
|
|
|
|
281
|
|
|
if ($this->getParentTemplate() instanceof ViewController) { |
282
|
|
|
return $this->_cleanOutput($this->getParentTemplate()->setVariables($this->getVariables())->render()); |
283
|
|
|
} else { |
284
|
|
|
return $this->_cleanOutput($content); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Magic method for providing a view helpers |
290
|
|
|
* |
291
|
|
|
* @param string $name The class name |
292
|
|
|
* @param array $arguments Arguments if given |
293
|
|
|
* |
294
|
|
|
* @return AbstractViewHelper |
295
|
|
|
* |
296
|
|
|
* @throws ViewHelperException |
297
|
|
|
*/ |
298
|
|
|
public function __call($name, $arguments) |
299
|
|
|
{ |
300
|
|
|
$coreViewHelper = __NAMESPACE__ . '\Helper\\' . ucfirst($name); |
301
|
|
|
|
302
|
|
|
/** @var Config $config */ |
303
|
|
|
$config = ServiceLocator::instance()->get(Config::class); |
304
|
|
|
$namespace = '\\' . $config->get('namespacePrefix'); |
305
|
|
|
$customViewHelper = $namespace . '\\View\\Helper\\' . ucfirst($name); |
306
|
|
|
|
307
|
|
|
// Search in custom view helpers |
308
|
|
|
|
309
|
|
View Code Duplication |
if (class_exists($customViewHelper)) { |
|
|
|
|
310
|
|
|
|
311
|
|
|
/** @var AbstractViewHelper $class */ |
312
|
|
|
$class = new $customViewHelper; |
313
|
|
|
$class->setView($this); |
314
|
|
|
|
315
|
|
|
return $this->_callUserFuncArray($class, $arguments); |
316
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
// No custom view helper found, search in core view helpers |
320
|
|
|
|
321
|
|
View Code Duplication |
if (class_exists($coreViewHelper)) { |
|
|
|
|
322
|
|
|
|
323
|
|
|
/** @var AbstractViewHelper $class */ |
324
|
|
|
$class = new $coreViewHelper; |
325
|
|
|
$class->setView($this); |
326
|
|
|
|
327
|
|
|
return $this->_callUserFuncArray($class, $arguments); |
328
|
|
|
|
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
throw new ViewHelperException('No view helper for "' . $name . '" found.'); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Abstraction of call_user_func_array |
336
|
|
|
* |
337
|
|
|
* @param $class |
338
|
|
|
* @param $arguments |
339
|
|
|
* |
340
|
|
|
* @return mixed |
341
|
|
|
*/ |
342
|
|
|
private function _callUserFuncArray($class, $arguments) |
343
|
|
|
{ |
344
|
|
|
return call_user_func_array($class, $arguments); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Destructor |
349
|
|
|
*/ |
350
|
|
|
public function __destruct() |
351
|
|
|
{ |
352
|
|
|
unset( $this->variable ); |
353
|
|
|
unset( $this->template ); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..