1
|
|
|
<?php |
2
|
|
|
namespace Tuum\View; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Renderer |
6
|
|
|
* |
7
|
|
|
* @package Tuum\View |
8
|
|
|
* |
9
|
|
|
* @property Section $section |
10
|
|
|
*/ |
11
|
|
|
class Renderer |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* do not render section if the section has this value. |
15
|
|
|
*/ |
16
|
|
|
const NO_SECTION_RENDER = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var LocatorInterface |
20
|
|
|
*/ |
21
|
|
|
private $locator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Section |
25
|
|
|
*/ |
26
|
|
|
private $section; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $services = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $view_file = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $view_extension = 'php'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $view_data = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $layout_file = null; |
52
|
|
|
|
53
|
|
|
// +----------------------------------------------------------------------+ |
54
|
|
|
// construction |
55
|
|
|
// +----------------------------------------------------------------------+ |
56
|
|
|
/** |
57
|
|
|
* @param LocatorInterface $locator |
58
|
|
|
* @param null|Section $section |
59
|
|
|
*/ |
60
|
|
|
public function __construct($locator, $section=null) |
61
|
|
|
{ |
62
|
|
|
$this->locator = $locator; |
63
|
|
|
$this->section = $section ?: new Section(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $view_dir |
68
|
|
|
* @return Renderer |
69
|
|
|
*/ |
70
|
|
|
public static function forge($view_dir) |
71
|
|
|
{ |
72
|
|
|
return new self(new Locator($view_dir), new Section()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $file |
77
|
|
|
* @param array $data |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setLayout($file, array $data = []) |
81
|
|
|
{ |
82
|
|
|
$this->layout_file = $file; |
83
|
|
|
$this->view_data = array_merge($this->view_data, $data); |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $dir |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setRoot($dir) |
92
|
|
|
{ |
93
|
|
|
$this->locator->addRoot($dir); |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $ext |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setFileExtension($ext) |
102
|
|
|
{ |
103
|
|
|
$this->view_extension = $ext; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $file |
109
|
|
|
* @return bool|string |
110
|
|
|
*/ |
111
|
|
|
public function getPath($file) |
112
|
|
|
{ |
113
|
|
|
return $this->locator->locate($file . '.' . $this->view_extension); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $name |
118
|
|
|
* @param mixed $service |
119
|
|
|
*/ |
120
|
|
|
public function register($name, $service) |
121
|
|
|
{ |
122
|
|
|
$this->services[$name] = $service; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $name |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
public function service($name) |
130
|
|
|
{ |
131
|
|
|
if (!array_key_exists($name, $this->services)) { |
132
|
|
|
throw new \RuntimeException('no such service in Renderer: ' . $name); |
133
|
|
|
} |
134
|
|
|
return $this->services[$name]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $name |
139
|
|
|
* @return mixed|null |
140
|
|
|
*/ |
141
|
|
|
public function __get($name) |
142
|
|
|
{ |
143
|
|
|
if ($name === 'section') return $this->section; |
144
|
|
|
return $this->service($name); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// +----------------------------------------------------------------------+ |
148
|
|
|
// rendering a view file. |
149
|
|
|
// +----------------------------------------------------------------------+ |
150
|
|
|
/** |
151
|
|
|
* render a block, without default layout. |
152
|
|
|
* |
153
|
|
|
* @param string $file |
154
|
|
|
* @param array $data |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function block($file, $data = []) |
158
|
|
|
{ |
159
|
|
|
$block = clone($this); |
160
|
|
|
$block->layout_file = null; |
161
|
|
|
return $block->doRender($file, $data); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $file |
166
|
|
|
* @param string $section |
167
|
|
|
* @param array $data |
168
|
|
|
*/ |
169
|
|
|
public function blockAsSection($file, $section, $data = []) |
170
|
|
|
{ |
171
|
|
|
$this->section->set($section, $this->block($file, $data)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* a simple renderer for a raw PHP file. |
176
|
|
|
* non-polluting execution when rendering a view file. |
177
|
|
|
* |
178
|
|
|
* @param string|array $file |
179
|
|
|
* @param array $data |
180
|
|
|
* @return string |
181
|
|
|
* @throws \Exception |
182
|
|
|
*/ |
183
|
|
|
public function render($file, $data = []) |
184
|
|
|
{ |
185
|
|
|
$viewer = clone($this); |
186
|
|
|
return $viewer->doRender($file, $data); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* a simple renderer for a raw PHP file. |
191
|
|
|
* |
192
|
|
|
* @param string|array $file |
193
|
|
|
* @param array $data |
194
|
|
|
* @return string |
195
|
|
|
* @throws \Exception |
196
|
|
|
*/ |
197
|
|
|
private function doRender($file, $data) |
198
|
|
|
{ |
199
|
|
|
$this->view_data = array_merge($this->view_data, $data); |
200
|
|
|
if (is_array($file)) { |
201
|
|
|
foreach($file as $key => $val) { |
202
|
|
|
$this->section->set($key, $val); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
elseif(!$this->view_file = $this->getPath($file)) { |
|
|
|
|
206
|
|
|
return null; |
207
|
|
|
} |
208
|
|
|
else { |
209
|
|
|
$this->setContent($this->renderViewFile()); |
210
|
|
|
} |
211
|
|
|
if (!isset($this->layout_file)) { |
212
|
|
|
return $this->section->get('content'); |
213
|
|
|
} |
214
|
|
|
$layout = clone($this); |
215
|
|
|
$layout->layout_file = null; |
216
|
|
|
return $layout->doRender($this->layout_file, $this->view_data); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* a simple renderer for a raw PHP file. |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
* @throws \Exception |
224
|
|
|
*/ |
225
|
|
|
private function renderViewFile() |
226
|
|
|
{ |
227
|
|
|
try { |
228
|
|
|
|
229
|
|
|
ob_start(); |
230
|
|
|
extract($this->view_data); |
231
|
|
|
|
232
|
|
|
/** @noinspection PhpIncludeInspection */ |
233
|
|
|
include($this->view_file); |
234
|
|
|
|
235
|
|
|
return trim(ob_get_clean(), "\n"); |
236
|
|
|
|
237
|
|
|
} catch (\Exception $e) { |
238
|
|
|
|
239
|
|
|
ob_end_clean(); |
240
|
|
|
throw $e; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
public function getContent() |
248
|
|
|
{ |
249
|
|
|
return $this->section->get('content'); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $content |
254
|
|
|
*/ |
255
|
|
|
public function setContent($content) |
256
|
|
|
{ |
257
|
|
|
$this->section->set('content', $content); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* should not share section. |
262
|
|
|
*/ |
263
|
|
|
public function __clone() |
264
|
|
|
{ |
265
|
|
|
$this->section = clone $this->section; |
266
|
|
|
} |
267
|
|
|
// +----------------------------------------------------------------------+ |
268
|
|
|
} |
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
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. 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.