1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Patternseek ComponentView library. |
4
|
|
|
* |
5
|
|
|
* (c)2016 Tolan Blundell <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PatternSeek\ComponentView\Template; |
11
|
|
|
|
12
|
|
|
use PatternSeek\ComponentView\AbstractViewComponent; |
13
|
|
|
use PatternSeek\ComponentView\Response; |
14
|
|
|
use PatternSeek\ComponentView\ViewState\ViewState; |
15
|
|
|
use Puli\Repository\Api\ResourceRepository; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This class allows the use of PHP files as templates. |
19
|
|
|
* Twig templates are generally preferred but in certain |
20
|
|
|
* cases such as porting legacy code a PHP template may |
21
|
|
|
* save a lot of work. |
22
|
|
|
* |
23
|
|
|
* Class PhpTemplate |
24
|
|
|
* @package PatternSeek\ComponentView |
25
|
|
|
*/ |
26
|
|
|
class PhpTemplate extends AbstractTemplate |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string A PHP template path |
31
|
|
|
*/ |
32
|
|
|
protected $templatePath; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string A PHP template string |
36
|
|
|
*/ |
37
|
|
|
protected $templateString; |
38
|
|
|
/** |
39
|
|
|
* @var ResourceRepository |
40
|
|
|
*/ |
41
|
|
|
protected $repo; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param AbstractViewComponent $component |
45
|
|
|
* @param string $templatePath |
46
|
|
|
* @param null $templateString |
47
|
|
|
* @param ResourceRepository $repo |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
AbstractViewComponent $component, |
51
|
|
|
$templatePath = null, |
52
|
|
|
$templateString = null, |
53
|
|
|
ResourceRepository $repo = null |
54
|
|
|
){ |
55
|
|
|
parent::__construct( $component ); |
56
|
|
|
$this->templatePath = $templatePath; |
57
|
|
|
$this->templateString = $templateString; |
58
|
|
|
// Optional Puli repo |
59
|
|
|
$this->repo = $repo; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ViewState $state |
64
|
|
|
* @param array $props |
65
|
|
|
* @return Response |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
|
|
protected function doRender( ViewState $state, array $props = [ ] ) |
69
|
|
|
{ |
70
|
|
|
// Available variables in template file are: |
71
|
|
|
// $state |
72
|
|
|
// $props |
73
|
|
|
// $thisComponent (equivalent of 'this' in TwigTemplates) |
74
|
|
|
// $parent |
75
|
|
|
// $exec |
76
|
|
|
|
77
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */ |
78
|
|
|
$thisComponent = $this->component; |
79
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */ |
80
|
|
|
$parent = $this->component->getParent(); |
81
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */ |
82
|
|
|
$exec = $this->component->exec; |
83
|
|
|
if ($this->templatePath) { |
84
|
|
|
$realPath = $this->getRealTemplatePath( $this->templatePath ); |
85
|
|
|
ob_start(); |
86
|
|
|
/** @noinspection PhpIncludeInspection */ |
87
|
|
|
include( $realPath ); |
88
|
|
|
$rendered = ob_get_clean(); |
89
|
|
|
}elseif ($this->templateString) { |
90
|
|
|
ob_start(); |
91
|
|
|
eval( "?>" . $this->templateString ); |
|
|
|
|
92
|
|
|
$rendered = ob_get_clean(); |
93
|
|
|
}else { |
94
|
|
|
throw new \Exception( "Neither valid template path no valid template string passed to PhpTemplate." ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return new Response( "text/html", $rendered ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $templatePath |
102
|
|
|
* @return null|string |
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
|
|
private function getRealTemplatePath( $templatePath ) |
106
|
|
|
{ |
107
|
|
|
if ($this->repo instanceof ResourceRepository) { |
|
|
|
|
108
|
|
|
if ($this->repo->contains( $templatePath )) { |
109
|
|
|
return $this->repo->get( $templatePath ) |
110
|
|
|
->getPath(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
if (file_exists( $templatePath )) { |
114
|
|
|
return $templatePath; |
115
|
|
|
} |
116
|
|
|
throw new \Exception( "Template path not found in repository or on filesystem: {$templatePath}, in PhpTemplate" ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
On one hand,
eval
might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM,eval
prevents some optimization that they perform.