Skip to content

Commit 76543f6

Browse files
committed
Improved handling of nested templates, and fix issue with frames that have a $ in their name (besides $parent)
1 parent eb726fc commit 76543f6

File tree

2 files changed

+62
-30
lines changed

2 files changed

+62
-30
lines changed

src/Frame.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ public function getRootNode(): self
3939
public function getName(): string
4040
{
4141
if ($this->parent && str_contains($this->name, '$parent')) {
42-
return str_replace('$parent', $this->parent->getName(), $this->name);
42+
$parent = $this;
43+
$parentName = '';
44+
while ($parent = $parent->getParent()) {
45+
$parentName = $parent->getName();
46+
if ($parentName) {
47+
break;
48+
}
49+
}
50+
return str_replace('$parent', $parentName, $this->name);
4351
}
4452

4553
return $this->name;
@@ -53,7 +61,11 @@ public function getClassName(): ?string
5361
return null;
5462
}
5563

56-
return $prefix === '' ? $name : $prefix . '_' . $name;
64+
return str_replace(
65+
['$', ' ', '-', '.'],
66+
['', '_', '_', '_'],
67+
$prefix === '' ? $name : $prefix . '_' . $name,
68+
);
5769
}
5870

5971
public function getParent(): ?self

src/XmlFileParser.php

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private function childHasInterestingData(Frame $child): bool
172172

173173
private function writeFrame(Frame $frame, ?string $linkPrefix, ?string $typeOverride = null): string
174174
{
175-
$shouldWriteGlobal = $frame->getName() && $frame->getRootNode()::class === Frame::class;
175+
$shouldWriteGlobal = $frame->getRootNode()->getName() && $frame->getRootNode()::class === Frame::class;
176176
$data = '';
177177
$globalChildrenWithParentKey = [];
178178
$inheritedKeyValues = [];
@@ -185,31 +185,12 @@ private function writeFrame(Frame $frame, ?string $linkPrefix, ?string $typeOver
185185
}
186186
}
187187
if ($shouldWriteGlobal) {
188-
foreach ($frame->getInherits() as $templateName) {
189-
$template = $this->templateRegistry->get($templateName);
190-
if (!$template) {
191-
continue;
192-
}
193-
foreach ($template->getKeyValues() as $key => $value) {
194-
$inheritedKeyValues[$key] = $value;
195-
}
196-
foreach ($template->getChildren() as $child) {
197-
$clone = $child->withParent($frame);
198-
if (!empty($clone->getName())) { // will create a global
199-
$data .= $this->writeFrame(
200-
$clone,
201-
$linkPrefix,
202-
// this must be $child rather than $clone
203-
$this->childHasInterestingData($child)
204-
? $child->getClassName()
205-
: $child->getType(),
206-
);
207-
if ($clone->getParentKey()) {
208-
$inheritedKeyValues[$clone->getParentKey()] = [$clone->getName()];
209-
}
210-
}
211-
}
212-
}
188+
$this->handleInherits(
189+
$frame,
190+
$inheritedKeyValues,
191+
$linkPrefix,
192+
$data
193+
);
213194
}
214195

215196
if ($linkPrefix) {
@@ -252,8 +233,12 @@ private function writeFrame(Frame $frame, ?string $linkPrefix, ?string $typeOver
252233
}
253234
}
254235
}
255-
if ($shouldWriteGlobal) {
256-
$data .= $frame->getName() . " = {}\n";
236+
if ($shouldWriteGlobal && $frame->getName()) {
237+
$name = $frame->getName();
238+
if (str_contains($name, '$')) {
239+
$name = '_G["' . $name . '"]';
240+
}
241+
$data .= $name . " = {}\n";
257242
foreach ($globalChildrenWithParentKey as $key => $value) {
258243
$data .= $frame->getName() . '["' . $key . '"] = ' . $value . "\n";
259244
}
@@ -267,4 +252,39 @@ private function writeFrame(Frame $frame, ?string $linkPrefix, ?string $typeOver
267252

268253
return $data . "\n";
269254
}
255+
256+
private function handleInherits(Frame $frame, array &$inheritedKeyValues, ?string $linkPrefix, string &$data): void
257+
{
258+
foreach ($frame->getInherits() as $templateName) {
259+
$template = $this->templateRegistry->get($templateName);
260+
if (!$template) {
261+
continue;
262+
}
263+
$template = $template->withParent($frame);
264+
$this->handleInherits($template, $inheritedKeyValues, $linkPrefix, $data);
265+
foreach ($template->getKeyValues() as $key => $value) {
266+
$inheritedKeyValues[$key] = $value;
267+
}
268+
foreach ($template->getChildren() as $child) {
269+
$clone = $child->withParent($frame);
270+
if (!empty($clone->getName())) { // will create a global
271+
$data .= $this->writeFrame(
272+
$clone,
273+
$linkPrefix,
274+
// this must be $child rather than $clone
275+
$this->childHasInterestingData($child)
276+
? $child->getClassName()
277+
: $child->getType(),
278+
);
279+
if ($clone->getParentKey()) {
280+
$name = $clone->getName();
281+
if (str_contains($name, '$')) {
282+
$name = '_G["' . $name . '"]';
283+
}
284+
$inheritedKeyValues[$clone->getParentKey()] = [$name];
285+
}
286+
}
287+
}
288+
}
289+
}
270290
}

0 commit comments

Comments
 (0)