Split extension into extension and runtime classes to match how the maker bundle creates twig extensions

This commit is contained in:
Brabli
2023-05-31 11:51:36 +01:00
parent 08d906260a
commit 493f0c275b
4 changed files with 38 additions and 31 deletions

View File

@@ -5,10 +5,7 @@ declare(strict_types=1);
namespace Pcm\IconBundle;
use Pcm\IconBundle\DependencyInjection\PcmIconExtension;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
class PcmIconBundle extends AbstractBundle

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Pcm\IconBundle\Twig\Extension;
use Pcm\IconBundle\Twig\Runtime\IconRuntime;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
final class IconExtension extends AbstractExtension
{
/**
* @inheritDoc
*/
public function getFunctions(): array
{
return [
new TwigFunction('icon', [IconRuntime::class, 'renderIcon'])
];
}
}

View File

@@ -2,12 +2,13 @@
declare(strict_types=1);
namespace Pcm\IconBundle\Twig\Functions;
namespace Pcm\IconBundle\Twig\Runtime;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Pcm\IconBundle\Exception\ColourNotFound;
use Pcm\IconBundle\Exception\IconNotFound;
use Twig\Extension\RuntimeExtensionInterface;
final class IconExtension extends AbstractExtension
final class IconRuntime implements RuntimeExtensionInterface
{
public const DEFAULT_SIZE = 32;
@@ -22,18 +23,6 @@ final class IconExtension extends AbstractExtension
public function __construct(private array $directories, private array $colours) {}
/**
* @inheritDoc
*/
public function getFunctions(): array
{
return [
new TwigFunction('icon', [$this, 'renderIcon'], [
'is_safe' => ['html']
])
];
}
/**
* @param array $options
* ```
@@ -211,7 +200,3 @@ final class IconExtension extends AbstractExtension
}
}
}
class IconNotFound extends \Exception {};
class ColourNotFound extends \Exception {};

View File

@@ -4,12 +4,12 @@ declare(strict_types=1);
namespace Pcm\IconBundle\Tests\Twig\Functions;
use Pcm\IconBundle\Twig\Functions\IconExtension;
use Pcm\IconBundle\Twig\Functions\IconNotFound;
use Pcm\IconBundle\Twig\Functions\ColourNotFound;
use Pcm\IconBundle\Exception\ColourNotFound;
use Pcm\IconBundle\Exception\IconNotFound;
use Pcm\IconBundle\Twig\Runtime\IconRuntime;
use PHPUnit\Framework\TestCase;
class IconExtensionTest extends TestCase
class IconRuntimeTest extends TestCase
{
private const ICON = 'test';
@@ -32,11 +32,14 @@ class IconExtensionTest extends TestCase
]
];
private IconExtension $icon;
private IconRuntime $icon;
protected function setUp(): void
{
$this->icon = new IconExtension(['tests/icons'], self::COLOURS);
$this->icon = new IconRuntime(
directories: ['tests/icons'],
colours: self::COLOURS
);
}
public function testThrowsWhenPassedAnInvalidIconName(): void
@@ -119,7 +122,7 @@ class IconExtensionTest extends TestCase
public function testDefaultSizeIsSetOnSvgIfNoSizeOptionPassed(): void
{
$defaultSize = IconExtension::DEFAULT_SIZE;
$defaultSize = IconRuntime::DEFAULT_SIZE;
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$regex = "/^<svg.+?width=\"{$defaultSize}\"/";
@@ -131,7 +134,7 @@ class IconExtensionTest extends TestCase
public function testDefaultSizeIsOnlySetOnce(): void
{
$defaultSize = IconExtension::DEFAULT_SIZE;
$defaultSize = IconRuntime::DEFAULT_SIZE;
$content = $this->icon->renderIcon(['icon' => self::ICON]);
$widthRegex = "/width=\"{$defaultSize}\"/";