Skip to content

Commit 61dad88

Browse files
sneakyvvweaverryan
authored andcommitted
[TwigComponent][LiveComponent] Fix DataModelPropsSubscriber for embedded components
1 parent 77a8655 commit 61dad88

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

src/ComponentRenderer.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,18 @@ public function embeddedContext(string $name, array $props, array $context, stri
9393

9494
$this->componentStack->push($mounted);
9595

96-
try {
97-
$embeddedContext = $this->preRender($mounted, $context)->getVariables();
98-
99-
if (!isset($embeddedContext['outerBlocks'])) {
100-
$embeddedContext['outerBlocks'] = new BlockStack();
101-
}
96+
$embeddedContext = $this->preRender($mounted, $context)->getVariables();
10297

103-
return $embeddedContext;
104-
} finally {
105-
$this->componentStack->pop();
98+
if (!isset($embeddedContext['outerBlocks'])) {
99+
$embeddedContext['outerBlocks'] = new BlockStack();
106100
}
101+
102+
return $embeddedContext;
103+
}
104+
105+
public function finishEmbeddedComponentRender(): void
106+
{
107+
$this->componentStack->pop();
107108
}
108109

109110
private function preRender(MountedComponent $mounted, array $context = []): PreRenderEvent

src/ComponentStack.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @internal
1818
*/
19-
class ComponentStack
19+
class ComponentStack implements \IteratorAggregate
2020
{
2121
/**
2222
* @var MountedComponent[]
@@ -60,4 +60,12 @@ public function hasParentComponent(): bool
6060
{
6161
return (bool) $this->getParentComponent();
6262
}
63+
64+
/**
65+
* @return MountedComponent[]|\ArrayIterator
66+
*/
67+
public function getIterator(): \Traversable
68+
{
69+
return new \ArrayIterator(array_reverse($this->components));
70+
}
6371
}

src/Twig/ComponentExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ public function embeddedContext(string $name, array $props, array $context, stri
8080
}
8181
}
8282

83+
public function finishEmbeddedComponentRender(): void
84+
{
85+
try {
86+
$this->container->get(ComponentRenderer::class)->finishEmbeddedComponentRender();
87+
} catch (\Throwable $e) {
88+
$this->throwRuntimeError($name, $e);
89+
}
90+
}
91+
8392
private function throwRuntimeError(string $name, \Throwable $e): void
8493
{
8594
if (!($e instanceof \Exception)) {

src/Twig/ComponentNode.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public function compile(Compiler $compiler): void
8585
$compiler->raw('->display($embeddedContext, $embeddedBlocks);');
8686
$compiler->raw("\n");
8787

88+
$compiler->write('$this->extensions[')
89+
->string(ComponentExtension::class)
90+
->raw(']->finishEmbeddedComponentRender()')
91+
->raw(";\n")
92+
;
93+
8894
$compiler
8995
->outdent()
9096
->write('}')

0 commit comments

Comments
 (0)