Skip to content

Commit 24a582b

Browse files
committed
Apply modifiers in LiveComponentMetadata
1 parent c4003f0 commit 24a582b

File tree

5 files changed

+18
-27
lines changed

5 files changed

+18
-27
lines changed

src/LiveComponent/src/EventListener/QueryStringInitializeSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function onPreMount(PreMountEvent $event): void
5555

5656
$metadata = $this->metadataFactory->getMetadata($event->getMetadata()->getName());
5757

58-
if (!$metadata->hasQueryStringBindings()) {
58+
if (!$metadata->hasQueryStringBindings($event->getComponent())) {
5959
return;
6060
}
6161

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ public function dehydrate(object $component, ComponentAttributes $attributes, Li
6565
$takenFrontendPropertyNames = [];
6666

6767
$dehydratedProps = new DehydratedProps();
68-
foreach ($componentMetadata->getAllLivePropsMetadata() as $propMetadata) {
69-
$propMetadata = $propMetadata->withModifier($component);
68+
foreach ($componentMetadata->getAllLivePropsMetadata($component) as $propMetadata) {
7069
$propertyName = $propMetadata->getName();
7170
$frontendName = $propMetadata->calculateFieldName($component, $propertyName);
7271

@@ -146,21 +145,9 @@ public function hydrate(object $component, array $props, array $updatedProps, Li
146145
$attributes = new ComponentAttributes($dehydratedOriginalProps->getPropValue(self::ATTRIBUTES_KEY, []));
147146
$dehydratedOriginalProps->removePropValue(self::ATTRIBUTES_KEY);
148147

149-
$allMetadata = $componentMetadata->getAllLivePropsMetadata();
150-
uasort($allMetadata, static function (LivePropMetadata $a, LivePropMetadata $b) {
151-
if ($a->hasModifier() && !$b->hasModifier()) {
152-
return 1;
153-
} elseif (!$a->hasModifier() && $b->hasModifier()) {
154-
return -1;
155-
} else {
156-
return 0;
157-
}
158-
});
159-
160-
foreach ($allMetadata as $propMetadata) {
161-
$propMetadata = $propMetadata->withModifier($component);
162-
// foreach ($componentMetadata->getAllLivePropsMetadata($component) as $propMetadata) {
148+
foreach ($componentMetadata->getAllLivePropsMetadata($component) as $propMetadata) {
163149
$frontendName = $propMetadata->calculateFieldName($component, $propMetadata->getName());
150+
164151
if (!$dehydratedOriginalProps->hasPropValue($frontendName)) {
165152
// this property was not sent, so skip
166153
// even if this has writable paths, if no identity is sent,

src/LiveComponent/src/Metadata/LiveComponentMetadata.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public function __construct(
2727
/** @var LivePropMetadata[] */
2828
private array $livePropsMetadata,
2929
) {
30+
uasort(
31+
$this->livePropsMetadata,
32+
static fn (LivePropMetadata $a, LivePropMetadata $b) => $a->hasModifier() <=> $b->hasModifier()
33+
);
3034
}
3135

3236
public function getComponentMetadata(): ComponentMetadata
@@ -37,9 +41,11 @@ public function getComponentMetadata(): ComponentMetadata
3741
/**
3842
* @return LivePropMetadata[]
3943
*/
40-
public function getAllLivePropsMetadata(): array
44+
public function getAllLivePropsMetadata(object $component): iterable
4145
{
42-
return $this->livePropsMetadata;
46+
foreach ($this->livePropsMetadata as $livePropMetadata) {
47+
yield $livePropMetadata->withModifier($component);
48+
}
4349
}
4450

4551
/**
@@ -62,9 +68,9 @@ public function getOnlyPropsThatAcceptUpdatesFromParent(array $inputProps): arra
6268
return array_intersect_key($inputProps, array_flip($propNames));
6369
}
6470

65-
public function hasQueryStringBindings(): bool
71+
public function hasQueryStringBindings($component): bool
6672
{
67-
foreach ($this->getAllLivePropsMetadata() as $livePropMetadata) {
73+
foreach ($this->getAllLivePropsMetadata($component) as $livePropMetadata) {
6874
if ($livePropMetadata->queryStringMapping()) {
6975
return true;
7076
}

src/LiveComponent/src/Util/LiveControllerAttributesCreator.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,11 @@ public function attributesForRendering(MountedComponent $mounted, ComponentMetad
105105
$attributesCollection->setRequestMethod($requestMethod);
106106
}
107107

108-
if ($liveMetadata->hasQueryStringBindings()) {
108+
if ($liveMetadata->hasQueryStringBindings($mounted->getComponent())) {
109109
$queryMapping = [];
110-
foreach ($liveMetadata->getAllLivePropsMetadata() as $livePropMetadata) {
111-
$livePropMetadata = $livePropMetadata->withModifier($mounted->getComponent());
110+
foreach ($liveMetadata->getAllLivePropsMetadata($mounted->getComponent()) as $livePropMetadata) {
112111
if ($livePropMetadata->queryStringMapping()) {
113-
$frontendName = $livePropMetadata->calculateFieldName($mounted, $livePropMetadata->getName());
112+
$frontendName = $livePropMetadata->calculateFieldName($mounted->getComponent(), $livePropMetadata->getName());
114113
$queryMapping[$frontendName] = ['name' => $frontendName];
115114
}
116115
}

src/LiveComponent/src/Util/QueryStringPropsExtractor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ public function extract(Request $request, LiveComponentMetadata $metadata, objec
4242
}
4343
$data = [];
4444

45-
foreach ($metadata->getAllLivePropsMetadata() as $livePropMetadata) {
46-
$livePropMetadata = $livePropMetadata->withModifier($component);
45+
foreach ($metadata->getAllLivePropsMetadata($component) as $livePropMetadata) {
4746
if ($livePropMetadata->queryStringMapping()) {
4847
$frontendName = $livePropMetadata->calculateFieldName($component, $livePropMetadata->getName());
4948
if (null !== ($value = $query[$frontendName] ?? null)) {

0 commit comments

Comments
 (0)