Description
Symfony version(s) affected: >= 2.8.0
(not sure exactly, probably as soon as the TemplateCacheWarmer
was introduced)
Description
The template cache warmer loads (lexes, compiles, ...) all files the default template iterator is listing. This may include images and other non-templates inside /templates
, /app/Resources/views
, any bundles resource paths and so on.
This can consume quite a lot of memory (and processing time). In fact for large files this fails completely. If you want to try this yourself do the following:
- create a large dummy file inside your default template path (800MB in this example):
dd if=/dev/zero of=file.txt count=1024 bs=781250
- warm the cache:
bin/console cache:warm
- wait...
In Compiler.php line 129: Error:
Field width 3200000000 is too long
In my opinion it's wrong to include random files in the cache warming process. One could argue that putting those files inside the template/views folders is a bad practice to begin with but Symfony does so as well and it allows to include assets like so: {{ include('@Twig/images/symfony-logo.svg') }}
.
Conclusion
In theory every file could be a twig template that's content needs to be parsed but it should imo have a .twig
file extension in this case. Like mylogo.svg.twig
or template.html5.twig
. (← these files potentially aren't valid svgs or html5 documents and you'd probably have setup your IDE to enable Twig language support based on the file extension anyway)
Possible Solution
Add a callback filter iterator or a condition inside the TemplateCacheWarmer
that strips all files without a .twig
extension before calling $twig->load($template)
.