Split extension into extension and runtime classes to match how the maker bundle creates twig extensions
This commit is contained in:
@@ -5,10 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Pcm\IconBundle;
|
namespace Pcm\IconBundle;
|
||||||
|
|
||||||
use Pcm\IconBundle\DependencyInjection\PcmIconExtension;
|
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\Extension\ExtensionInterface;
|
||||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
|
||||||
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
|
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
|
||||||
|
|
||||||
class PcmIconBundle extends AbstractBundle
|
class PcmIconBundle extends AbstractBundle
|
||||||
|
|||||||
22
src/Twig/Extension/IconExtension.php
Normal file
22
src/Twig/Extension/IconExtension.php
Normal 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'])
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,12 +2,13 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Pcm\IconBundle\Twig\Functions;
|
namespace Pcm\IconBundle\Twig\Runtime;
|
||||||
|
|
||||||
use Twig\Extension\AbstractExtension;
|
use Pcm\IconBundle\Exception\ColourNotFound;
|
||||||
use Twig\TwigFunction;
|
use Pcm\IconBundle\Exception\IconNotFound;
|
||||||
|
use Twig\Extension\RuntimeExtensionInterface;
|
||||||
|
|
||||||
final class IconExtension extends AbstractExtension
|
final class IconRuntime implements RuntimeExtensionInterface
|
||||||
{
|
{
|
||||||
public const DEFAULT_SIZE = 32;
|
public const DEFAULT_SIZE = 32;
|
||||||
|
|
||||||
@@ -22,18 +23,6 @@ final class IconExtension extends AbstractExtension
|
|||||||
|
|
||||||
public function __construct(private array $directories, private array $colours) {}
|
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
|
* @param array $options
|
||||||
* ```
|
* ```
|
||||||
@@ -211,7 +200,3 @@ final class IconExtension extends AbstractExtension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class IconNotFound extends \Exception {};
|
|
||||||
|
|
||||||
class ColourNotFound extends \Exception {};
|
|
||||||
@@ -4,12 +4,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\IconBundle\Tests\Twig\Functions;
|
namespace Pcm\IconBundle\Tests\Twig\Functions;
|
||||||
|
|
||||||
use Pcm\IconBundle\Twig\Functions\IconExtension;
|
use Pcm\IconBundle\Exception\ColourNotFound;
|
||||||
use Pcm\IconBundle\Twig\Functions\IconNotFound;
|
use Pcm\IconBundle\Exception\IconNotFound;
|
||||||
use Pcm\IconBundle\Twig\Functions\ColourNotFound;
|
use Pcm\IconBundle\Twig\Runtime\IconRuntime;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class IconExtensionTest extends TestCase
|
class IconRuntimeTest extends TestCase
|
||||||
{
|
{
|
||||||
private const ICON = 'test';
|
private const ICON = 'test';
|
||||||
|
|
||||||
@@ -32,11 +32,14 @@ class IconExtensionTest extends TestCase
|
|||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
private IconExtension $icon;
|
private IconRuntime $icon;
|
||||||
|
|
||||||
protected function setUp(): void
|
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
|
public function testThrowsWhenPassedAnInvalidIconName(): void
|
||||||
@@ -119,7 +122,7 @@ class IconExtensionTest extends TestCase
|
|||||||
|
|
||||||
public function testDefaultSizeIsSetOnSvgIfNoSizeOptionPassed(): void
|
public function testDefaultSizeIsSetOnSvgIfNoSizeOptionPassed(): void
|
||||||
{
|
{
|
||||||
$defaultSize = IconExtension::DEFAULT_SIZE;
|
$defaultSize = IconRuntime::DEFAULT_SIZE;
|
||||||
$content = $this->icon->renderIcon(['icon' => self::ICON]);
|
$content = $this->icon->renderIcon(['icon' => self::ICON]);
|
||||||
|
|
||||||
$regex = "/^<svg.+?width=\"{$defaultSize}\"/";
|
$regex = "/^<svg.+?width=\"{$defaultSize}\"/";
|
||||||
@@ -131,7 +134,7 @@ class IconExtensionTest extends TestCase
|
|||||||
|
|
||||||
public function testDefaultSizeIsOnlySetOnce(): void
|
public function testDefaultSizeIsOnlySetOnce(): void
|
||||||
{
|
{
|
||||||
$defaultSize = IconExtension::DEFAULT_SIZE;
|
$defaultSize = IconRuntime::DEFAULT_SIZE;
|
||||||
$content = $this->icon->renderIcon(['icon' => self::ICON]);
|
$content = $this->icon->renderIcon(['icon' => self::ICON]);
|
||||||
|
|
||||||
$widthRegex = "/width=\"{$defaultSize}\"/";
|
$widthRegex = "/width=\"{$defaultSize}\"/";
|
||||||
Reference in New Issue
Block a user