Skip to content

Commit 5311d7e

Browse files
committed
Added loads more annotated things - mainly focused on templates that result in globals being created
1 parent fb41668 commit 5311d7e

File tree

2 files changed

+90
-19
lines changed

2 files changed

+90
-19
lines changed

src/Frame.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44

55
namespace App;
66

7+
use RuntimeException;
78
use SimpleXMLElement;
89

910
class Frame
1011
{
12+
private readonly ?self $originalParent;
13+
1114
public function __construct(
1215
private readonly string $name,
1316
private readonly string $type,
1417
private readonly SimpleXMLElement $xmlElement,
15-
private readonly ?self $parent = null,
18+
private ?self $parent = null,
1619
private array $children = [],
1720
) {
21+
$this->originalParent = $parent;
1822
}
1923

2024
public function isRootNode(): bool
@@ -57,6 +61,19 @@ public function getParent(): ?self
5761
return $this->parent;
5862
}
5963

64+
public function withParent(self $parent): self
65+
{
66+
$clone = clone $this;
67+
$clone->parent = $parent;
68+
69+
return $clone;
70+
}
71+
72+
public function getOriginalParent(): ?self
73+
{
74+
return $this->originalParent;
75+
}
76+
6077
public function getParentKey(): ?string
6178
{
6279
$parentKey = (string) $this->xmlElement->attributes()['parentKey'] ?? '';
@@ -102,7 +119,7 @@ public function getInherits(): array
102119
}
103120

104121
/**
105-
* @return array<string, string> [key => formattedValue] value is formatted to be written directly into lua
122+
* @return array<string, array{1: string, 2: string}> [key => [formattedValue, type]] value is formatted to be written directly into lua
106123
*/
107124
public function getKeyValues(): array
108125
{
@@ -111,18 +128,31 @@ public function getKeyValues(): array
111128
}
112129
$keyValues = [];
113130
foreach ($this->xmlElement->KeyValues as $child) {
114-
$key = (string) $child->attributes()['key'] ?? '';
115-
$value = (string) $child->attributes()['value'] ?? '';
116-
$type = (string) $child->attributes()['type'] ?? 'string';
131+
if (!isset($child->KeyValue)) {
132+
continue;
133+
}
134+
$keyValue = $child->KeyValue;
135+
$key = (string) $keyValue->attributes()['key'] ?? '';
136+
$value = (string) $keyValue->attributes()['value'] ?? '';
137+
$type = (string) $keyValue->attributes()['type'] ?: 'string';
117138
if ($key === '') {
118139
continue;
119140
}
120141
$value = match($type) {
121-
'number', 'global' => $value,
122-
'boolean' => $value === 'true',
123-
default => json_encode($value), // json_encodes adds quotes to the string
142+
'number', 'global', 'boolean' => $value,
143+
'nil' => 'nil',
144+
'string' => json_encode($value), // json_encodes adds quotes to the string
145+
default => throw new RuntimeException("Unknown type: $type"),
146+
};
147+
$type = match($type) {
148+
'number' => 'number',
149+
'boolean' => 'boolean',
150+
'global' => 'any',
151+
'nil' => 'nil',
152+
'string' => 'string',
153+
default => throw new RuntimeException("Unknown type: $type"),
124154
};
125-
$keyValues[$key] = $value;
155+
$keyValues[$key] = [$value, $type];
126156
}
127157

128158
return $keyValues;

src/XmlFileParser.php

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,37 +170,72 @@ private function childHasInterestingData(Frame $child): bool
170170
|| !empty($child->getChildren());
171171
}
172172

173-
private function writeFrame(Frame $frame, ?string $linkPrefix): string
173+
private function writeFrame(Frame $frame, ?string $linkPrefix, ?string $typeOverride = null): string
174174
{
175175
$data = '';
176+
$globalChildrenWithParentKey = [];
176177
foreach ($frame->getChildren() as $child) {
177178
if ($this->childHasInterestingData($child)) {
178179
$data .= $this->writeFrame($child, $linkPrefix);
180+
if ($child->getName() && $child->getParentKey()) {
181+
$globalChildrenWithParentKey[$child->getParentKey()] = $child->getName();
182+
}
183+
}
184+
}
185+
$inheritedKeyValues = [];
186+
foreach ($frame->getInherits() as $templateName) {
187+
$template = $this->templateRegistry->get($templateName);
188+
if (!$template) {
189+
continue;
190+
}
191+
foreach ($template->getKeyValues() as $key => $value) {
192+
$inheritedKeyValues[$key] = $value;
193+
}
194+
foreach ($template->getChildren() as $child) {
195+
$clone = $child->withParent($frame);
196+
if (!empty($clone->getName())) { // will create a global
197+
$data .= $this->writeFrame(
198+
$clone,
199+
$linkPrefix,
200+
$this->childHasInterestingData($child) ? $child->getClassName() : $child->getType(), // this must be $child rather than $clone
201+
);
202+
if($clone->getParentKey()) {
203+
$inheritedKeyValues[$clone->getParentKey()] = [$clone->getName()];
204+
}
205+
}
179206
}
180207
}
181208

182209
if ($linkPrefix) {
183210
$data .= "--- [Source]($linkPrefix#L" . $frame->getLineNumber() . ")\n";
184211
}
185212
if ($frame->getParent()) {
186-
$data .= '--- child of ' . $frame->getParent()->getName() . "\n";
213+
$data .= '--- child of ' . $frame->getParent()->getName();
214+
if ($frame->getOriginalParent() && $frame->getOriginalParent() !== $frame->getParent()) {
215+
$data .= ' (created in template ' . $frame->getOriginalParent()->getName() . ')';
216+
}
217+
$data .= "\n";
187218
}
188219
if ($frame instanceof Intrinsic) {
189220
$data .= "--- Intrinsic\n";
190221
} elseif ($frame instanceof Template) {
191222
$data .= "--- Template\n";
192223
}
193-
$data .= '--- @class ' . $frame->getClassName() . ' : ' . $frame->getType();
194-
foreach ($frame->getInherits() as $inherit) {
195-
$data .= ', ' . $inherit;
196-
}
197-
foreach ($frame->getMixins() as $mixin) {
198-
$data .= ', ' . $mixin;
224+
if ($typeOverride) {
225+
$data .= '--- @type ' . $typeOverride;
226+
} else {
227+
$data .= '--- @class ' . $frame->getClassName() . ' : ' . $frame->getType();
228+
foreach ($frame->getInherits() as $inherit) {
229+
$data .= ', ' . $inherit;
230+
}
231+
foreach ($frame->getMixins() as $mixin) {
232+
$data .= ', ' . $mixin;
233+
}
199234
}
200235
$data .= "\n";
201236

202237
foreach ($frame->getKeyValues() as $key => $value) {
203-
$data .= '--- @field ' . $key . ' ' . $value . "\n";
238+
$data .= '--- @field ' . $key . ' ' . $value[1] . ' # ' . $value[0] . "\n";
204239
}
205240
foreach ($frame->getChildren() as $child) {
206241
if ($child->getParentKey()) {
@@ -213,9 +248,15 @@ private function writeFrame(Frame $frame, ?string $linkPrefix): string
213248
}
214249
if ($frame->getName() && $frame->getRootNode()::class === Frame::class) {
215250
$data .= $frame->getName() . " = {}\n";
216-
foreach ($frame->getKeyValues() as $key => $value) {
251+
foreach ($globalChildrenWithParentKey as $key => $value) {
217252
$data .= $frame->getName() . '["' . $key . '"] = ' . $value . "\n";
218253
}
254+
foreach ($frame->getKeyValues() as $key => $value) {
255+
$data .= $frame->getName() . '["' . $key . '"] = ' . $value[0] . "\n";
256+
}
257+
foreach ($inheritedKeyValues as $key => $value) {
258+
$data .= $frame->getName() . '["' . $key . '"] = ' . $value[0] . " -- inherited\n";
259+
}
219260
}
220261

221262
return $data . "\n";

0 commit comments

Comments
 (0)