1 | <?php |
||
27 | class TwigView extends View |
||
28 | { |
||
29 | public const EXT = '.twig'; |
||
30 | |||
31 | public const ENV_CONFIG = 'WyriHaximus.TwigView.environment'; |
||
32 | |||
33 | /** |
||
34 | * Extension to use. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $_ext = self::EXT; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $extensions = [ |
||
44 | self::EXT, |
||
45 | '.php', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * Twig instance. |
||
50 | * |
||
51 | * @var \Twig\Environment |
||
52 | */ |
||
53 | protected $twig; |
||
54 | |||
55 | /** |
||
56 | * Return empty string when View instance is cast to string. |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | public function __toString(): string |
||
64 | |||
65 | /** |
||
66 | * Initialize view. |
||
67 | * |
||
68 | */ |
||
69 | 17 | public function initialize(): void |
|
79 | |||
80 | /** |
||
81 | * @param string $extension |
||
82 | */ |
||
83 | public function unshiftExtension($extension) |
||
87 | |||
88 | /** |
||
89 | * Get twig environment instance. |
||
90 | * |
||
91 | * @return \Twig\Environment |
||
92 | */ |
||
93 | 5 | public function getTwig(): Environment |
|
97 | |||
98 | /** |
||
99 | * @internal |
||
100 | */ |
||
101 | 4 | public function setTwig(Environment $twig): void |
|
105 | |||
106 | /** |
||
107 | * @return array |
||
108 | */ |
||
109 | 17 | protected function resolveConfig(): array |
|
133 | |||
134 | /** |
||
135 | * @return array |
||
136 | */ |
||
137 | 17 | protected function readConfig(): array |
|
150 | |||
151 | /** |
||
152 | * Create the template loader. |
||
153 | * |
||
154 | * @return \WyriHaximus\TwigView\Lib\Twig\Loader |
||
155 | */ |
||
156 | 17 | protected function getLoader(): Loader |
|
163 | |||
164 | /** |
||
165 | * Render the template. |
||
166 | * |
||
167 | * @param string $viewFile Template file. |
||
168 | * @param array $data Data that can be used by the template. |
||
169 | * |
||
170 | * @throws \Exception |
||
171 | * @return string |
||
172 | */ |
||
173 | 3 | protected function _render(string $viewFile, array $data = []): string |
|
205 | |||
206 | /** |
||
207 | * @param string|null $name |
||
208 | * @throws \Exception |
||
209 | * @return string |
||
210 | */ |
||
211 | 3 | protected function _getTemplateFileName(?string $name = null): string |
|
212 | { |
||
213 | 3 | $rethrow = new Exception('You\'re not supposed to get here'); |
|
214 | 3 | foreach ($this->extensions as $extension) { |
|
215 | 3 | $this->_ext = $extension; |
|
216 | try { |
||
217 | 3 | return parent::_getTemplateFileName($name); |
|
218 | 1 | } catch (Exception $exception) { |
|
219 | 1 | $rethrow = $exception; |
|
220 | } |
||
221 | } |
||
222 | |||
223 | throw $rethrow; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param string|null $name |
||
228 | * @throws \Exception |
||
229 | * @return string |
||
230 | */ |
||
231 | 1 | protected function _getLayoutFileName(?string $name = null): string |
|
245 | |||
246 | /** |
||
247 | * @param string $name |
||
248 | * @param bool $pluginCheck |
||
249 | * @return string|bool |
||
250 | */ |
||
251 | protected function _getElementFileName(string $name, bool $pluginCheck = true) |
||
263 | } |
||
264 |