|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Plates\RenderTemplate; |
|
4
|
|
|
|
|
5
|
|
|
use League\Plates; |
|
6
|
|
|
|
|
7
|
|
|
final class PlatesRenderTemplate implements Plates\RenderTemplate |
|
8
|
|
|
{ |
|
9
|
|
|
private $resolve_name; |
|
10
|
|
|
private $resolve_data; |
|
11
|
|
|
private $include; |
|
12
|
|
|
private $create_render_context; |
|
13
|
|
|
private $render_context_var_name; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct( |
|
16
|
|
|
$resolve_name = null, |
|
17
|
|
|
$resolve_data = null, |
|
18
|
|
|
$include = null, |
|
19
|
|
|
$create_render_context = null, |
|
20
|
|
|
$render_context_var_name = 'v' |
|
21
|
|
|
) { |
|
22
|
|
|
$this->resolve_name = $resolve_name ?: Plates\Util\id(true); |
|
23
|
|
|
$this->resolve_data = $resolve_data ?: Plates\Util\id(true); |
|
24
|
|
|
$this->include = $include ?: Plates\Template\phpInclude(); |
|
25
|
|
|
$this->create_render_context = $create_render_context ?: function($render, $template) { |
|
26
|
|
|
return (object) [ |
|
27
|
|
|
'render' => $render, |
|
28
|
|
|
'template' => $template |
|
29
|
|
|
]; |
|
30
|
|
|
}; |
|
31
|
|
|
$this->render_context_var_name = $render_context_var_name; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function renderTemplate(Plates\Template $template) { |
|
35
|
|
|
$resolve_name = $this->resolve_name; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
list($path) = $template->resolveName($this->resolve_name); |
|
38
|
|
|
list($data) = $template->resolveData($this->resolve_data); |
|
39
|
|
|
|
|
40
|
|
|
$template->addContext([ |
|
41
|
|
|
'path' => $path, |
|
42
|
|
|
'current_directory' => basename($path) |
|
43
|
|
|
]); |
|
44
|
|
|
$ctx = call_user_func($this->create_render_context, $this, $template); |
|
45
|
|
|
|
|
46
|
|
|
if (array_key_exists($this->render_context_var_name, $data)) { |
|
47
|
|
|
throw new Plates\Exception\PlatesException('Cannot set render context because a variable already exists as ' . $this->render_context_var_name); |
|
48
|
|
|
} |
|
49
|
|
|
$data[$this->render_context_var_name] = $ctx; |
|
50
|
|
|
|
|
51
|
|
|
// this is done for BC reasons} |
|
52
|
|
|
$include = $this->include->bindTo($ctx); |
|
53
|
|
|
|
|
54
|
|
|
$contents = $include($path, $data); |
|
55
|
|
|
|
|
56
|
|
|
return $contents; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.