Skip to content

Commit a219df2

Browse files
committed
feature #282 [Twig][Live] Cleanup (kbond)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Twig][Live] Cleanup | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Tickets | n/a | License | MIT - General cleanup (remove dead code/use constructor property promotion where applicable) - Explicitly mark internal API - (cs) move `use function...` after `use` Commits ------- ca78deb [Twig][Live] Cleanup
2 parents acfa92b + ca78deb commit a219df2

30 files changed

+128
-115
lines changed

.php-cs-fixer.dist.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
'native_constant_invocation' => true,
1717
'combine_nested_dirname' => true,
1818
'list_syntax' => ['syntax' => 'short'],
19+
'ordered_imports' => [
20+
'imports_order' => ['const', 'class', 'function'],
21+
],
1922
])
2023
->setRiskyAllowed(true)
2124
->setFinder(

src/LiveComponent/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/LiveComponent/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/LiveComponent/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/LiveComponent/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/LiveComponent/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
#[PreReRender]
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/LiveComponent/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/LiveComponent/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/LiveComponent/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/LiveComponent/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::preReRenderMethods($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

0 commit comments

Comments
 (0)