Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions src/Twig/AppExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -22,7 +21,7 @@
* @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
Expand All @@ -38,21 +37,14 @@ 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
* in its own language (e.g. English, Français, Español, etc.).
*
* @return array<int, array<string, string>>
*/
#[AsTwigFunction('locales')]
public function getLocales(): array
{
if (null !== $this->locales) {
Expand All @@ -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;
Expand Down
15 changes: 4 additions & 11 deletions src/Twig/SourceCodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,7 +28,7 @@
* @author Ryan Weaver <weaverryan@gmail.com>
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
final class SourceCodeExtension extends AbstractExtension
final class SourceCodeExtension
{
/**
* @var callable|null
Expand All @@ -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);
Expand All @@ -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', [
Expand Down
39 changes: 39 additions & 0 deletions tests/Twig/AppExtensionTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}