Skip to content

Commit a930f1e

Browse files
kbondweaverryan
authored andcommitted
[Twig][Live] Cleanup
1 parent 0177934 commit a930f1e

19 files changed

+94
-89
lines changed

src/Attribute/LiveArg.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function __construct(public ?string $name = null)
2424
}
2525

2626
/**
27+
* @internal
28+
*
2729
* @return array<string, string>
2830
*/
2931
public static function liveArgs(object $component, string $action): array

src/Attribute/LiveProp.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,41 @@ public function __construct(
5151
$this->fieldName = $fieldName;
5252
}
5353

54+
/**
55+
* @internal
56+
*/
5457
public function isReadonly(): bool
5558
{
5659
return !$this->writable;
5760
}
5861

62+
/**
63+
* @internal
64+
*/
5965
public function exposed(): array
6066
{
6167
return $this->exposed;
6268
}
6369

70+
/**
71+
* @internal
72+
*/
6473
public function hydrateMethod(): ?string
6574
{
6675
return $this->hydrateWith ? trim($this->hydrateWith, '()') : null;
6776
}
6877

78+
/**
79+
* @internal
80+
*/
6981
public function dehydrateMethod(): ?string
7082
{
7183
return $this->dehydrateWith ? trim($this->dehydrateWith, '()') : null;
7284
}
7385

86+
/**
87+
* @internal
88+
*/
7489
public function calculateFieldName(object $component, string $fallback): string
7590
{
7691
if (!$this->fieldName) {

src/Attribute/LivePropContext.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@
2020
*/
2121
final class LivePropContext
2222
{
23-
private LiveProp $liveProp;
24-
private \ReflectionProperty $reflectionProperty;
25-
26-
public function __construct(LiveProp $liveProp, \ReflectionProperty $reflectionProperty)
23+
public function __construct(private LiveProp $liveProp, private \ReflectionProperty $reflectionProperty)
2724
{
28-
$this->liveProp = $liveProp;
29-
$this->reflectionProperty = $reflectionProperty;
3025
}
3126

3227
public function liveProp(): LiveProp

src/ComponentValidator.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@
2525
*/
2626
class ComponentValidator implements ComponentValidatorInterface, ServiceSubscriberInterface
2727
{
28-
private ContainerInterface $container;
29-
30-
public function __construct(ContainerInterface $container)
28+
public function __construct(private ContainerInterface $container)
3129
{
32-
$this->container = $container;
3330
}
3431

3532
/**

src/ComponentWithFormTrait.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,29 @@ trait ComponentWithFormTrait
6767
*/
6868
abstract protected function instantiateForm(): FormInterface;
6969

70+
/**
71+
* @internal
72+
*/
7073
#[PostMount]
71-
public function postMount(array $data): array
74+
public function initializeForm(array $data): array
7275
{
7376
// allow the FormView object to be passed into the component() as "form"
7477
if (\array_key_exists('form', $data)) {
7578
$this->formView = $data['form'];
79+
7680
unset($data['form']);
7781

78-
if ($this->formView) {
79-
// if a FormView is passed in and it contains any errors, then
80-
// we mark that this entire component has been validated so that
81-
// all validation errors continue showing on re-render
82-
if (LiveFormUtility::doesFormContainAnyErrors($this->formView)) {
83-
$this->isValidated = true;
84-
$this->validatedFields = [];
85-
}
82+
// if a FormView is passed in and it contains any errors, then
83+
// we mark that this entire component has been validated so that
84+
// all validation errors continue showing on re-render
85+
if ($this->formView && LiveFormUtility::doesFormContainAnyErrors($this->formView)) {
86+
$this->isValidated = true;
87+
$this->validatedFields = [];
8688
}
8789
}
8890

8991
// set the formValues from the initial form view's data
90-
$this->initializeFormValues();
92+
$this->formValues = $this->extractFormValues($this->getForm());
9193

9294
return $data;
9395
}
@@ -98,6 +100,8 @@ public function postMount(array $data): array
98100
* This primarily applies to a re-render where $actionName is null.
99101
* But, in the event that there is an action and the form was
100102
* not submitted manually, it will be submitted here.
103+
*
104+
* @internal
101105
*/
102106
#[BeforeReRender]
103107
public function submitFormOnRender(): void
@@ -128,11 +132,6 @@ public function getFormName(): string
128132
return $this->formName;
129133
}
130134

131-
private function initializeFormValues(): void
132-
{
133-
$this->formValues = $this->extractFormValues($this->getForm());
134-
}
135-
136135
private function submitForm(bool $validateAll = true): void
137136
{
138137
if (null !== $this->formView) {
@@ -157,6 +156,7 @@ private function submitForm(bool $validateAll = true): void
157156
// re-extract the "view" values in case the submitted data
158157
// changed the underlying data or structure of the form
159158
$this->formValues = $this->extractFormValues($this->getForm());
159+
160160
// remove any validatedFields that do not exist in data anymore
161161
$this->validatedFields = LiveFormUtility::removePathsNotInData(
162162
$this->validatedFields ?? [],
@@ -168,12 +168,23 @@ private function submitForm(bool $validateAll = true): void
168168
}
169169
}
170170

171+
private function getFormInstance(): FormInterface
172+
{
173+
if (null === $this->formInstance) {
174+
$this->formInstance = $this->instantiateForm();
175+
}
176+
177+
return $this->formInstance;
178+
}
179+
171180
/**
172181
* Returns a hierarchical array of the entire form's values.
173182
*
174183
* This is used to pass the initial values into the live component's
175184
* frontend, and it's meant to equal the raw POST data that would
176185
* be sent if the form were submitted without modification.
186+
*
187+
* @internal
177188
*/
178189
private function extractFormValues(FormView $formView): array
179190
{
@@ -204,15 +215,9 @@ private function extractFormValues(FormView $formView): array
204215
return $values;
205216
}
206217

207-
private function getFormInstance(): FormInterface
208-
{
209-
if (null === $this->formInstance) {
210-
$this->formInstance = $this->instantiateForm();
211-
}
212-
213-
return $this->formInstance;
214-
}
215-
218+
/**
219+
* @internal
220+
*/
216221
private function clearErrorsForNonValidatedFields(FormInterface $form, string $currentPath = ''): void
217222
{
218223
if ($form instanceof ClearableErrorsInterface && (!$currentPath || !\in_array($currentPath, $this->validatedFields, true))) {

src/DependencyInjection/Compiler/OptionalDependencyPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
/**
1212
* @author Kevin Bond <[email protected]>
13+
*
14+
* @experimental
15+
*
16+
* @internal
1317
*/
1418
final class OptionalDependencyPass implements CompilerPassInterface
1519
{

src/DependencyInjection/LiveComponentExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
* @author Kevin Bond <[email protected]>
3131
*
3232
* @experimental
33+
*
34+
* @internal
3335
*/
3436
final class LiveComponentExtension extends Extension
3537
{

src/EventListener/AddLiveAttributesSubscriber.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
/**
1717
* @author Kevin Bond <[email protected]>
18+
*
19+
* @experimental
20+
*
21+
* @internal
1822
*/
1923
final class AddLiveAttributesSubscriber implements EventSubscriberInterface, ServiceSubscriberInterface
2024
{

src/EventListener/LiveComponentSubscriber.php

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,15 @@
4040
* @author Ryan Weaver <[email protected]>
4141
*
4242
* @experimental
43+
*
44+
* @internal
4345
*/
4446
class LiveComponentSubscriber implements EventSubscriberInterface, ServiceSubscriberInterface
4547
{
4648
private const HTML_CONTENT_TYPE = 'application/vnd.live-component+html';
4749

48-
private ContainerInterface $container;
49-
50-
public function __construct(ContainerInterface $container)
50+
public function __construct(private ContainerInterface $container)
5151
{
52-
$this->container = $container;
5352
}
5453

5554
public static function getSubscribedServices(): array
@@ -170,37 +169,29 @@ public function onKernelController(ControllerEvent $event): void
170169

171170
public function onKernelView(ViewEvent $event): void
172171
{
173-
$request = $event->getRequest();
174-
if (!$this->isLiveComponentRequest($request)) {
172+
if (!$this->isLiveComponentRequest($request = $event->getRequest())) {
175173
return;
176174
}
177175

178-
$response = $this->createResponse($request->attributes->get('_mounted_component'), $request);
179-
180-
$event->setResponse($response);
176+
$event->setResponse($this->createResponse($request->attributes->get('_mounted_component')));
181177
}
182178

183179
public function onKernelException(ExceptionEvent $event): void
184180
{
185-
$request = $event->getRequest();
186-
187-
if (!$this->isLiveComponentRequest($request)) {
181+
if (!$this->isLiveComponentRequest($request = $event->getRequest())) {
188182
return;
189183
}
190184

191185
if (!$event->getThrowable() instanceof UnprocessableEntityHttpException) {
192186
return;
193187
}
194188

195-
$mounted = $request->attributes->get('_mounted_component');
196-
197189
// in case the exception was too early somehow
198-
if (!$mounted) {
190+
if (!$mounted = $request->attributes->get('_mounted_component')) {
199191
return;
200192
}
201193

202-
$response = $this->createResponse($mounted, $request);
203-
$event->setResponse($response);
194+
$event->setResponse($this->createResponse($mounted));
204195
}
205196

206197
public function onKernelResponse(ResponseEvent $event): void
@@ -237,19 +228,15 @@ public static function getSubscribedEvents(): array
237228
];
238229
}
239230

240-
private function createResponse(MountedComponent $mounted, Request $request): Response
231+
private function createResponse(MountedComponent $mounted): Response
241232
{
242233
$component = $mounted->getComponent();
243234

244235
foreach (AsLiveComponent::beforeReRenderMethods($component) as $method) {
245236
$component->{$method->name}();
246237
}
247238

248-
$html = $this->container->get(ComponentRenderer::class)->render(
249-
$mounted,
250-
);
251-
252-
return new Response($html);
239+
return new Response($this->container->get(ComponentRenderer::class)->render($mounted));
253240
}
254241

255242
private function isLiveComponentRequest(Request $request): bool

src/Exception/UnsupportedHydrationException.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/LiveComponentHydrator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function dehydrate(MountedComponent $mounted): array
9191
$data[$frontendName] = [
9292
self::EXPOSED_PROP_KEY => $dehydratedValue,
9393
];
94+
9495
foreach ($liveProp->exposed() as $propertyPath) {
9596
$value = $this->propertyAccessor->getValue($component, sprintf('%s.%s', $name, $propertyPath));
9697
$data[$frontendName][$propertyPath] = \is_object($value) ? $this->normalizer->normalize($value, 'json', [self::LIVE_CONTEXT => true]) : $value;
@@ -154,6 +155,7 @@ public function hydrate(object $component, array $data, string $componentName):
154155
}
155156

156157
$dehydratedValue = $data[$frontendName];
158+
157159
// if there are exposed keys, then the main value should be hidden
158160
// in an array under self::EXPOSED_PROP_KEY. But if the value is
159161
// *not* an array, then use the main value. This could mean that,
@@ -173,7 +175,7 @@ public function hydrate(object $component, array $data, string $componentName):
173175
}
174176

175177
foreach ($liveProp->exposed() as $exposedProperty) {
176-
$propertyPath = $this->transformToArrayPath("{$name}.$exposedProperty");
178+
$propertyPath = self::transformToArrayPath("{$name}.$exposedProperty");
177179

178180
if (!$this->propertyAccessor->isReadable($data, $propertyPath)) {
179181
continue;
@@ -240,7 +242,7 @@ private function verifyChecksum(array $data, array $readonlyProperties): void
240242
* This allows us to use the property accessor to find this
241243
* inside an array.
242244
*/
243-
private function transformToArrayPath(string $propertyPath): string
245+
private static function transformToArrayPath(string $propertyPath): string
244246
{
245247
$parts = explode('.', $propertyPath);
246248
$path = '';

src/Normalizer/DoctrineObjectNormalizer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* @author Kevin Bond <[email protected]>
2222
*
2323
* @experimental
24+
*
25+
* @internal
2426
*/
2527
final class DoctrineObjectNormalizer implements NormalizerInterface, DenormalizerInterface
2628
{

src/Twig/LiveComponentExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* @author Kevin Bond <[email protected]>
1919
*
2020
* @experimental
21+
*
22+
* @internal
2123
*/
2224
final class LiveComponentExtension extends AbstractExtension
2325
{

src/Twig/LiveComponentRuntime.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @author Kevin Bond <[email protected]>
2020
*
2121
* @experimental
22+
*
23+
* @internal
2224
*/
2325
final class LiveComponentRuntime
2426
{

0 commit comments

Comments
 (0)