Skip to content

Commit cc4d007

Browse files
committed
feature #1623 Update the Twig extensions to use attributes (GromNaN)
This PR was merged into the main branch. Discussion ---------- Update the Twig extensions to use attributes Feature introduced in Symfony 7.3: to https://symfony.com/blog/new-in-symfony-7-3-twig-extension-attributes Commits ------- 8562d79 Update Twig extension to use attributes
2 parents e3ede42 + 8562d79 commit cc4d007

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

src/Twig/AppExtension.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
namespace App\Twig;
1313

1414
use Symfony\Component\Intl\Locales;
15-
use Twig\Extension\AbstractExtension;
16-
use Twig\TwigFunction;
15+
use Twig\Attribute\AsTwigFunction;
1716

1817
/**
1918
* See https://symfony.com/doc/current/templating/twig_extension.html.
@@ -22,7 +21,7 @@
2221
* @author Javier Eguiluz <[email protected]>
2322
* @author Julien ITARD <[email protected]>
2423
*/
25-
final class AppExtension extends AbstractExtension
24+
final class AppExtension
2625
{
2726
/**
2827
* @var list<array{code: string, name: string}>|null
@@ -38,21 +37,14 @@ public function __construct(
3837
) {
3938
}
4039

41-
public function getFunctions(): array
42-
{
43-
return [
44-
new TwigFunction('locales', [$this, 'getLocales']),
45-
new TwigFunction('is_rtl', [$this, 'isRtl']),
46-
];
47-
}
48-
4940
/**
5041
* Takes the list of codes of the locales (languages) enabled in the
5142
* application and returns an array with the name of each locale written
5243
* in its own language (e.g. English, Français, Español, etc.).
5344
*
5445
* @return array<int, array<string, string>>
5546
*/
47+
#[AsTwigFunction('locales')]
5648
public function getLocales(): array
5749
{
5850
if (null !== $this->locales) {
@@ -71,6 +63,7 @@ public function getLocales(): array
7163
/**
7264
* Check if the given locale is RTL.
7365
*/
66+
#[AsTwigFunction('is_rtl')]
7467
public function isRtl(?string $locale = null): bool
7568
{
7669
$locale = $locale ?? $this->defaultLocale;

src/Twig/SourceCodeExtension.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1515
use Symfony\Component\ErrorHandler\ErrorRenderer\FileLinkFormatter;
16+
use Twig\Attribute\AsTwigFunction;
1617
use Twig\Environment;
17-
use Twig\Extension\AbstractExtension;
1818
use Twig\TemplateWrapper;
19-
use Twig\TwigFunction;
2019

2120
use function Symfony\Component\String\u;
2221

@@ -29,7 +28,7 @@
2928
* @author Ryan Weaver <[email protected]>
3029
* @author Javier Eguiluz <[email protected]>
3130
*/
32-
final class SourceCodeExtension extends AbstractExtension
31+
final class SourceCodeExtension
3332
{
3433
/**
3534
* @var callable|null
@@ -49,17 +48,10 @@ public function setController(?callable $controller): void
4948
$this->controller = $controller;
5049
}
5150

52-
public function getFunctions(): array
53-
{
54-
return [
55-
new TwigFunction('link_source_file', $this->linkSourceFile(...), ['is_safe' => ['html'], 'needs_environment' => true]),
56-
new TwigFunction('show_source_code', $this->showSourceCode(...), ['is_safe' => ['html'], 'needs_environment' => true]),
57-
];
58-
}
59-
6051
/**
6152
* Render a link to a source file.
6253
*/
54+
#[AsTwigFunction('link_source_file', isSafe: ['html'])]
6355
public function linkSourceFile(Environment $twig, string $file, int $line): string
6456
{
6557
$text = str_replace('\\', '/', $file);
@@ -79,6 +71,7 @@ public function linkSourceFile(Environment $twig, string $file, int $line): stri
7971
);
8072
}
8173

74+
#[AsTwigFunction('show_source_code', isSafe: ['html'])]
8275
public function showSourceCode(Environment $twig, string|TemplateWrapper $template): string
8376
{
8477
return $twig->render('debug/source_code.html.twig', [

tests/Twig/AppExtensionTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Twig;
13+
14+
use App\Twig\AppExtension;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class AppExtensionTest extends TestCase
18+
{
19+
public function testGetLocales(): void
20+
{
21+
$extension = new AppExtension(['ar', 'es', 'fr'], 'ar');
22+
23+
$this->assertSame([
24+
['code' => 'ar', 'name' => 'العربية'],
25+
['code' => 'es', 'name' => 'español'],
26+
['code' => 'fr', 'name' => 'français'],
27+
], $extension->getLocales());
28+
}
29+
30+
public function testIsRtl(): void
31+
{
32+
$extension = new AppExtension(['ar', 'es', 'fr'], 'ar');
33+
34+
$this->assertFalse($extension->isRtl('fr'));
35+
$this->assertFalse($extension->isRtl('es'));
36+
$this->assertTrue($extension->isRtl('ar'));
37+
$this->assertTrue($extension->isRtl());
38+
}
39+
}

0 commit comments

Comments
 (0)