Skip to content

Commit 213832a

Browse files
committed
Refactor extraMetadata handling in MountedComponent and related classes
1 parent 0aaa5bf commit 213832a

File tree

4 files changed

+69
-40
lines changed

4 files changed

+69
-40
lines changed

src/LiveComponent/src/EventListener/DeferLiveComponentSubscriber.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
namespace Symfony\UX\LiveComponent\EventListener;
66

77
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8-
use Symfony\UX\LiveComponent\Util\LiveControllerAttributesCreator;
9-
use Symfony\UX\TwigComponent\ComponentStack;
8+
use Symfony\UX\TwigComponent\Event\PostMountEvent;
109
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
1110

1211
final class DeferLiveComponentSubscriber implements EventSubscriberInterface
@@ -17,50 +16,58 @@ final class DeferLiveComponentSubscriber implements EventSubscriberInterface
1716

1817
private const DEFAULT_LOADING_TEMPLATE = null;
1918

20-
public function __construct(
21-
private ComponentStack $componentStack,
22-
private LiveControllerAttributesCreator $attributesCreator,
23-
) {
19+
public function onPostMount(PostMountEvent $event): void
20+
{
21+
$data = $event->getData();
22+
if (\array_key_exists('defer', $data)) {
23+
$event->addExtraMetadata('defer', true);
24+
unset($event->getData()['defer']);
25+
}
26+
27+
if (\array_key_exists('loading-template', $data)) {
28+
$event->addExtraMetadata('loading-template', $data['loading-template']);
29+
unset($event->getData()['loading-template']);
30+
}
31+
32+
if (\array_key_exists('loading-tag', $data)) {
33+
$event->addExtraMetadata('loading-tag', $data['loading-tag']);
34+
unset($event->getData()['loading-tag']);
35+
}
36+
37+
$event->setData($data);
2438
}
2539

2640
public function onPreRender(PreRenderEvent $event): void
2741
{
2842
$mountedComponent = $event->getMountedComponent();
29-
$inputProps = $mountedComponent->getInputProps();
30-
$isDeferred = $inputProps['defer'] ?? false;
43+
$isDeferred = $mountedComponent->hasExtraMetadata('defer');
3144

3245
if (!$isDeferred) {
3346
return;
3447
}
3548

36-
$metadata = $event->getMetadata();
37-
$variables = $event->getVariables();
38-
$attributesKey = $metadata->getAttributesVar();
39-
4049
$event->setTemplate('@LiveComponent/deferred.html.twig');
4150

42-
$originalAttributes = $variables[$attributesKey]->all();
43-
44-
$attributes = $originalAttributes;
45-
$attributes = array_filter(
46-
$attributes,
47-
fn ($key) => !\in_array($key, ['defer', 'loading-template', 'loading-tag'], true),
48-
\ARRAY_FILTER_USE_KEY,
49-
);
51+
$variables = $event->getVariables();
52+
$variables['loadingTemplate'] = self::DEFAULT_LOADING_TEMPLATE;
53+
$variables['loadingTag'] = self::DEFAULT_LOADING_TAG;
5054

51-
$variables[$attributesKey] = $attributes;
52-
$variables['loadingTag'] = $originalAttributes['loading-tag'] ?? self::DEFAULT_LOADING_TAG;
53-
$variables['loadingTemplate'] = $originalAttributes['loading-template'] ?? self::DEFAULT_LOADING_TEMPLATE;
55+
if ($mountedComponent->hasExtraMetadata('loading-template')) {
56+
$variables['loadingTemplate'] = $mountedComponent->getExtraMetadata('loading-template');
57+
}
5458

55-
$mountedComponent->setAttributes(
56-
$mountedComponent->getAttributes()->without('defer', 'loading-template', 'loading-tag'),
57-
);
59+
if ($mountedComponent->hasExtraMetadata('loading-tag')) {
60+
$variables['loadingTag'] = $mountedComponent->getExtraMetadata('loading-tag');
61+
}
5862

5963
$event->setVariables($variables);
6064
}
6165

6266
public static function getSubscribedEvents(): array
6367
{
64-
return [PreRenderEvent::class => ['onPreRender', self::PRIORITY]];
68+
return [
69+
PostMountEvent::class => ['onPostMount', self::PRIORITY],
70+
PreRenderEvent::class => ['onPreRender', self::PRIORITY],
71+
];
6572
}
6673
}

src/TwigComponent/src/ComponentFactory.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
8888
}
8989
}
9090

91-
$data = $this->postMount($component, $data);
91+
$postMount = $this->postMount($component, $data);
92+
$data = $postMount['data'];
93+
$extraMetadata = $postMount['extraMetadata'];
9294

9395
// create attributes from "attributes" key if exists
9496
$attributesVar = $componentMetadata->getAttributesVar();
@@ -109,7 +111,8 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
109111
$componentMetadata->getName(),
110112
$component,
111113
new ComponentAttributes(array_merge($attributes, $data)),
112-
$originalData
114+
$originalData,
115+
$extraMetadata,
113116
);
114117
}
115118

@@ -188,11 +191,15 @@ private function preMount(object $component, array $data): array
188191
return $data;
189192
}
190193

194+
/**
195+
* @return array{data: array<string, mixed>, extraMetadata: array<string, mixed>}
196+
*/
191197
private function postMount(object $component, array $data): array
192198
{
193199
$event = new PostMountEvent($component, $data);
194200
$this->eventDispatcher->dispatch($event);
195201
$data = $event->getData();
202+
$extraMetadata = $event->getExtraMetadata();
196203

197204
foreach (AsTwigComponent::postMountMethods($component) as $method) {
198205
$newData = $component->{$method->name}($data);
@@ -202,7 +209,10 @@ private function postMount(object $component, array $data): array
202209
}
203210
}
204211

205-
return $data;
212+
return [
213+
'data' => $data,
214+
'extraMetadata' => $extraMetadata,
215+
];
206216
}
207217

208218
private function isAnonymousComponent(string $name): bool

src/TwigComponent/src/Event/PostMountEvent.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
*/
1919
final class PostMountEvent extends Event
2020
{
21-
public function __construct(private object $component, private array $data)
22-
{
21+
public function __construct(
22+
private object $component,
23+
private array $data,
24+
private array $extraMetadata = [],
25+
) {
2326
}
2427

2528
public function getComponent(): object
@@ -36,4 +39,19 @@ public function setData(array $data): void
3639
{
3740
$this->data = $data;
3841
}
42+
43+
public function getExtraMetadata(): array
44+
{
45+
return $this->extraMetadata;
46+
}
47+
48+
public function addExtraMetadata(string $key, mixed $value): void
49+
{
50+
$this->extraMetadata[$key] = $value;
51+
}
52+
53+
public function removeExtraMetadata(string $key): void
54+
{
55+
unset($this->extraMetadata[$key]);
56+
}
3957
}

src/TwigComponent/src/MountedComponent.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818
*/
1919
final class MountedComponent
2020
{
21-
/**
22-
* Any extra metadata that might be useful to set.
23-
*
24-
* @var array<string, mixed>
25-
*/
26-
private array $extraMetadata = [];
27-
2821
/**
2922
* @param array|null $inputProps if the component was just originally created,
3023
* (not hydrated from a request), this is the
@@ -34,7 +27,8 @@ public function __construct(
3427
private string $name,
3528
private object $component,
3629
private ComponentAttributes $attributes,
37-
private ?array $inputProps = []
30+
private ?array $inputProps = [],
31+
private array $extraMetadata = [],
3832
) {
3933
}
4034

0 commit comments

Comments
 (0)