1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Plates\Template; |
4
|
|
|
|
5
|
|
|
use League\Plates\Engine; |
6
|
|
|
use LogicException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Container which holds template data and provides access to template functions. |
10
|
|
|
*/ |
11
|
|
|
class Template |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Instance of the template engine. |
15
|
|
|
* @var Engine |
16
|
|
|
*/ |
17
|
|
|
protected $engine; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The name of the template. |
21
|
|
|
* @var Name |
22
|
|
|
*/ |
23
|
|
|
protected $name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The data assigned to the template. |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $data = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* An array of section content. |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $sections = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The name of the template layout. |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $layoutName; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The data assigned to the template layout. |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $layoutData; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create new Template instance. |
51
|
|
|
* @param Engine $engine |
52
|
|
|
* @param string $name |
53
|
|
|
*/ |
54
|
100 |
|
public function __construct(Engine $engine, $name) |
55
|
|
|
{ |
56
|
100 |
|
$this->engine = $engine; |
57
|
100 |
|
$this->name = new Name($engine, $name); |
58
|
|
|
|
59
|
100 |
|
$this->data($this->engine->getData($name)); |
60
|
100 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Magic method used to call extension functions. |
64
|
|
|
* @param string $name |
65
|
|
|
* @param array $arguments |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
8 |
|
public function __call($name, $arguments) |
69
|
|
|
{ |
70
|
8 |
|
return $this->engine->getFunction($name)->call($this, $arguments); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Assign data to template object. |
75
|
|
|
* @param array $data |
76
|
|
|
* @return null |
77
|
|
|
*/ |
78
|
100 |
|
public function data(array $data) |
79
|
|
|
{ |
80
|
100 |
|
$this->data = array_merge($this->data, $data); |
81
|
100 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check if the template exists. |
85
|
|
|
* @return boolean |
86
|
|
|
*/ |
87
|
88 |
|
public function exists() |
88
|
|
|
{ |
89
|
88 |
|
return $this->name->doesPathExist(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the template path. |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
84 |
|
public function path() |
97
|
|
|
{ |
98
|
84 |
|
return $this->name->getPath(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Render the template and layout. |
103
|
|
|
* @param array $data |
104
|
|
|
* @throws \Throwable |
105
|
|
|
* @throws \Exception |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
80 |
|
public function render(array $data = array()) |
109
|
|
|
{ |
110
|
|
|
try { |
111
|
80 |
|
$this->data($data); |
112
|
|
|
|
113
|
80 |
|
unset($data); |
114
|
|
|
|
115
|
80 |
|
extract($this->data); |
116
|
|
|
|
117
|
80 |
|
ob_start(); |
118
|
|
|
|
119
|
80 |
|
if (!$this->exists()) { |
120
|
4 |
|
throw new LogicException( |
121
|
4 |
|
'The template "' . $this->name->getName() . '" could not be found at "' . $this->path() . '".' |
122
|
4 |
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
76 |
|
include $this->path(); |
126
|
|
|
|
127
|
60 |
|
$content = ob_get_clean(); |
128
|
|
|
|
129
|
60 |
|
if (isset($this->layoutName)) { |
130
|
12 |
|
$layout = $this->engine->make($this->layoutName); |
131
|
12 |
|
$layout->sections = array_merge($this->sections, array('content' => $content)); |
132
|
12 |
|
$content = $layout->render($this->layoutData); |
133
|
12 |
|
} |
134
|
|
|
|
135
|
60 |
|
return $content; |
136
|
20 |
|
} catch (\Throwable $e) { |
|
|
|
|
137
|
|
|
$this->cleanOutputBuffer($e); |
138
|
20 |
|
} catch (\Exception $e) { |
139
|
20 |
|
$this->cleanOutputBuffer($e); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param \Throwable|\Exception $e |
145
|
|
|
* @throws \Throwable |
146
|
|
|
* @throws \Exception |
147
|
|
|
*/ |
148
|
20 |
|
private function cleanOutputBuffer($e) |
149
|
|
|
{ |
150
|
20 |
|
ob_end_clean(); |
151
|
20 |
|
throw $e; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the template's layout. |
156
|
|
|
* @param string $name |
157
|
|
|
* @param array $data |
158
|
|
|
* @return null |
159
|
|
|
*/ |
160
|
12 |
|
protected function layout($name, array $data = array()) |
161
|
|
|
{ |
162
|
12 |
|
$this->layoutName = $name; |
163
|
12 |
|
$this->layoutData = $data; |
164
|
12 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Start a new section block. |
168
|
|
|
* @param string $name |
169
|
|
|
* @return null |
170
|
|
|
*/ |
171
|
8 |
|
protected function start($name) |
172
|
|
|
{ |
173
|
8 |
|
if ($name === 'content') { |
174
|
4 |
|
throw new LogicException( |
175
|
|
|
'The section name "content" is reserved.' |
176
|
4 |
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
4 |
|
$this->sections[$name] = ''; |
180
|
|
|
|
181
|
4 |
|
ob_start(); |
182
|
4 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Stop the current section block. |
186
|
|
|
* @return null |
187
|
|
|
*/ |
188
|
8 |
|
protected function stop() |
189
|
|
|
{ |
190
|
8 |
|
if (empty($this->sections)) { |
191
|
4 |
|
throw new LogicException( |
192
|
|
|
'You must start a section before you can stop it.' |
193
|
4 |
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
4 |
|
end($this->sections); |
197
|
|
|
|
198
|
4 |
|
$this->sections[key($this->sections)] = ob_get_clean(); |
199
|
4 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns the content for a section block. |
203
|
|
|
* @param string $name Section name |
204
|
|
|
* @param string $default Default section content |
205
|
|
|
* @return string|null |
206
|
|
|
*/ |
207
|
12 |
|
protected function section($name, $default = null) |
208
|
|
|
{ |
209
|
12 |
|
if (!isset($this->sections[$name])) { |
210
|
8 |
|
return $default; |
211
|
|
|
} |
212
|
|
|
|
213
|
4 |
|
return $this->sections[$name]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Fetch a rendered template. |
218
|
|
|
* @param string $name |
219
|
|
|
* @param array $data |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
4 |
|
protected function fetch($name, array $data = array()) |
223
|
|
|
{ |
224
|
4 |
|
return $this->engine->render($name, $data); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Output a rendered template. |
229
|
|
|
* @param string $name |
230
|
|
|
* @param array $data |
231
|
|
|
* @return null |
232
|
|
|
*/ |
233
|
4 |
|
protected function insert($name, array $data = array()) |
234
|
|
|
{ |
235
|
4 |
|
echo $this->engine->render($name, $data); |
236
|
4 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Apply multiple functions to variable. |
240
|
|
|
* @param mixed $var |
241
|
|
|
* @param string $functions |
242
|
|
|
* @return mixed |
243
|
|
|
*/ |
244
|
12 |
|
protected function batch($var, $functions) |
245
|
|
|
{ |
246
|
12 |
|
foreach (explode('|', $functions) as $function) { |
247
|
12 |
|
if ($this->engine->doesFunctionExist($function)) { |
248
|
4 |
|
$var = call_user_func(array($this, $function), $var); |
249
|
12 |
|
} elseif (is_callable($function)) { |
250
|
8 |
|
$var = call_user_func($function, $var); |
251
|
8 |
|
} else { |
252
|
4 |
|
throw new LogicException( |
253
|
4 |
|
'The batch function could not find the "' . $function . '" function.' |
254
|
4 |
|
); |
255
|
|
|
} |
256
|
8 |
|
} |
257
|
|
|
|
258
|
8 |
|
return $var; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Escape string. |
263
|
|
|
* @param string $string |
264
|
|
|
* @param null|string $functions |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
12 |
|
protected function escape($string, $functions = null) |
268
|
|
|
{ |
269
|
12 |
|
static $flags; |
270
|
|
|
|
271
|
12 |
|
if (!isset($flags)) { |
272
|
4 |
|
$flags = ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0); |
273
|
4 |
|
} |
274
|
|
|
|
275
|
12 |
|
if ($functions) { |
276
|
4 |
|
$string = $this->batch($string, $functions); |
277
|
4 |
|
} |
278
|
|
|
|
279
|
12 |
|
return htmlspecialchars($string, $flags, 'UTF-8'); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Alias to escape function. |
284
|
|
|
* @param string $string |
285
|
|
|
* @param null|string $functions |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
4 |
|
protected function e($string, $functions = null) |
289
|
|
|
{ |
290
|
4 |
|
return $this->escape($string, $functions); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.