1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Phauthentic\Presentation\Renderer; |
5
|
|
|
|
6
|
|
|
use Phauthentic\Presentation\Renderer\Exception\MissingTemplateException; |
7
|
|
|
use Phauthentic\Presentation\View\ViewInterface; |
8
|
|
|
use LightnCandy\LightnCandy; |
9
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* php Lightncandy template engine adapter |
13
|
|
|
* |
14
|
|
|
* @link https://github.com/zordius/lightncandy |
15
|
|
|
*/ |
16
|
|
|
class LightnCandyRenderer implements RendererInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Root folder for the template files |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $templateRoot; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Psr\Cache\CacheItemPoolInterface |
27
|
|
|
*/ |
28
|
|
|
protected $cache; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Flags |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $flags = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Helpers |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $helpers = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor |
46
|
|
|
* |
47
|
|
|
* @param string $templateRoot Template Root |
48
|
|
|
* @param|null \Psr\Cache\CacheItemPoolInterface $cacheItemPool PSR Cache Item Pool |
49
|
|
|
*/ |
50
|
1 |
|
public function __construct( |
51
|
|
|
string $templateRoot, |
52
|
|
|
?CacheItemPoolInterface $cacheItemPool |
53
|
|
|
) { |
54
|
1 |
|
$this->templateRoot = $templateRoot; |
55
|
1 |
|
$this->cache = $cacheItemPool; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set flags |
60
|
|
|
* |
61
|
|
|
* @param array $flags Flags |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function setFlags(array $flags): self |
65
|
|
|
{ |
66
|
|
|
$this->flags = $flags; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets the helpers |
71
|
|
|
* |
72
|
|
|
* @param array $helpers Helpers |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function setHelpers(array $helpers) |
76
|
|
|
{ |
77
|
|
|
$this->helpers = $helpers; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $name Name |
84
|
|
|
* @param mixed $helper |
85
|
|
|
* @return $this; |
86
|
|
|
*/ |
87
|
|
|
public function addHelper(string $name, $helper) |
88
|
|
|
{ |
89
|
|
|
$this->helpers[$name] = $helper; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets the template file from the view DTO object |
96
|
|
|
* |
97
|
|
|
* @param \Phauthentic\Presentation\Renderer\ViewInterface |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
1 |
|
public function getTemplateFile(ViewInterface $view): string |
101
|
|
|
{ |
102
|
1 |
|
$path = $view->getTemplatePath(); |
103
|
1 |
|
$path = Utility::sanitizePath($path); |
104
|
|
|
|
105
|
1 |
|
$template = $this->templateRoot . DIRECTORY_SEPARATOR . $path . $view->getTemplate() . '.html'; |
106
|
|
|
|
107
|
1 |
|
if (!is_file($template)) { |
108
|
|
|
throw new MissingTemplateException('Template file missing: ' . $template); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return $template; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
*/ |
117
|
1 |
|
public function renderTemplate($template, $viewVars): string |
118
|
|
|
{ |
119
|
1 |
|
$tmpDir = sys_get_temp_dir(); |
120
|
1 |
|
$templateHash = hash_file('sha1', $template); |
121
|
1 |
|
$cachedTemplateFile = $tmpDir . DIRECTORY_SEPARATOR . sha1($template) . '-' . $templateHash; |
122
|
|
|
|
123
|
1 |
|
if (!file_exists($cachedTemplateFile)) { |
124
|
1 |
|
$templateString = file_get_contents($template); |
125
|
1 |
|
$phpTemplateString = LightnCandy::compile($templateString, [ |
126
|
1 |
|
'flags' => $this->flags, |
127
|
1 |
|
'helpers' => $this->helpers |
128
|
|
|
]); |
129
|
1 |
|
file_put_contents($cachedTemplateFile, '<?php ' . $phpTemplateString . '?>'); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
ob_start(); |
133
|
1 |
|
$renderer = require $cachedTemplateFile; |
134
|
1 |
|
$content = $renderer($viewVars); |
135
|
1 |
|
ob_end_clean(); |
136
|
|
|
|
137
|
1 |
|
return $content; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritDoc |
142
|
|
|
*/ |
143
|
1 |
|
public function render(ViewInterface $view): string |
144
|
|
|
{ |
145
|
1 |
|
$template = $this->getTemplateFile($view); |
146
|
|
|
|
147
|
1 |
|
return $this->renderTemplate($template, $view->getViewVars()); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: