diff --git a/composer.json b/composer.json index 33ba5a7..4c006a6 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "require": { "php": "^7.2", - "setono/tag-bag": "^1.0", + "setono/tag-bag": "^1.3", "twig/twig": "^2.0 || ^3.0" }, "require-dev": { diff --git a/src/Renderer/TwigRenderer.php b/src/Renderer/TwigRenderer.php index b874da3..43cb184 100644 --- a/src/Renderer/TwigRenderer.php +++ b/src/Renderer/TwigRenderer.php @@ -5,6 +5,7 @@ namespace Setono\TagBag\Renderer; use Setono\TagBag\Tag\TagInterface; +use Setono\TagBag\Tag\TemplateTagInterface; use Setono\TagBag\Tag\TwigTagInterface; use Twig\Environment; @@ -20,9 +21,12 @@ public function __construct(Environment $environment) public function supports(TagInterface $tag): bool { - return $tag instanceof TwigTagInterface; + return $tag instanceof TwigTagInterface || ($tag instanceof TemplateTagInterface && $tag->getTemplateType() === 'twig'); } + /** + * @param TemplateTagInterface|TagInterface $tag + */ public function render(TagInterface $tag): string { return $this->environment->render($tag->getTemplate(), $tag->getContext()); diff --git a/src/Tag/TwigTag.php b/src/Tag/TwigTag.php index 63658cd..edb2d2d 100644 --- a/src/Tag/TwigTag.php +++ b/src/Tag/TwigTag.php @@ -4,37 +4,9 @@ namespace Setono\TagBag\Tag; -class TwigTag extends Tag implements TwigTagInterface +class TwigTag extends TemplateTag implements TwigTagInterface { - /** @var string */ - private $template; - - /** @var array */ - private $context; - - public function __construct(string $template, array $context = []) - { - $this->setName('setono_tag_bag_twig_tag'); - $this->template = $template; - $this->context = $context; - } - - public function getTemplate(): string - { - return $this->template; - } - - public function getContext(): array - { - return $this->context; - } - - public function setContext(array $context): self - { - $this->context = $context; - - return $this; - } + protected $name = 'setono_tag_bag_twig_tag'; /** * @param mixed $value diff --git a/src/Tag/TwigTagInterface.php b/src/Tag/TwigTagInterface.php index 1f483c9..a7f1304 100644 --- a/src/Tag/TwigTagInterface.php +++ b/src/Tag/TwigTagInterface.php @@ -4,15 +4,6 @@ namespace Setono\TagBag\Tag; -interface TwigTagInterface extends TagInterface +interface TwigTagInterface extends TemplateTagInterface { - /** - * Returns the twig template - */ - public function getTemplate(): string; - - /** - * Returns the context to inject into the twig template when rendered - */ - public function getContext(): array; } diff --git a/tests/Renderer/TwigRendererTest.php b/tests/Renderer/TwigRendererTest.php index 008f7e8..ebc503c 100644 --- a/tests/Renderer/TwigRendererTest.php +++ b/tests/Renderer/TwigRendererTest.php @@ -22,7 +22,7 @@ public function it_supports_twig_tag_interface(): void { $renderer = new TwigRenderer($this->getEnvironment()); - $this->assertTrue($renderer->supports(new TwigTag('template'))); + self::assertTrue($renderer->supports(new TwigTag('template'))); } /** @@ -32,7 +32,7 @@ public function it_does_not_support_tag_interface(): void { $renderer = new TwigRenderer($this->getEnvironment()); - $this->assertFalse($renderer->supports(new class() extends Tag { + self::assertFalse($renderer->supports(new class() extends Tag { })); } @@ -54,7 +54,7 @@ public function it_renders(): void 'context_key' => 'context_value', ]); - $this->assertSame('content', $renderer->render($tag)); + self::assertSame('content', $renderer->render($tag)); } /**