Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/TagEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -47,6 +46,11 @@ class TagEngine
*/
protected array $discovery_cache = [];

/**
* @var array<string, class-string<\LordSimal\CustomHtmlElements\CustomTag>>
*/
protected array $tags = [];

/**
* Initialize TagEngine
*
Expand All @@ -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();

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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([
Expand All @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions tests/RegistryTags/First/Collision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace LordSimal\CustomHtmlElements\Test\RegistryTags\First;

use LordSimal\CustomHtmlElements\CustomTag;

class Collision extends CustomTag
{
public static string $tag = 'c-collision';

public function render(): string
{
return 'first';
}
}
16 changes: 16 additions & 0 deletions tests/RegistryTags/Second/Collision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace LordSimal\CustomHtmlElements\Test\RegistryTags\Second;

use LordSimal\CustomHtmlElements\CustomTag;

class Collision extends CustomTag
{
public static string $tag = 'c-collision';

public function render(): string
{
return 'second';
}
}
13 changes: 13 additions & 0 deletions tests/TagEngine/TagEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
*/
class TagEngineTest extends TestCase
{
public function testEnginesKeepTheirOwnTagRegistrations(): void
{
$first = new TagEngine([
'tag_directories' => [dirname(__DIR__) . '/RegistryTags/First'],
]);
$second = new TagEngine([
'tag_directories' => [dirname(__DIR__) . '/RegistryTags/Second'],
]);

$this->assertSame('first', $first->parse('<c-collision />'));
$this->assertSame('second', $second->parse('<c-collision />'));
}

public function testCanBeCreatedWithDefaultOptions(): void
{
$engine = new TagEngine();
Expand Down
Loading