Skip to content

Commit 17b429e

Browse files
committed
QueryStringPropsExtractor, renamed to RequestPropsExtractor shall get props also from path. RequestInitializeSubscriber also renamed
1 parent 63f4d32 commit 17b429e

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
use Symfony\UX\LiveComponent\EventListener\InterceptChildComponentRenderSubscriber;
3535
use Symfony\UX\LiveComponent\EventListener\LiveComponentSubscriber;
3636
use Symfony\UX\LiveComponent\EventListener\LiveUrlSubscriber;
37-
use Symfony\UX\LiveComponent\EventListener\QueryStringInitializeSubscriber;
37+
use Symfony\UX\LiveComponent\EventListener\RequestInitializeSubscriber;
3838
use Symfony\UX\LiveComponent\EventListener\ResetDeterministicIdSubscriber;
3939
use Symfony\UX\LiveComponent\Form\Type\LiveCollectionType;
4040
use Symfony\UX\LiveComponent\Hydration\HydrationExtensionInterface;
@@ -51,7 +51,7 @@
5151
use Symfony\UX\LiveComponent\Util\FingerprintCalculator;
5252
use Symfony\UX\LiveComponent\Util\LiveComponentStack;
5353
use Symfony\UX\LiveComponent\Util\LiveControllerAttributesCreator;
54-
use Symfony\UX\LiveComponent\Util\QueryStringPropsExtractor;
54+
use Symfony\UX\LiveComponent\Util\RequestPropsExtractor;
5555
use Symfony\UX\LiveComponent\Util\TwigAttributeHelperFactory;
5656
use Symfony\UX\TwigComponent\ComponentFactory;
5757
use Symfony\UX\TwigComponent\ComponentRenderer;
@@ -139,8 +139,8 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
139139

140140
$container->register('ux.live_component.live_url_subscriber', LiveUrlSubscriber::class)
141141
->setArguments([
142-
new Reference('router'),
143142
new Reference('ux.live_component.metadata_factory'),
143+
new Reference('ux.live_component.url_factory'),
144144
])
145145
->addTag('kernel.event_subscriber')
146146
;
@@ -210,6 +210,9 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
210210

211211
$container->register('ux.live_component.attribute_helper_factory', TwigAttributeHelperFactory::class);
212212

213+
$container->register('ux.live_component.url_factory', UrlFactory::class)
214+
->setArguments([new Reference('router')]);
215+
213216
$container->register('ux.live_component.live_controller_attributes_creator', LiveControllerAttributesCreator::class)
214217
->setArguments([
215218
new Reference('ux.live_component.metadata_factory'),
@@ -233,12 +236,12 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
233236
->addTag('container.service_subscriber', ['key' => LiveControllerAttributesCreator::class, 'id' => 'ux.live_component.live_controller_attributes_creator'])
234237
;
235238

236-
$container->register('ux.live_component.query_string_props_extractor', QueryStringPropsExtractor::class)
239+
$container->register('ux.live_component.query_string_props_extractor', RequestPropsExtractor::class)
237240
->setArguments([
238241
new Reference('ux.live_component.component_hydrator'),
239242
]);
240243

241-
$container->register('ux.live_component.query_string_initializer_subscriber', QueryStringInitializeSubscriber::class)
244+
$container->register('ux.live_component.query_string_initializer_subscriber', RequestInitializeSubscriber::class)
242245
->setArguments([
243246
new Reference('request_stack'),
244247
new Reference('ux.live_component.metadata_factory'),

src/LiveComponent/src/EventListener/QueryStringInitializeSubscriber.php renamed to src/LiveComponent/src/EventListener/RequestInitializeSubscriber.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616
use Symfony\Component\PropertyAccess\Exception\ExceptionInterface as PropertyAccessExceptionInterface;
1717
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1818
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
19-
use Symfony\UX\LiveComponent\Util\QueryStringPropsExtractor;
19+
use Symfony\UX\LiveComponent\Util\RequestPropsExtractor;
2020
use Symfony\UX\TwigComponent\Event\PostMountEvent;
2121

2222
/**
2323
* @author Nicolas Rigaud <[email protected]>
2424
*
2525
* @internal
2626
*/
27-
class QueryStringInitializeSubscriber implements EventSubscriberInterface
27+
class RequestInitializeSubscriber implements EventSubscriberInterface
2828
{
2929
public function __construct(
3030
private readonly RequestStack $requestStack,
3131
private readonly LiveComponentMetadataFactory $metadataFactory,
32-
private readonly QueryStringPropsExtractor $queryStringPropsExtractor,
32+
private readonly RequestPropsExtractor $requestPropsExtractor,
3333
private readonly PropertyAccessorInterface $propertyAccessor,
3434
) {
3535
}
@@ -60,11 +60,11 @@ public function onPostMount(PostMountEvent $event): void
6060
return;
6161
}
6262

63-
$queryStringData = $this->queryStringPropsExtractor->extract($request, $metadata, $event->getComponent());
63+
$requestData = $this->requestPropsExtractor->extract($request, $metadata, $event->getComponent());
6464

6565
$component = $event->getComponent();
6666

67-
foreach ($queryStringData as $name => $value) {
67+
foreach ($requestData as $name => $value) {
6868
try {
6969
$this->propertyAccessor->setValue($component, $name, $value);
7070
} catch (PropertyAccessExceptionInterface $exception) {

src/LiveComponent/src/Util/QueryStringPropsExtractor.php renamed to src/LiveComponent/src/Util/RequestPropsExtractor.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,28 @@
2626
*
2727
* @internal
2828
*/
29-
final class QueryStringPropsExtractor
29+
final class RequestPropsExtractor
3030
{
3131
public function __construct(private readonly LiveComponentHydrator $hydrator)
3232
{
3333
}
3434

3535
/**
36-
* Extracts relevant query parameters from the current URL and hydrates them.
36+
* Extracts relevant props parameters from the current URL and hydrates them.
3737
*/
3838
public function extract(Request $request, LiveComponentMetadata $metadata, object $component): array
3939
{
40-
$query = $request->query->all();
40+
$parameters = array_merge($request->attributes->all(), $request->query->all());
4141

42-
if (empty($query)) {
42+
if (empty($parameters)) {
4343
return [];
4444
}
4545
$data = [];
4646

4747
foreach ($metadata->getAllLivePropsMetadata($component) as $livePropMetadata) {
4848
if ($queryMapping = $livePropMetadata->urlMapping()) {
4949
$frontendName = $livePropMetadata->calculateFieldName($component, $livePropMetadata->getName());
50-
if (null !== ($value = $query[$queryMapping->as ?? $frontendName] ?? null)) {
50+
if (null !== ($value = $parameters[$queryMapping->as ?? $frontendName] ?? null)) {
5151
if ('' === $value) {
5252
// BC layer when "symfony/type-info" is not available
5353
if ($livePropMetadata instanceof LegacyLivePropMetadata) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1515
use Zenstruck\Browser\Test\HasBrowser;
1616

17-
class QueryStringInitializerSubscriberTest extends KernelTestCase
17+
class RequestInitializerSubscriberTest extends KernelTestCase
1818
{
1919
use HasBrowser;
2020

src/LiveComponent/tests/Functional/Util/QueryStringPropsExtractorTest.php renamed to src/LiveComponent/tests/Functional/Util/RequestPropsExtractorTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@
1616
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory;
1717
use Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Address;
1818
use Symfony\UX\LiveComponent\Tests\LiveComponentTestHelper;
19-
use Symfony\UX\LiveComponent\Util\QueryStringPropsExtractor;
19+
use Symfony\UX\LiveComponent\Util\RequestPropsExtractor;
2020

21-
class QueryStringPropsExtractorTest extends KernelTestCase
21+
class RequestPropsExtractorTest extends KernelTestCase
2222
{
2323
use LiveComponentTestHelper;
2424

2525
/**
2626
* @dataProvider getQueryStringTests
2727
*/
28-
public function testExtract(string $queryString, array $expected)
28+
public function testExtractFromQueryString(string $queryString, array $expected, array $attributes = []): void
2929
{
30-
$extractor = new QueryStringPropsExtractor($this->hydrator());
30+
$extractor = new RequestPropsExtractor($this->hydrator());
3131

3232
$request = Request::create('/'.!empty($queryString) ? '?'.$queryString : '');
33+
$request->attributes->add($attributes);
3334

3435
/** @var LiveComponentMetadataFactory $metadataFactory */
3536
$metadataFactory = self::getContainer()->get('ux.live_component.metadata_factory');
@@ -65,6 +66,10 @@ public function getQueryStringTests(): iterable
6566
'invalid array value' => ['arrayProp=foo', []],
6667
'invalid object value' => ['objectProp=foo', []],
6768
'aliased prop' => ['q=foo', ['boundPropWithAlias' => 'foo']],
69+
'attribute prop' => ['', ['stringProp' => 'foo'], ['stringProp' => 'foo']],
70+
'attribute aliased prop' => ['', ['boundPropWithAlias' => 'foo'], ['q' => 'foo']],
71+
'attribute not bound prop' => ['', [], ['unboundProp' => 'foo']],
72+
'query priority' => ['stringProp=foo', ['stringProp' => 'foo'], ['stringProp' => 'bar']],
6873
];
6974
}
7075
}

0 commit comments

Comments
 (0)