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