Skip to content

Commit 3ef4ace

Browse files
committed
Move query string initializer to PostMount
1 parent fc3e14c commit 3ef4ace

File tree

6 files changed

+21
-7
lines changed

6 files changed

+21
-7
lines changed

src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
229229
new Reference('request_stack'),
230230
new Reference('ux.live_component.metadata_factory'),
231231
new Reference('ux.live_component.query_string_props_extractor'),
232+
new Reference('property_accessor'),
232233
])
233234
->addTag('kernel.event_subscriber');
234235

src/LiveComponent/src/EventListener/QueryStringInitializeSubscriber.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1515
use Symfony\Component\HttpFoundation\RequestStack;
16+
use Symfony\Component\PropertyAccess\Exception\ExceptionInterface as PropertyAccessExceptionInterface;
17+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1618
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
1719
use Symfony\UX\LiveComponent\Util\QueryStringPropsExtractor;
18-
use Symfony\UX\TwigComponent\Event\PreMountEvent;
20+
use Symfony\UX\TwigComponent\Event\PostMountEvent;
1921

2022
/**
2123
* @author Nicolas Rigaud <squrious@protonmail.com>
@@ -30,17 +32,18 @@ public function __construct(
3032
private readonly RequestStack $requestStack,
3133
private readonly LiveComponentMetadataFactory $metadataFactory,
3234
private readonly QueryStringPropsExtractor $queryStringPropsExtractor,
35+
private readonly PropertyAccessorInterface $propertyAccessor,
3336
) {
3437
}
3538

3639
public static function getSubscribedEvents(): array
3740
{
3841
return [
39-
PreMountEvent::class => 'onPreMount',
42+
PostMountEvent::class => ['onPostMount', 256],
4043
];
4144
}
4245

43-
public function onPreMount(PreMountEvent $event): void
46+
public function onPostMount(PostMountEvent $event): void
4447
{
4548
if (!$event->getMetadata()->get('live', false)) {
4649
// Not a live component
@@ -61,6 +64,14 @@ public function onPreMount(PreMountEvent $event): void
6164

6265
$queryStringData = $this->queryStringPropsExtractor->extract($request, $metadata, $event->getComponent());
6366

64-
$event->setData(array_merge($event->getData(), $queryStringData));
67+
$component = $event->getComponent();
68+
69+
foreach ($queryStringData as $name => $value) {
70+
try {
71+
$this->propertyAccessor->setValue($component, $name, $value);
72+
} catch (PropertyAccessExceptionInterface $exception) {
73+
// Ignore errors
74+
}
75+
}
6576
}
6677
}

src/LiveComponent/tests/Fixtures/Component/ComponentWithUrlBoundProps.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ class ComponentWithUrlBoundProps
4747

4848
public function modifyProp7(LiveProp $prop): LiveProp
4949
{
50-
return $prop->withUrl(true);
50+
return $prop->withUrl($this->prop7InUrl);
5151
}
5252
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{{ component('component_with_url_bound_props') }}
1+
{{ component('component_with_url_bound_props', {
2+
prop7InUrl: true
3+
}) }}

src/LiveComponent/tests/Functional/EventListener/QueryStringInitializerSubscriberTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class QueryStringInitializerSubscriberTest extends KernelTestCase
2121
public function testQueryStringPropsInitialization()
2222
{
2323
$this->browser()
24+
->throwExceptions()
2425
->get('/render-template/render_component_with_url_bound_props?prop1=foo&prop2=42&prop3[]=foo&prop3[]=bar&prop4=unbound&prop5[address]=foo&prop5[city]=bar&field6=foo&prop7=foo')
2526
->assertSuccessful()
2627
->assertContains('Prop1: foo')

src/TwigComponent/tests/Integration/ComponentExtensionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1515
use Symfony\UX\TwigComponent\Tests\Fixtures\User;
1616
use Twig\Environment;
17-
use Twig\Error\RuntimeError;
1817

1918
/**
2019
* @author Kevin Bond <kevinbond@gmail.com>

0 commit comments

Comments
 (0)