diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/AttributeExtensionPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/AttributeExtensionPass.php index 24f760802bc94..354874866a0ae 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/AttributeExtensionPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/AttributeExtensionPass.php @@ -14,10 +14,13 @@ use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Exception\LogicException; use Twig\Attribute\AsTwigFilter; use Twig\Attribute\AsTwigFunction; use Twig\Attribute\AsTwigTest; +use Twig\Extension\AbstractExtension; use Twig\Extension\AttributeExtension; +use Twig\Extension\ExtensionInterface; /** * Register an instance of AttributeExtension for each service using the @@ -33,6 +36,14 @@ final class AttributeExtensionPass implements CompilerPassInterface public static function autoconfigureFromAttribute(ChildDefinition $definition, AsTwigFilter|AsTwigFunction|AsTwigTest $attribute, \ReflectionMethod $reflector): void { + $class = $reflector->getDeclaringClass(); + if ($class->implementsInterface(ExtensionInterface::class)) { + if ($class->isSubclassOf(AbstractExtension::class)) { + throw new LogicException(\sprintf('The class "%s" cannot extend "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, AbstractExtension::class, $attribute::class, $reflector->name)); + } + throw new LogicException(\sprintf('The class "%s" cannot implement "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, ExtensionInterface::class, $attribute::class, $reflector->name)); + } + $definition->addTag(self::TAG); // The service must be tagged as a runtime to call non-static methods diff --git a/src/Symfony/Bundle/TwigBundle/Tests/Functional/AttributeExtensionTest.php b/src/Symfony/Bundle/TwigBundle/Tests/Functional/AttributeExtensionTest.php index 81ce2cbe97bca..8b4e4555f36a0 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/Functional/AttributeExtensionTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/Functional/AttributeExtensionTest.php @@ -11,11 +11,15 @@ namespace Symfony\Bundle\TwigBundle\Tests\Functional; +use PHPUnit\Framework\Attributes\After; +use PHPUnit\Framework\Attributes\Before; +use PHPUnit\Framework\Attributes\BeforeClass; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\TwigBundle\Tests\TestCase; use Symfony\Bundle\TwigBundle\TwigBundle; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Exception\LogicException; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel\Kernel; use Twig\Attribute\AsTwigFilter; @@ -23,23 +27,23 @@ use Twig\Attribute\AsTwigTest; use Twig\Environment; use Twig\Error\RuntimeError; +use Twig\Extension\AbstractExtension; use Twig\Extension\AttributeExtension; class AttributeExtensionTest extends TestCase { - public function testExtensionWithAttributes() + /** @beforeClass */ + #[BeforeClass] + public static function assertTwigVersion(): void { if (!class_exists(AttributeExtension::class)) { self::markTestSkipped('Twig 3.21 is required.'); } + } - $kernel = new class('test', true) extends Kernel - { - public function registerBundles(): iterable - { - return [new FrameworkBundle(), new TwigBundle()]; - } - + public function testExtensionWithAttributes() + { + $kernel = new class extends AttributeExtensionKernel { public function registerContainerConfiguration(LoaderInterface $loader): void { $loader->load(static function (ContainerBuilder $container) { @@ -53,11 +57,6 @@ public function registerContainerConfiguration(LoaderInterface $loader): void $container->setAlias('twig_test', 'twig')->setPublic(true); }); } - - public function getProjectDir(): string - { - return sys_get_temp_dir().'/'.Kernel::VERSION.'/AttributeExtension'; - } }; $kernel->boot(); @@ -73,10 +72,30 @@ public function getProjectDir(): string $twig->getRuntime(StaticExtensionWithAttributes::class); } + public function testInvalidExtensionClass() + { + $kernel = new class extends AttributeExtensionKernel { + public function registerContainerConfiguration(LoaderInterface $loader): void + { + $loader->load(static function (ContainerBuilder $container) { + $container->register(InvalidExtensionWithAttributes::class, InvalidExtensionWithAttributes::class) + ->setAutoconfigured(true); + }); + } + }; + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('The class "Symfony\Bundle\TwigBundle\Tests\Functional\InvalidExtensionWithAttributes" cannot extend "Twig\Extension\AbstractExtension" and use the "#[Twig\Attribute\AsTwigFilter]" attribute on method "funFilter()", choose one or the other.'); + + $kernel->boot(); + } + + /** * @before * @after */ + #[Before, After] protected function deleteTempDir() { if (file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/AttributeExtension')) { @@ -85,6 +104,24 @@ protected function deleteTempDir() } } +abstract class AttributeExtensionKernel extends Kernel +{ + public function __construct() + { + parent::__construct('test', true); + } + + public function registerBundles(): iterable + { + return [new FrameworkBundle(), new TwigBundle()]; + } + + public function getProjectDir(): string + { + return sys_get_temp_dir().'/'.Kernel::VERSION.'/AttributeExtension'; + } +} + class StaticExtensionWithAttributes { #[AsTwigFilter('foo')] @@ -112,10 +149,19 @@ public function __construct(private bool $prefix) { } - #[AsTwigFilter('foo')] - #[AsTwigFunction('foo')] + #[AsTwigFilter('prefix_foo')] + #[AsTwigFunction('prefix_foo')] public function prefix(string $value): string { return $this->prefix.$value; } } + +class InvalidExtensionWithAttributes extends AbstractExtension +{ + #[AsTwigFilter('fun')] + public function funFilter(): string + { + return 'fun'; + } +}