1 | <?php declare(strict_types=1); |
||
26 | class TwigView extends View |
||
27 | { |
||
28 | const EXT = '.twig'; |
||
29 | |||
30 | const ENV_CONFIG = 'WyriHaximus.TwigView.environment'; |
||
31 | |||
32 | /** |
||
33 | * Extension to use. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $_ext = self::EXT; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $extensions = [ |
||
43 | self::EXT, |
||
44 | '.php', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Twig instance. |
||
49 | * |
||
50 | * @var \Twig\Environment |
||
51 | */ |
||
52 | protected $twig; |
||
53 | |||
54 | /** |
||
55 | * Return empty string when View instance is cast to string. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function __toString(): string |
||
63 | |||
64 | /** |
||
65 | * Initialize view. |
||
66 | * |
||
67 | */ |
||
68 | 17 | public function initialize(): void |
|
69 | { |
||
70 | 17 | $this->twig = new Environment($this->getLoader(), $this->resolveConfig()); |
|
71 | |||
72 | 17 | $this->getEventManager()->dispatch(ConstructEvent::create($this, $this->twig)); |
|
73 | |||
74 | 17 | $this->_ext = self::EXT; |
|
75 | |||
76 | 17 | parent::initialize(); |
|
77 | 17 | } |
|
78 | |||
79 | /** |
||
80 | * @param string $extension |
||
81 | */ |
||
82 | public function unshiftExtension($extension) |
||
83 | { |
||
84 | array_unshift($this->extensions, $extension); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Get twig environment instance. |
||
89 | * |
||
90 | * @return \Twig\Environment |
||
91 | */ |
||
92 | 5 | public function getTwig(): Environment |
|
96 | |||
97 | /** |
||
98 | * @internal |
||
99 | */ |
||
100 | 4 | public function setTwig(Environment $twig): void |
|
104 | |||
105 | /** |
||
106 | * @return array |
||
107 | */ |
||
108 | 17 | protected function resolveConfig(): array |
|
132 | |||
133 | /** |
||
134 | * @return array |
||
135 | */ |
||
136 | 17 | protected function readConfig(): array |
|
149 | |||
150 | /** |
||
151 | * Create the template loader. |
||
152 | * |
||
153 | * @return \WyriHaximus\TwigView\Lib\Twig\Loader |
||
154 | */ |
||
155 | 17 | protected function getLoader(): Loader |
|
162 | |||
163 | /** |
||
164 | * Render the template. |
||
165 | * |
||
166 | * @param string $viewFile Template file. |
||
167 | * @param array $data Data that can be used by the template. |
||
168 | * |
||
169 | * @throws \Exception |
||
170 | * @return string |
||
171 | */ |
||
172 | 3 | protected function _render(string $viewFile, array $data = []): string |
|
204 | |||
205 | /** |
||
206 | * @param string|null $name |
||
207 | * @throws \Exception |
||
208 | * @return string |
||
209 | */ |
||
210 | 3 | protected function _getViewFileName(?string $name = null): string |
|
224 | |||
225 | /** |
||
226 | * @param string|null $name |
||
227 | * @throws \Exception |
||
228 | * @return string |
||
229 | */ |
||
230 | 1 | protected function _getLayoutFileName(?string $name = null): string |
|
244 | |||
245 | /** |
||
246 | * @param string $name |
||
247 | * @param bool $pluginCheck |
||
248 | * @return string|bool |
||
249 | */ |
||
250 | protected function _getElementFileName(string $name, bool $pluginCheck = true) |
||
262 | } |
||
263 |