|
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\RenderInterface; |
|
22
|
|
|
use Biurad\UI\Template; |
|
23
|
|
|
use Latte; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Render for Latte templating. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
final class LatteRender extends AbstractRender implements CacheInterface |
|
31
|
|
|
{ |
|
32
|
|
|
protected const EXTENSIONS = ['latte']; |
|
33
|
|
|
|
|
34
|
|
|
/** @var Latte\Engine */ |
|
35
|
|
|
protected $latte; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* LatteEngine constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string[] $extensions |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function __construct(Latte\Engine $engine = null, array $extensions = self::EXTENSIONS) |
|
43
|
|
|
{ |
|
44
|
4 |
|
$this->latte = $engine ?? new Latte\Engine(); |
|
45
|
4 |
|
$this->extensions = $extensions; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
4 |
|
public function withCache(?string $cacheDir): void |
|
52
|
|
|
{ |
|
53
|
4 |
|
if (null !== $cacheDir) { |
|
54
|
3 |
|
$this->latte->setTempDirectory($cacheDir); // Replace regardless ... |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
4 |
|
public function withLoader(Template $loader): RenderInterface |
|
62
|
|
|
{ |
|
63
|
4 |
|
$this->latte->addFilter('template', [$loader, 'find'])->addFunction('template', [$loader, 'render']); |
|
64
|
|
|
|
|
65
|
4 |
|
return parent::withLoader($loader); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public function render(string $template, array $parameters): string |
|
72
|
|
|
{ |
|
73
|
4 |
|
$source = self::loadHtml($template) ?? $template; |
|
74
|
|
|
|
|
75
|
4 |
|
if ($source !== $template || !\file_exists($template)) { |
|
76
|
3 |
|
$templateLoader = new Latte\Loaders\StringLoader([$template => $source]); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
$latte = $this->latte->setLoader($templateLoader ?? new class($this->loader) extends Latte\Loaders\FileLoader { |
|
|
|
|
|
|
80
|
|
|
/** @var Template */ |
|
81
|
|
|
private $loader; |
|
82
|
|
|
|
|
83
|
|
|
public function __construct(Template $loader, ?string $baseDir = null) |
|
84
|
|
|
{ |
|
85
|
4 |
|
$this->loader = $loader; |
|
86
|
4 |
|
parent::__construct($baseDir); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getContent($fileName): string |
|
90
|
|
|
{ |
|
91
|
1 |
|
if (!\is_file($fileName)) { |
|
92
|
|
|
$fileName = $this->loader->find($fileName); |
|
93
|
|
|
|
|
94
|
|
|
if (null === $fileName) { |
|
95
|
|
|
throw new Latte\RuntimeException(\sprintf('Missing template file \'%s\'.', $fileName)); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return parent::getContent($fileName); |
|
100
|
|
|
} |
|
101
|
|
|
}); |
|
102
|
|
|
|
|
103
|
4 |
|
return $latte->renderToString($template, $parameters); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Registers run-time filter. |
|
108
|
|
|
* |
|
109
|
|
|
* @return $this |
|
|
|
|
|
|
110
|
|
|
*/ |
|
111
|
|
|
public function addFilter(?string $name, callable $callback) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->latte->addFilter($name, $callback); |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Adds new macro. |
|
120
|
|
|
* |
|
121
|
|
|
* @return $this |
|
|
|
|
|
|
122
|
|
|
*/ |
|
123
|
|
|
public function addMacro(string $name, Latte\Macro $macro) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->latte->addMacro($name, $macro); |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Registers run-time function. |
|
132
|
|
|
* |
|
133
|
|
|
* @return $this |
|
|
|
|
|
|
134
|
|
|
*/ |
|
135
|
|
|
public function addFunction(string $name, callable $callback) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->latte->addFunction($name, $callback); |
|
138
|
|
|
|
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Adds new provider. |
|
144
|
|
|
* |
|
145
|
|
|
* @param mixed $value |
|
146
|
|
|
* |
|
147
|
|
|
* @return $this |
|
|
|
|
|
|
148
|
|
|
*/ |
|
149
|
|
|
public function addProvider(string $name, $value) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->latte->addProvider($name, $value); |
|
152
|
|
|
|
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return $this |
|
|
|
|
|
|
158
|
|
|
*/ |
|
159
|
|
|
public function setPolicy(?Latte\Policy $policy) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->latte->setPolicy($policy); |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return $this |
|
|
|
|
|
|
168
|
|
|
*/ |
|
169
|
|
|
public function setExceptionHandler(callable $callback) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->latte->setExceptionHandler($callback); |
|
172
|
|
|
|
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|