1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Biurad opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.2 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Biurad\UI\Renders; |
19
|
|
|
|
20
|
|
|
use Biurad\UI\Interfaces\CacheInterface; |
21
|
|
|
use Biurad\UI\Interfaces\TemplateInterface; |
22
|
|
|
use Biurad\UI\Interfaces\RenderInterface; |
23
|
|
|
use Latte; |
24
|
|
|
use Latte\Loaders\FileLoader; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Render for Latte templating. |
28
|
|
|
* |
29
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
final class LatteRender extends AbstractRender implements CacheInterface |
32
|
|
|
{ |
33
|
|
|
protected const EXTENSIONS = ['latte']; |
34
|
|
|
|
35
|
|
|
protected const DEFAULT_TEMPLATE = 'hello.latte'; |
36
|
|
|
|
37
|
|
|
/** @var Latte\Engine */ |
38
|
|
|
protected $latte; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* LatteEngine constructor. |
42
|
|
|
* |
43
|
|
|
* @param string[] $extensions |
44
|
|
|
*/ |
45
|
4 |
|
public function __construct(Latte\Engine $engine = null, array $extensions = self::EXTENSIONS) |
46
|
|
|
{ |
47
|
4 |
|
$this->latte = $engine ?? new Latte\Engine(); |
48
|
4 |
|
$this->extensions = $extensions; |
49
|
4 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
4 |
|
public function withCache(?string $cacheDir): void |
55
|
|
|
{ |
56
|
4 |
|
if (null !== $cacheDir) { |
57
|
3 |
|
$this->latte->setTempDirectory($cacheDir); // Replace regardless ... |
58
|
|
|
} |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
4 |
|
public function withLoader(TemplateInterface $loader): RenderInterface |
65
|
|
|
{ |
66
|
4 |
|
$this->latte->addFunction('template', static function (string $template, array $parameters = []) use ($loader): string { |
67
|
3 |
|
return $loader->render($template, $parameters); |
68
|
4 |
|
}); |
69
|
|
|
|
70
|
4 |
|
return parent::withLoader($loader); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
4 |
|
public function render(string $template, array $parameters): string |
77
|
|
|
{ |
78
|
4 |
|
if (\file_exists($template)) { |
79
|
4 |
|
$templateLoader = new FileLoader(); |
80
|
|
|
} else { |
81
|
3 |
|
$templateId = \substr(\md5($template), 0, 7); |
82
|
3 |
|
$templateLoader = new StringLoader([$templateId => (\file_exists($this->latte->getCacheFile($templateId)) ? '' : self::loadHtml($template) ?? $template)]); |
83
|
|
|
|
84
|
3 |
|
$template = $templateId; |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
$this->latte->setLoader($templateLoader); |
88
|
|
|
|
89
|
4 |
|
return $this->latte->renderToString($template, $parameters); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Registers run-time filter. |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function addFilter(?string $name, callable $callback) |
98
|
|
|
{ |
99
|
|
|
$this->latte->addFilter($name, $callback); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Adds new macro. |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function addMacro(string $name, Latte\Macro $macro) |
110
|
|
|
{ |
111
|
|
|
$this->latte->addMacro($name, $macro); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Registers run-time function. |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function addFunction(string $name, callable $callback) |
122
|
|
|
{ |
123
|
|
|
$this->latte->addFunction($name, $callback); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Adds new provider. |
130
|
|
|
* |
131
|
|
|
* @param mixed $value |
132
|
|
|
* |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function addProvider(string $name, $value) |
136
|
|
|
{ |
137
|
|
|
$this->latte->addProvider($name, $value); |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function setPolicy(?Latte\Policy $policy) |
146
|
|
|
{ |
147
|
|
|
$this->latte->setPolicy($policy); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
|
|
public function setExceptionHandler(callable $callback) |
156
|
|
|
{ |
157
|
|
|
$this->latte->setExceptionHandler($callback); |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Latte Template loader. |
165
|
|
|
*/ |
166
|
|
|
class StringLoader extends \Latte\Loaders\StringLoader |
167
|
|
|
{ |
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
* |
171
|
|
|
* @param string $name |
172
|
|
|
*/ |
173
|
3 |
|
public function getUniqueId($name): string |
174
|
|
|
{ |
175
|
3 |
|
return $name; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|