Skip to content

[Twig] Move AsTwigFilter/AsTwigFunction attribute rules to the Twig composer-based set - #1011

Open
TomasVotruba wants to merge 1 commit into
mainfrom
move-twig-attribute-rules-to-twig-set
Open

[Twig] Move AsTwigFilter/AsTwigFunction attribute rules to the Twig composer-based set#1011
TomasVotruba wants to merge 1 commit into
mainfrom
move-twig-attribute-rules-to-twig-set

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

GetFiltersToAsTwigFilterAttributeRector and GetFunctionsToAsTwigFunctionAttributeRector were registered in the Symfony composer-based set, while both are bound to twig/twig:

public function provideComposerPackageConstraint(): ComposerPackageConstraint
{
    return new ComposerPackageConstraint('twig/twig', '>=3.21');
}

The attributes they add come from Twig itself — Symfony 7.3 only made them usable in Twig extensions. A project on Twig 3.21 without symfony/twig-bundle should still get the rewrite:

 use Twig\Extension\AbstractExtension;
+use Twig\Attribute\AsTwigFunction;

 class SomeExtension extends AbstractExtension
 {
-    public function getFunctions(): array
-    {
-        return [new TwigFunction('some_function', $this->someFunction(...))];
-    }
-
+    #[AsTwigFunction('some_function')]
     public function someFunction(): string
     {
         return 'result';
     }
 }

So both move to config/sets/twig/composer-based.php, under a // twig/twig 3.21 group. No rule behavior changes — the version bond was already correct, only the set registration was in the wrong file.

The rule namespace stays Symfony73 so the classes and their tests stay where they are.

…omposer-based set

Both rules are bound to twig/twig >=3.21, not to any Symfony package, so they belong to the Twig set. The attribute they add, #[AsTwigFilter] and #[AsTwigFunction], comes from Twig itself; Symfony 7.3 only made it usable in Twig extensions.

Rule namespace stays Symfony73 to keep the classes and their tests in place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant