Skip to content

Commit 58143b3

Browse files
committed
[TwigComponent][LiveComponent] Seek only for live components in stack
1 parent 24338fb commit 58143b3

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/LiveComponent/src/EventListener/DataModelPropsSubscriber.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1515
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
16+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1617
use Symfony\UX\LiveComponent\Util\ModelBindingParser;
1718
use Symfony\UX\TwigComponent\ComponentStack;
1819
use Symfony\UX\TwigComponent\Event\PreMountEvent;
20+
use Symfony\UX\TwigComponent\MountedComponent;
1921

2022
/**
2123
* Parses the "data-model" key, which triggers extra props to be passed in.
@@ -55,7 +57,7 @@ public function onPreMount(PreMountEvent $event): void
5557
$data['data-model'] = $dataModel;
5658

5759
// the parent is still listed as the "current" component at this point
58-
$parentMountedComponent = $this->componentStack->getCurrentComponent();
60+
$parentMountedComponent = $this->getCurrentLiveComponent($this->componentStack);
5961
if (null === $parentMountedComponent) {
6062
throw new \LogicException('You can only pass "data-model" when rendering a component when you\'re rendering inside of a parent component.');
6163
}
@@ -76,4 +78,20 @@ public static function getSubscribedEvents(): array
7678
PreMountEvent::class => 'onPreMount',
7779
];
7880
}
81+
82+
private function getCurrentLiveComponent(ComponentStack $componentStack): ?MountedComponent
83+
{
84+
foreach ($componentStack as $mountedComponent) {
85+
if ($this->isLiveComponent($mountedComponent->getComponent()::class)) {
86+
return $mountedComponent;
87+
}
88+
}
89+
90+
return null;
91+
}
92+
93+
private function isLiveComponent(string $classname): bool
94+
{
95+
return [] !== (new \ReflectionClass($classname))->getAttributes(AsLiveComponent::class);
96+
}
7997
}

src/TwigComponent/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
}

0 commit comments

Comments
 (0)