From a31b398b0cec43363ee47041f9ed56d20359b958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 14 May 2024 13:50:25 +0200 Subject: [PATCH] Allow the renderer argument to be null --- src/Renderer/CompositeRenderer.php | 7 +++++++ src/TagBag.php | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Renderer/CompositeRenderer.php b/src/Renderer/CompositeRenderer.php index 148f3be..212d352 100644 --- a/src/Renderer/CompositeRenderer.php +++ b/src/Renderer/CompositeRenderer.php @@ -12,6 +12,13 @@ final class CompositeRenderer implements RendererInterface /** @var list */ private array $renderers = []; + public function __construct(RendererInterface ...$renderers) + { + foreach ($renderers as $renderer) { + $this->add($renderer); + } + } + public function add(RendererInterface $renderer): void { $this->renderers[] = $renderer; diff --git a/src/TagBag.php b/src/TagBag.php index dd0e2ec..c63413b 100644 --- a/src/TagBag.php +++ b/src/TagBag.php @@ -16,6 +16,9 @@ use Setono\TagBag\Exception\UnsupportedTagException; use Setono\TagBag\Generator\FingerprintGeneratorInterface; use Setono\TagBag\Generator\ValueBasedFingerprintGenerator; +use Setono\TagBag\Renderer\CompositeRenderer; +use Setono\TagBag\Renderer\ContentAwareRenderer; +use Setono\TagBag\Renderer\ElementRenderer; use Setono\TagBag\Renderer\RendererInterface; use Setono\TagBag\Storage\StorageInterface; use Setono\TagBag\Tag\RenderedTag; @@ -34,11 +37,16 @@ final class TagBag implements TagBagInterface, LoggerAwareInterface private ?EventDispatcherInterface $eventDispatcher = null; + private readonly RendererInterface $renderer; + private readonly FingerprintGeneratorInterface $fingerprintGenerator; - public function __construct(private readonly RendererInterface $renderer, FingerprintGeneratorInterface $fingerprintGenerator = null) - { + public function __construct( + RendererInterface $renderer = null, + FingerprintGeneratorInterface $fingerprintGenerator = null, + ) { $this->logger = new NullLogger(); + $this->renderer = $renderer ?? new CompositeRenderer(new ElementRenderer(), new ContentAwareRenderer()); $this->fingerprintGenerator = $fingerprintGenerator ?? new ValueBasedFingerprintGenerator(); }