From 74e0a4045475a4bda7567911accf0cf2251d7aba Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Sat, 12 Sep 2026 07:10:47 +0200 Subject: [PATCH] Keep tag registrations isolated per engine --- src/TagEngine.php | 15 +++++++++++---- tests/RegistryTags/First/Collision.php | 16 ++++++++++++++++ tests/RegistryTags/Second/Collision.php | 16 ++++++++++++++++ tests/TagEngine/TagEngineTest.php | 13 +++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 tests/RegistryTags/First/Collision.php create mode 100644 tests/RegistryTags/Second/Collision.php diff --git a/src/TagEngine.php b/src/TagEngine.php index 8f649d3..0211f51 100644 --- a/src/TagEngine.php +++ b/src/TagEngine.php @@ -5,7 +5,6 @@ use LordSimal\CustomHtmlElements\Error\ConfigException; use LordSimal\CustomHtmlElements\Error\RegexException; -use LordSimal\CustomHtmlElements\Error\TagNotFoundException; use Spatie\StructureDiscoverer\Discover; class TagEngine @@ -46,6 +45,11 @@ class TagEngine */ protected array $discovery_cache = []; + /** + * @var array> + */ + protected array $tags = []; + /** * Initialize TagEngine * @@ -57,6 +61,7 @@ public function __construct(array $options = []) if ($options) { $this->options = array_merge($this->options, $options); } + $this->tags = TagRegistry::getTags(); $this->setRegex(); $this->registerTags(); @@ -128,6 +133,7 @@ protected function registerTags(): void ->extending(CustomTag::class)->get(); /** @var \LordSimal\CustomHtmlElements\CustomTag|string $class */ foreach ($classes as $class) { + $this->tags[$class::$tag] = $class; TagRegistry::register($class); } } @@ -242,14 +248,15 @@ protected function renderComponent(string $componentName, array $attributes, str } } - try { - $class = TagRegistry::getTag(sprintf('%s-%s', $this->options['component_prefix'], $componentName)); + $tagName = sprintf('%s-%s', $this->options['component_prefix'], $componentName); + if (isset($this->tags[$tagName])) { + $class = $this->tags[$tagName]; $tag = new $class($attributes, $innerContent); if ($tag->disabled) { return ''; } - } catch (TagNotFoundException) { + } else { $tag = new SimpleTag($attributes, $innerContent); $tag::$tag = $componentName; } diff --git a/tests/RegistryTags/First/Collision.php b/tests/RegistryTags/First/Collision.php new file mode 100644 index 0000000..e1df58f --- /dev/null +++ b/tests/RegistryTags/First/Collision.php @@ -0,0 +1,16 @@ + [dirname(__DIR__) . '/RegistryTags/First'], + ]); + $second = new TagEngine([ + 'tag_directories' => [dirname(__DIR__) . '/RegistryTags/Second'], + ]); + + $this->assertSame('first', $first->parse('')); + $this->assertSame('second', $second->parse('')); + } + /** * Test singleton instance creation *