diff --git a/src/Frame.php b/src/Frame.php index 6576c768cb..cbbe10e93e 100644 --- a/src/Frame.php +++ b/src/Frame.php @@ -36,15 +36,17 @@ public function getRootNode(): self return $node; } - public function getName(): string + public function getName(bool $emptyParent = false): string { if ($this->parent && str_contains($this->name, '$parent')) { $parent = $this; $parentName = ''; - while ($parent = $parent->getParent()) { - $parentName = $parent->getName(); - if ($parentName) { - break; + if (!$emptyParent) { + while ($parent = $parent->getParent()) { + $parentName = $parent->getName(); + if ($parentName) { + break; + } } } return str_replace('$parent', $parentName, $this->name); @@ -55,17 +57,16 @@ public function getName(): string public function getClassName(): ?string { + if ($this->getName() && $this->getRootNode()::class === Frame::class) { + return $this->sanitizeClassName($this->getName()); + } $prefix = $this->parent?->getClassName() ?? ''; - $name = $this->getName() ?: $this->getParentKey(); - if (!$name) { + $name = $this->getName(true) ?: $this->getParentKey(); + if (!$name || ($this->parent && !$prefix)) { return null; } - return str_replace( - ['$', ' ', '-', '.'], - ['', '_', '_', '_'], - $prefix === '' ? $name : $prefix . '_' . $name, - ); + return $this->sanitizeClassName($prefix === '' ? $name : ($prefix . '_' . $name)); } public function getParent(): ?self @@ -140,7 +141,9 @@ public function getInherits(): array $inherits = (string) $this->xmlElement->attributes()['inherits'] ?? ''; $inherits = str_replace(' ', '', $inherits); - return $inherits === '' ? [] : explode(',', $inherits); + return $inherits === '' + ? [] + : array_map($this->sanitizeClassName(...), explode(',', $inherits)); } /** @@ -189,4 +192,13 @@ public function getLineNumber(): int return $node->getLineNo(); } + + private function sanitizeClassName(string $name): string + { + return str_replace( + ['$', ' ', '-', '.', '!'], + ['', '_', '_', '_', '_'], + $name, + ); + } } diff --git a/src/Registry.php b/src/Registry.php index c9080391f5..e014e55f39 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -11,12 +11,12 @@ class Registry { /** - * @var T[] + * @var array */ private array $items = []; /** - * @param T $template + * @param T&Frame $template */ public function register(?string $name, Frame $template): void { @@ -27,7 +27,7 @@ public function register(?string $name, Frame $template): void } /** - * @return T|null + * @return (T&Frame)|null */ public function get(string $name): ?Frame { @@ -35,7 +35,7 @@ public function get(string $name): ?Frame } /** - * @return T[] + * @return array */ public function all(): array { diff --git a/src/XmlFileParser.php b/src/XmlFileParser.php index 1890affc49..86863f1539 100644 --- a/src/XmlFileParser.php +++ b/src/XmlFileParser.php @@ -103,7 +103,7 @@ private function parseNode(SimpleXMLElement $node, Registry $fileRegistry, ?Fram $this->frameRegistry->register($frame->getClassName(), $frame); } if (!empty($name)) { - $fileRegistry->register($name, $frame); + $fileRegistry->register($frame->getClassName(), $frame); } $parent?->addChild($frame);