diff --git a/src/TagEngine.php b/src/TagEngine.php index f5a6967..1e3b3e4 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 ReflectionClass; use Spatie\StructureDiscoverer\Discover; @@ -47,6 +46,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,8 +133,17 @@ protected function registerTags(): void // This is quite expensive, so only do it once $classes = Discover::in($tag_directory)->classes() ->extending(CustomTag::class)->get(); - /** @var \LordSimal\CustomHtmlElements\CustomTag|string $class */ foreach ($classes as $class) { + if (!is_subclass_of($class, CustomTag::class)) { + continue; + } + + $tagName = (new ReflectionClass($class))->getStaticPropertyValue('tag'); + if (!is_string($tagName)) { + continue; + } + + $this->tags[$tagName] = $class; TagRegistry::register($class); } } @@ -236,11 +250,8 @@ protected function parseAttributes(string $attributesString): array */ protected function renderComponent(string $componentName, array $attributes, string $innerContent = ''): string { - try { - $class = TagRegistry::getTag(sprintf('%s-%s', $this->options['component_prefix'], $componentName)); - } catch (TagNotFoundException) { - $class = null; - } + $tagName = sprintf('%s-%s', $this->options['component_prefix'], $componentName); + $class = $this->tags[$tagName] ?? null; if ($this->options['enable_cache']) { $cacheKey = hash('sha256', serialize([ @@ -261,7 +272,8 @@ protected function renderComponent(string $componentName, array $attributes, str if ($class !== null) { $tag = new $class($attributes, $innerContent); - if ($tag->disabled) { + $properties = get_object_vars($tag); + if (($attributes['disabled'] ?? false) || ($properties['disabled'] ?? false)) { return ''; } } else { 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('')); + } + public function testCanBeCreatedWithDefaultOptions(): void { $engine = new TagEngine();