mirror of
https://github.com/symfony/demo.git
synced 2026-03-24 00:02:32 +01:00
Update Twig extension to use attributes
This commit is contained in:
@@ -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 @@ use Twig\TwigFunction;
|
||||
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
|
||||
* @author Julien ITARD <julienitard@gmail.com>
|
||||
*/
|
||||
final class AppExtension extends AbstractExtension
|
||||
final class AppExtension
|
||||
{
|
||||
/**
|
||||
* @var list<array{code: string, name: string}>|null
|
||||
@@ -38,14 +37,6 @@ final class AppExtension extends AbstractExtension
|
||||
) {
|
||||
}
|
||||
|
||||
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 @@ final class AppExtension extends AbstractExtension
|
||||
*
|
||||
* @return array<int, array<string, string>>
|
||||
*/
|
||||
#[AsTwigFunction('locales')]
|
||||
public function getLocales(): array
|
||||
{
|
||||
if (null !== $this->locales) {
|
||||
@@ -71,6 +63,7 @@ final class AppExtension extends AbstractExtension
|
||||
/**
|
||||
* Check if the given locale is RTL.
|
||||
*/
|
||||
#[AsTwigFunction('is_rtl')]
|
||||
public function isRtl(?string $locale = null): bool
|
||||
{
|
||||
$locale = $locale ?? $this->defaultLocale;
|
||||
|
||||
@@ -13,10 +13,9 @@ namespace App\Twig;
|
||||
|
||||
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 @@ use function Symfony\Component\String\u;
|
||||
* @author Ryan Weaver <weaverryan@gmail.com>
|
||||
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
|
||||
*/
|
||||
final class SourceCodeExtension extends AbstractExtension
|
||||
final class SourceCodeExtension
|
||||
{
|
||||
/**
|
||||
* @var callable|null
|
||||
@@ -49,17 +48,10 @@ final class SourceCodeExtension extends AbstractExtension
|
||||
$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 @@ final class SourceCodeExtension extends AbstractExtension
|
||||
);
|
||||
}
|
||||
|
||||
#[AsTwigFunction('show_source_code', isSafe: ['html'])]
|
||||
public function showSourceCode(Environment $twig, string|TemplateWrapper $template): string
|
||||
{
|
||||
return $twig->render('debug/source_code.html.twig', [
|
||||
|
||||
39
tests/Twig/AppExtensionTest.php
Normal file
39
tests/Twig/AppExtensionTest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user