Skip to content

[LiveComponent] Only consider Live components in InterceptChildComponentRenderSubscriber #1097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Symfony\UX\LiveComponent\Twig\TemplateMap;
use Symfony\UX\LiveComponent\Util\ChildComponentPartialRenderer;
use Symfony\UX\LiveComponent\Util\FingerprintCalculator;
use Symfony\UX\LiveComponent\Util\LiveComponentStack;
use Symfony\UX\LiveComponent\Util\LiveControllerAttributesCreator;
use Symfony\UX\LiveComponent\Util\TwigAttributeHelperFactory;
use Symfony\UX\TwigComponent\ComponentFactory;
Expand Down Expand Up @@ -113,7 +114,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
$container->register('ux.live_component.event_listener.data_model_props_subscriber', DataModelPropsSubscriber::class)
->addTag('kernel.event_subscriber')
->setArguments([
new Reference('ux.twig_component.component_stack'),
new Reference('ux.twig_component.live_component_stack'),
new Reference('property_accessor'),
])
;
Expand All @@ -130,10 +131,16 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
$container->register('ux.live_component.live_responder', LiveResponder::class);
$container->setAlias(LiveResponder::class, 'ux.live_component.live_responder');

$container->register('ux.live_component.intercept_child_component_render_subscriber', InterceptChildComponentRenderSubscriber::class)
$container->register('ux.twig_component.live_component_stack', LiveComponentStack::class)
->setArguments([
new Reference('ux.twig_component.component_stack'),
])
;

$container->register('ux.live_component.intercept_child_component_render_subscriber', InterceptChildComponentRenderSubscriber::class)
->setArguments([
new Reference('ux.twig_component.live_component_stack'),
])
->addTag('container.service_subscriber', ['key' => DeterministicTwigIdCalculator::class, 'id' => 'ux.live_component.deterministic_id_calculator'])
->addTag('container.service_subscriber', ['key' => ChildComponentPartialRenderer::class, 'id' => 'ux.live_component.child_component_partial_renderer'])
->addTag('kernel.event_subscriber');
Expand Down
24 changes: 3 additions & 21 deletions src/LiveComponent/src/EventListener/DataModelPropsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Util\LiveComponentStack;
use Symfony\UX\LiveComponent\Util\ModelBindingParser;
use Symfony\UX\TwigComponent\ComponentStack;
use Symfony\UX\TwigComponent\Event\PreMountEvent;
use Symfony\UX\TwigComponent\MountedComponent;

/**
* Parses the "data-model" key, which triggers extra props to be passed in.
Expand All @@ -33,7 +31,7 @@ final class DataModelPropsSubscriber implements EventSubscriberInterface
{
private ModelBindingParser $modelBindingParser;

public function __construct(private ComponentStack $componentStack, private PropertyAccessorInterface $propertyAccessor)
public function __construct(private LiveComponentStack $componentStack, private PropertyAccessorInterface $propertyAccessor)
{
$this->modelBindingParser = new ModelBindingParser();
}
Expand All @@ -58,7 +56,7 @@ public function onPreMount(PreMountEvent $event): void

// find the first parent of the component about to be rendered that is a Live Component
// only those can have properties controlled via the data-model attribute
$parentMountedComponent = $this->getCurrentLiveComponent($this->componentStack);
$parentMountedComponent = $this->componentStack->getCurrentLiveComponent();
if (null === $parentMountedComponent) {
throw new \LogicException('You can only pass "data-model" when rendering a component when you\'re rendering inside of a parent component.');
}
Expand All @@ -79,20 +77,4 @@ public static function getSubscribedEvents(): array
PreMountEvent::class => 'onPreMount',
];
}

private function getCurrentLiveComponent(ComponentStack $componentStack): ?MountedComponent
{
foreach ($componentStack as $mountedComponent) {
if ($this->isLiveComponent($mountedComponent->getComponent()::class)) {
return $mountedComponent;
}
}

return null;
}

private function isLiveComponent(string $classname): bool
{
return [] !== (new \ReflectionClass($classname))->getAttributes(AsLiveComponent::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Symfony\UX\LiveComponent\Twig\DeterministicTwigIdCalculator;
use Symfony\UX\LiveComponent\Util\ChildComponentPartialRenderer;
use Symfony\UX\LiveComponent\Util\LiveComponentStack;
use Symfony\UX\LiveComponent\Util\LiveControllerAttributesCreator;
use Symfony\UX\TwigComponent\ComponentStack;
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;

/**
Expand All @@ -34,15 +34,15 @@ class InterceptChildComponentRenderSubscriber implements EventSubscriberInterfac
public const CHILDREN_FINGERPRINTS_METADATA_KEY = 'children_fingerprints';

public function __construct(
private ComponentStack $componentStack,
private LiveComponentStack $componentStack,
private ContainerInterface $container,
) {
}

public function preComponentCreated(PreCreateForRenderEvent $event): void
{
// if there is already a component, that's a parent. Else, this is not a child.
if (null === $parentComponent = $this->componentStack->getCurrentComponent()) {
if (null === $parentComponent = $this->componentStack->getCurrentLiveComponent()) {
return;
}

Expand Down
54 changes: 54 additions & 0 deletions src/LiveComponent/src/Util/LiveComponentStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Symfony\UX\LiveComponent\Util;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\TwigComponent\ComponentStack;
use Symfony\UX\TwigComponent\MountedComponent;

/**
* This class decorates the TwigComponent\ComponentStack adding specific Live component functionalities.
*
* @author Bart Vanderstukken <[email protected]>
*
* @internal
*/
final class LiveComponentStack extends ComponentStack
{
public function __construct(private readonly ComponentStack $componentStack)
{
}

public function getCurrentLiveComponent(): ?MountedComponent
{
foreach ($this->componentStack as $mountedComponent) {
if ($this->isLiveComponent($mountedComponent->getComponent()::class)) {
return $mountedComponent;
}
}

return null;
}

private function isLiveComponent(string $classname): bool
{
return [] !== (new \ReflectionClass($classname))->getAttributes(AsLiveComponent::class);
}

public function getCurrentComponent(): ?MountedComponent
{
return $this->componentStack->getCurrentComponent();
}

public function getParentComponent(): ?MountedComponent
{
return $this->componentStack->getParentComponent();
}

public function hasParentComponent(): bool
{
return $this->componentStack->hasParentComponent();
}
}