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 Twig; |
21
|
|
|
use Twig\Cache\CacheInterface; |
22
|
|
|
use Twig\Extension\ExtensionInterface; |
23
|
|
|
use Twig\Loader\ArrayLoader; |
24
|
|
|
use Twig\Loader\ChainLoader; |
25
|
|
|
use Twig\NodeVisitor\NodeVisitorInterface; |
26
|
|
|
use Twig\RuntimeLoader\RuntimeLoaderInterface; |
27
|
|
|
use Twig\TwigFilter; |
28
|
|
|
use Twig\TwigFunction; |
29
|
|
|
|
30
|
|
|
final class TwigRender extends AbstractRender |
31
|
|
|
{ |
32
|
|
|
protected const EXTENSIONS = ['twig']; |
33
|
|
|
|
34
|
|
|
/** @var Twig\Environment */ |
35
|
|
|
protected $environment; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* TwigEngine constructor. |
39
|
|
|
* |
40
|
|
|
* @param Twig\Environment $engine |
41
|
|
|
* @param string[] $extensions |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Twig\Environment $environment, array $extensions = self::EXTENSIONS) |
44
|
|
|
{ |
45
|
|
|
$this->environment = $environment; |
46
|
|
|
$this->extensions = $extensions; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function render(string $template, array $parameters): string |
53
|
|
|
{ |
54
|
|
|
$parameters = \array_merge($parameters, ['view' => $this]); |
55
|
|
|
$source = $this->getLoader()->find($template); |
56
|
|
|
|
57
|
|
|
$this->environment->setLoader(new ChainLoader([ |
58
|
|
|
new ArrayLoader([ |
59
|
|
|
$template => (string) $source->isFile() ? \file_get_contents($source) : $source, |
60
|
|
|
]), |
61
|
|
|
$this->environment->getLoader(), |
62
|
|
|
])); |
63
|
|
|
|
64
|
|
|
return $this->environment->render($template, $parameters); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $charset |
69
|
|
|
*/ |
70
|
|
|
public function setCharset(string $charset): void |
71
|
|
|
{ |
72
|
|
|
$this->environment->setCharset($charset); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sets the current cache implementation. |
77
|
|
|
* |
78
|
|
|
* @param CacheInterface|false|string $cache A Twig\Cache\CacheInterface implementation, |
79
|
|
|
* an absolute path to the compiled templates, |
80
|
|
|
* or false to disable cache |
81
|
|
|
*/ |
82
|
|
|
public function setCache($cache): void |
83
|
|
|
{ |
84
|
|
|
$this->environment->setCache($cache); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param RuntimeLoaderInterface $loader |
89
|
|
|
*/ |
90
|
|
|
public function addRuntimeLoader(RuntimeLoaderInterface $loader): void |
91
|
|
|
{ |
92
|
|
|
$this->environment->addRuntimeLoader($loader); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param ExtensionInterface $extension |
97
|
|
|
*/ |
98
|
|
|
public function addExtension(ExtensionInterface $extension): void |
99
|
|
|
{ |
100
|
|
|
$this->environment->addExtension($extension); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param ExtensionInterface[] $extensions An array of extensions |
105
|
|
|
*/ |
106
|
|
|
public function setExtensions(array $extensions): void |
107
|
|
|
{ |
108
|
|
|
$this->environment->setExtensions($extensions); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param NodeVisitorInterface $visitor |
113
|
|
|
*/ |
114
|
|
|
public function addNodeVisitor(NodeVisitorInterface $visitor): void |
115
|
|
|
{ |
116
|
|
|
$this->environment->addNodeVisitor($visitor); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param TwigFilter $filter |
121
|
|
|
*/ |
122
|
|
|
public function addFilter(TwigFilter $filter): void |
123
|
|
|
{ |
124
|
|
|
$this->environment->addFilter($filter); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param callable $callable |
129
|
|
|
*/ |
130
|
|
|
public function registerUndefinedFilterCallback(callable $callable): void |
131
|
|
|
{ |
132
|
|
|
$this->environment->registerUndefinedFilterCallback($callable); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param TwigFunction $function |
137
|
|
|
*/ |
138
|
|
|
public function addFunction(TwigFunction $function): void |
139
|
|
|
{ |
140
|
|
|
$this->environment->addFunction($function); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|