Skip to content

Commit

Permalink
Extract twig runtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Jul 30, 2023
1 parent 91baf8a commit 3d2f371
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 175 deletions.
44 changes: 12 additions & 32 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,35 @@ parameters:
count: 1
path: tests/Bridge/Symfony/App/AppKernel.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Twig\\\\\\\\TwigFilter' and Twig\\\\TwigFilter will always evaluate to true\\.$#"
count: 1
path: tests/Twig/Extension/RouterTwigExtensionTest.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Twig\\\\\\\\TwigFunction' and Twig\\\\TwigFunction will always evaluate to true\\.$#"
count: 1
path: tests/Twig/Extension/RouterTwigExtensionTest.php

-
message: "#^Method Nucleos\\\\Twig\\\\Tests\\\\Twig\\\\Extension\\\\RouterTwigExtensionTest\\:\\:getSplitList\\(\\) return type has no value type specified in iterable type iterable\\.$#"
count: 1
path: tests/Twig/Extension/RouterTwigExtensionTest.php

-
message: "#^Method Nucleos\\\\Twig\\\\Tests\\\\Twig\\\\Extension\\\\RouterTwigExtensionTest\\:\\:testSplitTag\\(\\) has parameter \\$output with no value type specified in iterable type array\\.$#"
count: 1
path: tests/Twig/Extension/RouterTwigExtensionTest.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Twig\\\\\\\\TwigFilter' and Twig\\\\TwigFilter will always evaluate to true\\.$#"
count: 1
path: tests/Twig/Extension/StringTwigExtensionTest.php

-
message: "#^Call to method getResponse\\(\\) on an unknown class Symfony\\\\Component\\\\HttpKernel\\\\Client\\.$#"
count: 1
path: tests/Twig/Extension/TwigExtensionIntegrationTest.php
path: tests/Bridge/Symfony/App/TwigExtensionIntegrationTest.php

-
message: "#^Call to method request\\(\\) on an unknown class Symfony\\\\Component\\\\HttpKernel\\\\Client\\.$#"
count: 1
path: tests/Twig/Extension/TwigExtensionIntegrationTest.php
path: tests/Bridge/Symfony/App/TwigExtensionIntegrationTest.php

-
message: "#^Instantiated class Symfony\\\\Component\\\\HttpKernel\\\\Client not found\\.$#"
count: 1
path: tests/Twig/Extension/TwigExtensionIntegrationTest.php
path: tests/Bridge/Symfony/App/TwigExtensionIntegrationTest.php

-
message: "#^Method Nucleos\\\\Twig\\\\Tests\\\\Runtime\\\\RouterRuntimeTest\\:\\:getSplitList\\(\\) return type has no value type specified in iterable type iterable\\.$#"
count: 1
path: tests/Runtime/RouterRuntimeTest.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Twig\\\\\\\\TwigFilter' and Twig\\\\TwigFilter will always evaluate to true\\.$#"
message: "#^Method Nucleos\\\\Twig\\\\Tests\\\\Runtime\\\\RouterRuntimeTest\\:\\:testSplitTag\\(\\) has parameter \\$output with no value type specified in iterable type array\\.$#"
count: 1
path: tests/Twig/Extension/UrlAutoConverterTwigExtensionTest.php
path: tests/Runtime/RouterRuntimeTest.php

-
message: "#^Method Nucleos\\\\Twig\\\\Tests\\\\Twig\\\\Extension\\\\UrlAutoConverterTwigExtensionTest\\:\\:getLinkText\\(\\) return type has no value type specified in iterable type iterable\\.$#"
message: "#^Method Nucleos\\\\Twig\\\\Tests\\\\Runtime\\\\UrlAutoConverterRuntimeTest\\:\\:getLinkText\\(\\) return type has no value type specified in iterable type iterable\\.$#"
count: 1
path: tests/Twig/Extension/UrlAutoConverterTwigExtensionTest.php
path: tests/Runtime/UrlAutoConverterRuntimeTest.php

-
message: "#^Method Nucleos\\\\Twig\\\\Tests\\\\Util\\\\StringUtilsTest\\:\\:getObfuscatedStrings\\(\\) return type has no value type specified in iterable type iterable\\.$#"
Expand Down
21 changes: 15 additions & 6 deletions src/Bridge/Symfony/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,31 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Nucleos\Twig\Extension\RouterTwigExtension;
use Nucleos\Twig\Extension\StringTwigExtension;
use Nucleos\Twig\Extension\UrlAutoConverterTwigExtension;
use Nucleos\Twig\Extension\RouterExtension;
use Nucleos\Twig\Extension\StringExtension;
use Nucleos\Twig\Extension\UrlAutoConverterExtension;
use Symfony\Component\DependencyInjection\Reference;

return static function (ContainerConfigurator $container): void {
$container->services()

->set(UrlAutoConverterTwigExtension::class)
->set(UrlAutoConverterExtension::class)
->tag('twig.extension')

->set(StringTwigExtension::class)
->set(StringExtension::class)
->tag('twig.extension')

->set(RouterTwigExtension::class)
->set(RouterExtension::class)
->tag('twig.extension')

->set(UrlAutoConverterExtension::class)
->tag('twig.runtime')

->set(StringExtension::class)
->tag('twig.runtime')

->set(RouterExtension::class)
->tag('twig.runtime')
->args([
new Reference('router'),
])
Expand Down
34 changes: 34 additions & 0 deletions src/Extension/RouterExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nucleos\Twig\Extension;

use Nucleos\Twig\Runtime\RouterRuntime;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

final class RouterExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('routeExists', [RouterRuntime::class, 'routeExists']),
];
}

public function getFilters(): array
{
return [
new TwigFilter('splitTag', [RouterRuntime::class, 'splitTag']),
];
}
}
27 changes: 27 additions & 0 deletions src/Extension/StringExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nucleos\Twig\Extension;

use Nucleos\Twig\Runtime\StringRuntime;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

final class StringExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('format_bytes', [StringRuntime::class, 'formatBytes']),
new TwigFilter('obfuscate', [StringRuntime::class, 'obfuscate']),
];
}
}
26 changes: 26 additions & 0 deletions src/Extension/UrlAutoConverterExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nucleos\Twig\Extension;

use Nucleos\Twig\Runtime\UrlAutoConverterRuntime;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

final class UrlAutoConverterExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('converturls', [UrlAutoConverterRuntime::class, 'convertLinks'], ['is_safe' => ['html']]),
];
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nucleos\Twig\Extension;
namespace Nucleos\Twig\Runtime;

use Symfony\Component\Routing\RouterInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\Extension\RuntimeExtensionInterface;

final class RouterTwigExtension extends AbstractExtension
final class RouterRuntime implements RuntimeExtensionInterface
{
private RouterInterface $router;

Expand All @@ -25,20 +21,6 @@ public function __construct(RouterInterface $router)
$this->router = $router;
}

public function getFunctions(): array
{
return [
new TwigFunction('routeExists', [$this, 'routeExists']),
];
}

public function getFilters(): array
{
return [
new TwigFilter('splitTag', [$this, 'splitTag']),
];
}

public function routeExists(string $name): bool
{
return null !== $this->router->getRouteCollection()->get($name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nucleos\Twig\Extension;
namespace Nucleos\Twig\Runtime;

use Locale;
use Nucleos\Twig\Util\StringUtils;
use NumberFormatter;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

final class StringTwigExtension extends AbstractExtension
final class StringRuntime
{
public function getFilters(): array
{
return [
new TwigFilter('format_bytes', [$this, 'formatBytes']),
new TwigFilter('obfuscate', [$this, 'obfuscate']),
];
}

public function formatBytes(float $bytes, bool $si = true, int $fractionDigits = 0, ?string $locale = null): string
{
if (null === $locale) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nucleos\Twig\Extension;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
namespace Nucleos\Twig\Runtime;

final class UrlAutoConverterTwigExtension extends AbstractExtension
final class UrlAutoConverterRuntime
{
public function getFilters(): array
{
return [
new TwigFilter(
'converturls',
[$this, 'convertLinks'],
[
'is_safe' => ['html'],
]
),
];
}

/**
* @param array<string, string> $options
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
* file that was distributed with this source code.
*/

namespace Nucleos\Twig\Tests\Twig\Extension;
namespace Nucleos\Twig\Tests\Bridge\Symfony\App;

use Nucleos\Twig\Tests\Bridge\Symfony\App\AppKernel;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpKernel\Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Nucleos\Twig\Bridge\Symfony\DependencyInjection\NucleosTwigExtension;
use Nucleos\Twig\Extension\RouterTwigExtension;
use Nucleos\Twig\Extension\StringTwigExtension;
use Nucleos\Twig\Extension\UrlAutoConverterTwigExtension;
use Nucleos\Twig\Extension\RouterExtension;
use Nucleos\Twig\Extension\StringExtension;
use Nucleos\Twig\Extension\UrlAutoConverterExtension;

final class NucleosTwigExtensionTest extends AbstractExtensionTestCase
{
public function testLoadDefault(): void
{
$this->load();

$this->assertContainerBuilderHasService(UrlAutoConverterTwigExtension::class);
$this->assertContainerBuilderHasService(StringTwigExtension::class);
$this->assertContainerBuilderHasService(RouterTwigExtension::class);
$this->assertContainerBuilderHasService(UrlAutoConverterExtension::class);
$this->assertContainerBuilderHasService(StringExtension::class);
$this->assertContainerBuilderHasService(RouterExtension::class);
}

protected function getContainerExtensions(): array
Expand Down
Loading

0 comments on commit 3d2f371

Please sign in to comment.