diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index fecbfb71d..5b54e6c38 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -12,8 +12,7 @@ namespace App\Twig; use Symfony\Component\Intl\Locales; -use Twig\Extension\AbstractExtension; -use Twig\TwigFunction; +use Twig\Attribute\AsTwigFunction; /** * See https://symfony.com/doc/current/templating/twig_extension.html. @@ -22,7 +21,7 @@ * @author Javier Eguiluz * @author Julien ITARD */ -final class AppExtension extends AbstractExtension +final class AppExtension { /** * @var list|null @@ -38,14 +37,6 @@ public function __construct( ) { } - public function getFunctions(): array - { - return [ - new TwigFunction('locales', [$this, 'getLocales']), - new TwigFunction('is_rtl', [$this, 'isRtl']), - ]; - } - /** * Takes the list of codes of the locales (languages) enabled in the * application and returns an array with the name of each locale written @@ -53,6 +44,7 @@ public function getFunctions(): array * * @return array> */ + #[AsTwigFunction('locales')] public function getLocales(): array { if (null !== $this->locales) { @@ -71,6 +63,7 @@ public function getLocales(): array /** * Check if the given locale is RTL. */ + #[AsTwigFunction('is_rtl')] public function isRtl(?string $locale = null): bool { $locale = $locale ?? $this->defaultLocale; diff --git a/src/Twig/SourceCodeExtension.php b/src/Twig/SourceCodeExtension.php index 2f9e013b5..6cc400b72 100644 --- a/src/Twig/SourceCodeExtension.php +++ b/src/Twig/SourceCodeExtension.php @@ -13,10 +13,9 @@ use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\ErrorHandler\ErrorRenderer\FileLinkFormatter; +use Twig\Attribute\AsTwigFunction; use Twig\Environment; -use Twig\Extension\AbstractExtension; use Twig\TemplateWrapper; -use Twig\TwigFunction; use function Symfony\Component\String\u; @@ -29,7 +28,7 @@ * @author Ryan Weaver * @author Javier Eguiluz */ -final class SourceCodeExtension extends AbstractExtension +final class SourceCodeExtension { /** * @var callable|null @@ -49,17 +48,10 @@ public function setController(?callable $controller): void $this->controller = $controller; } - public function getFunctions(): array - { - return [ - new TwigFunction('link_source_file', $this->linkSourceFile(...), ['is_safe' => ['html'], 'needs_environment' => true]), - new TwigFunction('show_source_code', $this->showSourceCode(...), ['is_safe' => ['html'], 'needs_environment' => true]), - ]; - } - /** * Render a link to a source file. */ + #[AsTwigFunction('link_source_file', isSafe: ['html'])] public function linkSourceFile(Environment $twig, string $file, int $line): string { $text = str_replace('\\', '/', $file); @@ -79,6 +71,7 @@ public function linkSourceFile(Environment $twig, string $file, int $line): stri ); } + #[AsTwigFunction('show_source_code', isSafe: ['html'])] public function showSourceCode(Environment $twig, string|TemplateWrapper $template): string { return $twig->render('debug/source_code.html.twig', [ diff --git a/tests/Twig/AppExtensionTest.php b/tests/Twig/AppExtensionTest.php new file mode 100644 index 000000000..e38997270 --- /dev/null +++ b/tests/Twig/AppExtensionTest.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Twig; + +use App\Twig\AppExtension; +use PHPUnit\Framework\TestCase; + +class AppExtensionTest extends TestCase +{ + public function testGetLocales(): void + { + $extension = new AppExtension(['ar', 'es', 'fr'], 'ar'); + + $this->assertSame([ + ['code' => 'ar', 'name' => 'العربية'], + ['code' => 'es', 'name' => 'español'], + ['code' => 'fr', 'name' => 'français'], + ], $extension->getLocales()); + } + + public function testIsRtl(): void + { + $extension = new AppExtension(['ar', 'es', 'fr'], 'ar'); + + $this->assertFalse($extension->isRtl('fr')); + $this->assertFalse($extension->isRtl('es')); + $this->assertTrue($extension->isRtl('ar')); + $this->assertTrue($extension->isRtl()); + } +}