From c7f20abea62d82d36b2ef954a9e6847537243063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Thu, 30 Nov 2023 09:39:47 +0100 Subject: [PATCH] Add AsTwigExtension attribute and make function name required --- src/Extension/Attribute/AsTwigExtension.php | 13 +++++++ src/Extension/Attribute/AsTwigFilter.php | 6 ++-- src/Extension/Attribute/AsTwigFunction.php | 6 ++-- src/Extension/Attribute/AsTwigTest.php | 6 ++-- tests/Extension/AttributeExtensionTest.php | 31 ++++++++-------- .../Fixtures/ExtensionWithAttributes.php | 36 +++++++++---------- 6 files changed, 53 insertions(+), 45 deletions(-) create mode 100644 src/Extension/Attribute/AsTwigExtension.php diff --git a/src/Extension/Attribute/AsTwigExtension.php b/src/Extension/Attribute/AsTwigExtension.php new file mode 100644 index 00000000000..ef0edeb656d --- /dev/null +++ b/src/Extension/Attribute/AsTwigExtension.php @@ -0,0 +1,13 @@ + ['fooFilter', 'fooFilter', []]; yield 'with name' => ['foo', 'fooFilter', []]; - yield 'with env' => ['withEnvFilter', 'withEnvFilter', ['needs_environment' => true]]; - yield 'with context' => ['withContextFilter', 'withContextFilter', ['needs_context' => true]]; - yield 'with env and context' => ['withEnvAndContextFilter', 'withEnvAndContextFilter', ['needs_environment' => true, 'needs_context' => true]]; - yield 'no argument' => ['noArgFilter', 'noArgFilter', []]; - yield 'variadic' => ['variadicFilter', 'variadicFilter', ['is_variadic' => true]]; - yield 'deprecated' => ['deprecatedFilter', 'deprecatedFilter', ['deprecated' => true, 'alternative' => 'bar']]; + yield 'with env' => ['with_env_filter', 'withEnvFilter', ['needs_environment' => true]]; + yield 'with context' => ['with_context_filter', 'withContextFilter', ['needs_context' => true]]; + yield 'with env and context' => ['with_env_and_context_filter', 'withEnvAndContextFilter', ['needs_environment' => true, 'needs_context' => true]]; + yield 'no argument' => ['no_arg_filter', 'noArgFilter', []]; + yield 'variadic' => ['variadic_filter', 'variadicFilter', ['is_variadic' => true]]; + yield 'deprecated' => ['deprecated_filter', 'deprecatedFilter', ['deprecated' => true, 'alternative' => 'bar']]; } /** @@ -64,14 +63,13 @@ public function testFunction(string $name, string $method, array $options) public static function provideFunctions() { - yield 'basic' => ['fooFunction', 'fooFunction', []]; yield 'with name' => ['foo', 'fooFunction', []]; - yield 'with env' => ['withEnvFunction', 'withEnvFunction', ['needs_environment' => true]]; - yield 'with context' => ['withContextFunction', 'withContextFunction', ['needs_context' => true]]; - yield 'with env and context' => ['withEnvAndContextFunction', 'withEnvAndContextFunction', ['needs_environment' => true, 'needs_context' => true]]; - yield 'no argument' => ['noArgFunction', 'noArgFunction', []]; - yield 'variadic' => ['variadicFunction', 'variadicFunction', ['is_variadic' => true]]; - yield 'deprecated' => ['deprecatedFunction', 'deprecatedFunction', ['deprecated' => true, 'alternative' => 'bar']]; + yield 'with env' => ['with_env_function', 'withEnvFunction', ['needs_environment' => true]]; + yield 'with context' => ['with_context_function', 'withContextFunction', ['needs_context' => true]]; + yield 'with env and context' => ['with_env_and_context_function', 'withEnvAndContextFunction', ['needs_environment' => true, 'needs_context' => true]]; + yield 'no argument' => ['no_arg_function', 'noArgFunction', []]; + yield 'variadic' => ['variadic_function', 'variadicFunction', ['is_variadic' => true]]; + yield 'deprecated' => ['deprecated_function', 'deprecatedFunction', ['deprecated' => true, 'alternative' => 'bar']]; } /** @@ -94,10 +92,9 @@ public function testTest(string $name, string $method, array $options) public static function provideTests() { - yield 'basic' => ['fooTest', 'fooTest', []]; yield 'with name' => ['foo', 'fooTest', []]; - yield 'variadic' => ['variadicTest', 'variadicTest', ['is_variadic' => true]]; - yield 'deprecated' => ['deprecatedTest', 'deprecatedTest', ['deprecated' => true, 'alternative' => 'bar']]; + yield 'variadic' => ['variadic_test', 'variadicTest', ['is_variadic' => true]]; + yield 'deprecated' => ['deprecated_test', 'deprecatedTest', ['deprecated' => true, 'alternative' => 'bar']]; } public function testRuntimeExtension() diff --git a/tests/Extension/Fixtures/ExtensionWithAttributes.php b/tests/Extension/Fixtures/ExtensionWithAttributes.php index 0429e39e18b..3cc44bc0064 100644 --- a/tests/Extension/Fixtures/ExtensionWithAttributes.php +++ b/tests/Extension/Fixtures/ExtensionWithAttributes.php @@ -3,97 +3,95 @@ namespace Twig\Tests\Extension\Fixtures; use Twig\Environment; +use Twig\Extension\Attribute\AsTwigExtension; use Twig\Extension\Attribute\AsTwigFilter; use Twig\Extension\Attribute\AsTwigFunction; use Twig\Extension\Attribute\AsTwigTest; -use Twig\Extension\RuntimeExtensionInterface; -class ExtensionWithAttributes implements RuntimeExtensionInterface +#[AsTwigExtension] +class ExtensionWithAttributes { - #[AsTwigFilter] #[AsTwigFilter(name: 'foo')] public function fooFilter(string $string) { } - #[AsTwigFilter] + #[AsTwigFilter('with_context_filter')] public function withContextFilter(array $context, string $string) { } - #[AsTwigFilter] + #[AsTwigFilter('with_env_filter')] public function withEnvFilter(Environment $env, string $string) { } - #[AsTwigFilter] + #[AsTwigFilter('with_env_and_context_filter')] public function withEnvAndContextFilter(Environment $env, array $context, string $string) { } - #[AsTwigFilter] + #[AsTwigFilter('no_arg_filter')] public function noArgFilter() { } - #[AsTwigFilter] + #[AsTwigFilter('variadic_filter')] public function variadicFilter(string ...$strings) { } - #[AsTwigFilter(deprecated: true, alternative: 'bar')] + #[AsTwigFilter('deprecated_filter', deprecated: true, alternative: 'bar')] public function deprecatedFilter(string $string) { } - #[AsTwigFunction] #[AsTwigFunction(name: 'foo')] public function fooFunction(string $string) { } - #[AsTwigFunction] + #[AsTwigFunction('with_context_function')] public function withContextFunction(array $context, string $string) { } - #[AsTwigFunction] + #[AsTwigFunction('with_env_function')] public function withEnvFunction(Environment $env, string $string) { } - #[AsTwigFunction] + #[AsTwigFunction('with_env_and_context_function')] public function withEnvAndContextFunction(Environment $env, array $context, string $string) { } - #[AsTwigFunction] + #[AsTwigFunction('no_arg_function')] public function noArgFunction() { } - #[AsTwigFunction] + #[AsTwigFunction('variadic_function')] public function variadicFunction(string ...$strings) { } - #[AsTwigFunction(deprecated: true, alternative: 'bar')] + #[AsTwigFunction('deprecated_function', deprecated: true, alternative: 'bar')] public function deprecatedFunction(string $string) { } - #[AsTwigTest] #[AsTwigTest(name: 'foo')] public function fooTest(string $string) { } - #[AsTwigTest] + #[AsTwigTest('variadic_test')] public function variadicTest(string ...$strings) { } - #[AsTwigTest(deprecated: true, alternative: 'bar')] + #[AsTwigTest('deprecated_test', deprecated: true, alternative: 'bar')] public function deprecatedTest(string $string) { }