Skip to content
Merged
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
31 changes: 19 additions & 12 deletions src/Template/LazyTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,26 @@

class LazyTreeBuilder extends DOMTreeBuilder
{
public static \WeakMap|null $idMap = null;
private static int $id = 0;

/**
* @var array<CompiledComponent>
*/
private array $componentStack = [];

private readonly StateProviderInterface $stateProvider;
private readonly AuthenticationServiceInterface|null $authenticationService;
/**
* @var array<DOMNode>
*/
private array $childParents = [];

/**
* @var array<array<CompiledComponent>>
*/
private array $delayStack = [];

/**
* @var array<CompiledComponent>
*/
private array|null $delayStackPointer = null;

/**
* @var array<DataProvider>
*/
Expand Down Expand Up @@ -82,6 +78,7 @@ public function __construct(
} catch (NotFoundExceptionInterface|ContainerExceptionInterface) {
$this->authenticationService = null;
}
self::$idMap ??= new \WeakMap();
}

/**
Expand Down Expand Up @@ -137,8 +134,12 @@ private function pushComponent(string $name, array $attributes): void
$attributes
);

if ($attributes['id'] ?? null) {
self::$idMap[$this->componentStack[$name]] = $attributes['id'];
}

// only count children we are actually rendering
if(count($this->componentStack) === 1) {
if (count($this->componentStack) === 1) {
// in a weird quirk of php, this results in appending a new array to the delay stack and they are the same variable.
$this->delayStackPointer = &$this->delayStack[];
$this->delayStackPointer = [];
Expand Down Expand Up @@ -207,17 +208,15 @@ private function decorateForm(string $name, array $attributes): void
$idElement = $this->doc->createElement('input');
$idElement->setAttribute('type', 'hidden');
$idElement->setAttribute('name', 'target_id');
$idElement->setAttribute('value', $this->calculateId(self::$id));
$idElement->setAttribute(
'value',
self::$idMap[$this->parentComponent ?? reset($this->componentStack)] ?? 'null'
);
$this->current->appendChild($idElement);
}
}
}

private function calculateId(int $id): string
{
return substr('c' . md5((string)$id), 0, 8);
}

/**
* @param string $name
* @return void
Expand Down Expand Up @@ -304,10 +303,18 @@ private function decorateComponent(CompiledComponent|false $component): void
}
$id = $this->calculateId(++self::$id);
if ($this->current instanceof DOMElement && !$this->current->hasAttribute('id')) {
if ($component = end($this->componentStack)) {
self::$idMap[$component] = $id;
}
$this->current->setAttribute('id', $id);
}
}

private function calculateId(int $id): string
{
return substr('c' . md5((string)$id), 0, 8);
}

/**
* @param array<DOMNode> $children
* @param false $renderedChildren
Expand Down