Skip to content

Commit 75d876d

Browse files
committed
QueryStringPropsExtractor, renamed to UrlPropsExtractor shall get props from path also
1 parent 327515b commit 75d876d

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -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\UrlPropsExtractor;
5555
use Symfony\UX\LiveComponent\Util\TwigAttributeHelperFactory;
5656
use Symfony\UX\TwigComponent\ComponentFactory;
5757
use Symfony\UX\TwigComponent\ComponentRenderer;
@@ -231,7 +231,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
231231
->addTag('container.service_subscriber', ['key' => LiveControllerAttributesCreator::class, 'id' => 'ux.live_component.live_controller_attributes_creator'])
232232
;
233233

234-
$container->register('ux.live_component.query_string_props_extractor', QueryStringPropsExtractor::class)
234+
$container->register('ux.live_component.query_string_props_extractor', UrlPropsExtractor::class)
235235
->setArguments([
236236
new Reference('ux.live_component.component_hydrator'),
237237
]);

src/LiveComponent/src/EventListener/QueryStringInitializeSubscriber.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
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\UrlPropsExtractor;
2020
use Symfony\UX\TwigComponent\Event\PostMountEvent;
2121

2222
/**
@@ -27,10 +27,10 @@
2727
class QueryStringInitializeSubscriber implements EventSubscriberInterface
2828
{
2929
public function __construct(
30-
private readonly RequestStack $requestStack,
30+
private readonly RequestStack $requestStack,
3131
private readonly LiveComponentMetadataFactory $metadataFactory,
32-
private readonly QueryStringPropsExtractor $queryStringPropsExtractor,
33-
private readonly PropertyAccessorInterface $propertyAccessor,
32+
private readonly UrlPropsExtractor $queryStringPropsExtractor,
33+
private readonly PropertyAccessorInterface $propertyAccessor,
3434
) {
3535
}
3636

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\LiveComponent\Util;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Routing\RouterInterface;
1516
use Symfony\UX\LiveComponent\Exception\HydrationException;
1617
use Symfony\UX\LiveComponent\LiveComponentHydrator;
1718
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadata;
@@ -22,28 +23,29 @@
2223
*
2324
* @internal
2425
*/
25-
final class QueryStringPropsExtractor
26+
final class UrlPropsExtractor
2627
{
2728
public function __construct(private readonly LiveComponentHydrator $hydrator)
2829
{
2930
}
3031

3132
/**
32-
* Extracts relevant query parameters from the current URL and hydrates them.
33+
* Extracts relevant props parameters from the current URL and hydrates them.
3334
*/
3435
public function extract(Request $request, LiveComponentMetadata $metadata, object $component): array
3536
{
36-
$query = $request->query->all();
37+
$parameters = array_merge($request->attributes->all(), $request->query->all());
3738

38-
if (empty($query)) {
39+
//@todo never empty because custom values prefixed with _ ... do something ?
40+
if (empty($parameters)) {
3941
return [];
4042
}
4143
$data = [];
4244

4345
foreach ($metadata->getAllLivePropsMetadata($component) as $livePropMetadata) {
4446
if ($queryMapping = $livePropMetadata->urlMapping()) {
4547
$frontendName = $livePropMetadata->calculateFieldName($component, $livePropMetadata->getName());
46-
if (null !== ($value = $query[$queryMapping->as ?? $frontendName] ?? null)) {
48+
if (null !== ($value = $parameters[$queryMapping->as ?? $frontendName] ?? null)) {
4749
if ('' === $value && null !== $livePropMetadata->getType() && (!$livePropMetadata->isBuiltIn() || 'array' === $livePropMetadata->getType())) {
4850
// Cast empty string to empty array for objects and arrays
4951
$value = [];

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
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\UrlPropsExtractor;
2020

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

@@ -27,7 +27,7 @@ class QueryStringPropsExtractorTest extends KernelTestCase
2727
*/
2828
public function testExtract(string $queryString, array $expected)
2929
{
30-
$extractor = new QueryStringPropsExtractor($this->hydrator());
30+
$extractor = new UrlPropsExtractor($this->hydrator());
3131

3232
$request = Request::create('/'.!empty($queryString) ? '?'.$queryString : '');
3333

0 commit comments

Comments
 (0)