Skip to content

Commit 222143b

Browse files
committed
get better at handling ids
1 parent 48a6ce8 commit 222143b

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

src/Template/LazyTreeBuilder.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,26 @@
2727

2828
class LazyTreeBuilder extends DOMTreeBuilder
2929
{
30+
public static \WeakMap|null $idMap = null;
3031
private static int $id = 0;
31-
3232
/**
3333
* @var array<CompiledComponent>
3434
*/
3535
private array $componentStack = [];
36-
3736
private readonly StateProviderInterface $stateProvider;
3837
private readonly AuthenticationServiceInterface|null $authenticationService;
3938
/**
4039
* @var array<DOMNode>
4140
*/
4241
private array $childParents = [];
43-
4442
/**
4543
* @var array<array<CompiledComponent>>
4644
*/
4745
private array $delayStack = [];
48-
4946
/**
5047
* @var array<CompiledComponent>
5148
*/
5249
private array|null $delayStackPointer = null;
53-
5450
/**
5551
* @var array<DataProvider>
5652
*/
@@ -82,6 +78,7 @@ public function __construct(
8278
} catch (NotFoundExceptionInterface|ContainerExceptionInterface) {
8379
$this->authenticationService = null;
8480
}
81+
self::$idMap ??= new \WeakMap();
8582
}
8683

8784
/**
@@ -137,8 +134,12 @@ private function pushComponent(string $name, array $attributes): void
137134
$attributes
138135
);
139136

137+
if ($attributes['id'] ?? null) {
138+
self::$idMap[$this->componentStack[$name]] = $attributes['id'];
139+
}
140+
140141
// only count children we are actually rendering
141-
if(count($this->componentStack) === 1) {
142+
if (count($this->componentStack) === 1) {
142143
// in a weird quirk of php, this results in appending a new array to the delay stack and they are the same variable.
143144
$this->delayStackPointer = &$this->delayStack[];
144145
$this->delayStackPointer = [];
@@ -207,17 +208,15 @@ private function decorateForm(string $name, array $attributes): void
207208
$idElement = $this->doc->createElement('input');
208209
$idElement->setAttribute('type', 'hidden');
209210
$idElement->setAttribute('name', 'target_id');
210-
$idElement->setAttribute('value', $this->calculateId(self::$id));
211+
$idElement->setAttribute(
212+
'value',
213+
self::$idMap[$this->parentComponent ?? reset($this->componentStack)] ?? 'null'
214+
);
211215
$this->current->appendChild($idElement);
212216
}
213217
}
214218
}
215219

216-
private function calculateId(int $id): string
217-
{
218-
return substr('c' . md5((string)$id), 0, 8);
219-
}
220-
221220
/**
222221
* @param string $name
223222
* @return void
@@ -304,10 +303,18 @@ private function decorateComponent(CompiledComponent|false $component): void
304303
}
305304
$id = $this->calculateId(++self::$id);
306305
if ($this->current instanceof DOMElement && !$this->current->hasAttribute('id')) {
306+
if ($component = end($this->componentStack)) {
307+
self::$idMap[$component] = $id;
308+
}
307309
$this->current->setAttribute('id', $id);
308310
}
309311
}
310312

313+
private function calculateId(int $id): string
314+
{
315+
return substr('c' . md5((string)$id), 0, 8);
316+
}
317+
311318
/**
312319
* @param array<DOMNode> $children
313320
* @param false $renderedChildren

0 commit comments

Comments
 (0)