Making the Lexer initialize itself lazily, to avoid loading the extension set early #3837
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi!
Over on Symfony UX, we are pushing Twig in some crazy, "unconventional" ways by overriding the lexer to add a new HTML-like syntax - e.g.
<twig:Alert type="success">
- https://github.com/symfony/ux/blob/2.x/src/TwigComponent/src/Twig/ComponentLexer.phpNormally, the internal
Lexer
is not instantiated until it's needed - i.e. when a template is rendered. However, when overriding the lexer, ourComponentLexer
needs to be instantiated early and set onto theEnvironment
. And since it needs to extendLexer
,Lexer::__construct()
is called much earlier than normal. One line in the constructor -Twig/src/Lexer.php
Line 108 in f5ee1b6
Environment
. Some applications rely on being able to set extra Twig extensions afterEnvironment
is created, but before a template is rendered and our custom lexer currently breaks that.So, the suggestion is to make the work inside
__construct()
done later. Everything is private, so I believe this is safe.Cheers!