[Symfony73] Keep AbstractExtension when Twig extension overrides a built-in function/filter#968
Merged
Merged
Conversation
…ilt-in function/filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When a Twig extension overrides a built-in Twig function/filter (e.g.
include,date,escape), the override only works while the class stays a classicextends AbstractExtension. The#[AsTwigFunction]/#[AsTwigFilter]attribute-based registration cannot override core Twig callables the same way.GetFunctionsToAsTwigFunctionAttributeRector(and its filter sibling, via the shared transformer) now skips such built-in-named items: they stay in the array and the class keepsextends AbstractExtension. Other, non-built-in items in the same class are still converted.Skip a pure built-in override
No change — the class keeps
AbstractExtensionbecauseincludeis a built-in.Convert custom, keep built-in
final class KeepBuiltinConvertCustom extends AbstractExtension { public function getFunctions(): array { return [ new TwigFunction('include', $this->includeWithEvent(...), ['needs_environment' => true]), - new TwigFunction('custom_function', [$this, 'customFunction']), ]; } public function includeWithEvent(Environment $env, $template) { return $template; } + #[\Twig\Attribute\AsTwigFunction(name: 'custom_function')] public function customFunction($value) { return $value; } }The custom function becomes an attribute; the
includeoverride stays registered the classic way, soAbstractExtensionis preserved.