1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Cecil. |
||
5 | * |
||
6 | * (c) Arnaud Ligny <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Cecil\Renderer; |
||
15 | |||
16 | use Cecil\Builder; |
||
17 | use Cecil\Exception\RuntimeException; |
||
18 | use Cecil\Renderer\Extension\Core as CoreExtension; |
||
19 | use Cecil\Util; |
||
20 | use Performing\TwigComponents\Configuration; |
||
21 | use Symfony\Bridge\Twig\Extension\TranslationExtension; |
||
22 | use Symfony\Component\Translation\Formatter\MessageFormatter; |
||
23 | use Symfony\Component\Translation\IdentityTranslator; |
||
24 | use Symfony\Component\Translation\Translator; |
||
25 | use Twig\Extra\Cache\CacheExtension; |
||
26 | use Twig\Extra\Intl\IntlExtension; |
||
27 | use Twig\Extra\String\StringExtension; |
||
28 | |||
29 | /** |
||
30 | * Twig renderer. |
||
31 | * |
||
32 | * This class is responsible for rendering templates using the Twig templating engine. |
||
33 | * It initializes Twig with the necessary configurations, loads extensions, and provides methods |
||
34 | * to render templates, add global variables, and manage translations. |
||
35 | * It also supports debugging and profiling when in debug mode. |
||
36 | */ |
||
37 | class Twig implements RendererInterface |
||
38 | { |
||
39 | /** |
||
40 | * Builder object. |
||
41 | * @var Builder |
||
42 | */ |
||
43 | protected $builder; |
||
44 | /** |
||
45 | * Twig environment instance. |
||
46 | * @var \Twig\Environment |
||
47 | */ |
||
48 | private $twig; |
||
49 | /** |
||
50 | * Translator instance for translations. |
||
51 | * @var Translator |
||
52 | */ |
||
53 | private $translator = null; |
||
54 | /** |
||
55 | * Profile for debugging and profiling. |
||
56 | * @var \Twig\Profiler\Profile |
||
57 | */ |
||
58 | private $profile = null; |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function __construct(Builder $builder, $templatesPath) |
||
64 | { |
||
65 | $this->builder = $builder; |
||
66 | // load layouts |
||
67 | $loader = new \Twig\Loader\FilesystemLoader($templatesPath); |
||
68 | // default options |
||
69 | $loaderOptions = [ |
||
70 | 'debug' => $this->builder->isDebug(), |
||
71 | 'strict_variables' => true, |
||
72 | 'autoescape' => false, |
||
73 | 'auto_reload' => true, |
||
74 | 'cache' => false, |
||
75 | ]; |
||
76 | // use Twig cache? |
||
77 | if ($this->builder->getConfig()->isEnabled('cache.templates')) { |
||
78 | $loaderOptions = array_replace($loaderOptions, ['cache' => $this->builder->getConfig()->getCacheTemplatesPath()]); |
||
79 | } |
||
80 | // create the Twig instance |
||
81 | $this->twig = new \Twig\Environment($loader, $loaderOptions); |
||
82 | // set date format |
||
83 | $this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
||
84 | ->setDateFormat((string) $this->builder->getConfig()->get('date.format')); |
||
85 | // set timezone |
||
86 | if ($this->builder->getConfig()->has('date.timezone')) { |
||
87 | $this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
||
88 | ->setTimezone($this->builder->getConfig()->get('date.timezone') ?? date_default_timezone_get()); |
||
89 | } |
||
90 | /* |
||
91 | * adds extensions |
||
92 | */ |
||
93 | // Cecil core extension |
||
94 | $this->twig->addExtension(new CoreExtension($this->builder)); |
||
95 | // required by `template_from_string()` |
||
96 | $this->twig->addExtension(new \Twig\Extension\StringLoaderExtension()); |
||
97 | // the `u` filter (https://github.com/twigphp/string-extra) |
||
98 | $this->twig->addExtension(new StringExtension()); |
||
99 | // l10n |
||
100 | $this->translator = new Translator( |
||
101 | $this->builder->getConfig()->getLanguageProperty('locale'), |
||
102 | new MessageFormatter(new IdentityTranslator()), |
||
103 | $this->builder->getConfig()->isEnabled('cache.translations') ? $this->builder->getConfig()->getCacheTranslationsPath() : null, |
||
104 | $this->builder->isDebug() |
||
105 | ); |
||
106 | if (\count($this->builder->getConfig()->getLanguages()) > 0) { |
||
107 | foreach ((array) $this->builder->getConfig()->get('layouts.translations.formats') as $format) { |
||
108 | $loader = \sprintf('Symfony\Component\Translation\Loader\%sFileLoader', ucfirst($format)); |
||
109 | if (class_exists($loader)) { |
||
110 | $this->translator->addLoader($format, new $loader()); |
||
111 | $this->builder->getLogger()->debug(\sprintf('Translation loader for format "%s" found', $format)); |
||
112 | } |
||
113 | } |
||
114 | foreach ($this->builder->getConfig()->getLanguages() as $lang) { |
||
115 | // internal |
||
116 | $this->addTransResource($this->builder->getConfig()->getTranslationsInternalPath(), $lang['locale']); |
||
117 | // themes |
||
118 | if ($themes = $this->builder->getConfig()->getTheme()) { |
||
119 | foreach ($themes as $theme) { |
||
120 | $this->addTransResource($this->builder->getConfig()->getThemeDirPath($theme, 'translations'), $lang['locale']); |
||
121 | } |
||
122 | } |
||
123 | // site |
||
124 | $this->addTransResource($this->builder->getConfig()->getTranslationsPath(), $lang['locale']); |
||
125 | } |
||
126 | } |
||
127 | $this->twig->addExtension(new TranslationExtension($this->translator)); |
||
128 | // intl |
||
129 | $this->twig->addExtension(new IntlExtension()); |
||
130 | if (\extension_loaded('intl')) { |
||
131 | $this->builder->getLogger()->debug('PHP Intl extension is loaded'); |
||
132 | } |
||
133 | // filters fallback |
||
134 | $this->twig->registerUndefinedFilterCallback(function ($name) { |
||
135 | switch ($name) { |
||
136 | case 'localizeddate': |
||
137 | return new \Twig\TwigFilter($name, function (?\DateTime $value = null) { |
||
138 | return date($this->builder->getConfig()->get('date.format'), $value?->getTimestamp()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
139 | }); |
||
140 | } |
||
141 | |||
142 | return false; |
||
143 | }); |
||
144 | // components |
||
145 | Configuration::make($this->twig) |
||
146 | ->setTemplatesPath($this->builder->getConfig()->get('layouts.components.dir') ?? 'components') |
||
147 | ->setTemplatesExtension($this->builder->getConfig()->get('layouts.components.ext') ?? 'twig') |
||
148 | ->useCustomTags() |
||
149 | ->setup(); |
||
150 | // cache |
||
151 | $this->twig->addExtension(new CacheExtension()); |
||
152 | $this->twig->addRuntimeLoader(new TwigCacheRuntimeLoader($this->builder->getConfig()->getCacheTemplatesPath())); |
||
153 | // debug |
||
154 | if ($this->builder->isDebug()) { |
||
155 | // dump() |
||
156 | $this->twig->addExtension(new \Twig\Extension\DebugExtension()); |
||
157 | // profiler |
||
158 | $this->profile = new \Twig\Profiler\Profile(); |
||
159 | $this->twig->addExtension(new \Twig\Extension\ProfilerExtension($this->profile)); |
||
160 | } |
||
161 | // loads custom extensions |
||
162 | if ($this->builder->getConfig()->has('layouts.extensions')) { |
||
163 | foreach ((array) $this->builder->getConfig()->get('layouts.extensions') as $name => $class) { |
||
164 | try { |
||
165 | $this->twig->addExtension(new $class($this->builder)); |
||
166 | $this->builder->getLogger()->debug(\sprintf('Twig extension "%s" added', $name)); |
||
167 | } catch (RuntimeException | \Error $e) { |
||
168 | $this->builder->getLogger()->error(\sprintf('Unable to add Twig extension "%s": %s', $name, $e->getMessage())); |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function addGlobal(string $name, $value): void |
||
178 | { |
||
179 | $this->twig->addGlobal($name, $value); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function render(string $template, array $variables): string |
||
186 | { |
||
187 | return $this->twig->render($template, $variables); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function setLocale(string $locale): void |
||
194 | { |
||
195 | if (\extension_loaded('intl')) { |
||
196 | \Locale::setDefault($locale); |
||
197 | } |
||
198 | $this->translator === null ?: $this->translator->setLocale($locale); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | public function addTransResource(string $translationsDir, string $locale): void |
||
205 | { |
||
206 | $locales = [$locale]; |
||
207 | // if locale is 'fr_FR', trying to load ['fr', 'fr_FR'] |
||
208 | if (\strlen($locale) > 2) { |
||
209 | array_unshift($locales, substr($locale, 0, 2)); |
||
210 | } |
||
211 | foreach ($locales as $locale) { |
||
212 | foreach ((array) $this->builder->getConfig()->get('layouts.translations.formats') as $format) { |
||
213 | $translationFile = Util::joinPath($translationsDir, \sprintf('messages.%s.%s', $locale, $format)); |
||
214 | if (Util\File::getFS()->exists($translationFile)) { |
||
215 | $this->translator->addResource($format, $translationFile, $locale); |
||
216 | $this->builder->getLogger()->debug(\sprintf('Translation file "%s" added', $translationFile)); |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Returns the Twig instance. |
||
224 | */ |
||
225 | public function getTwig(): \Twig\Environment |
||
226 | { |
||
227 | return $this->twig; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Returns debug profile. |
||
232 | */ |
||
233 | public function getDebugProfile(): ?\Twig\Profiler\Profile |
||
234 | { |
||
235 | return $this->profile; |
||
236 | } |
||
237 | } |
||
238 |