Skip to content

Commit 2ff380a

Browse files
committed
minor fixes from reviews
1 parent b397d83 commit 2ff380a

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

src/LiveComponent/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ class RandomNumberComponent
119119
```
120120

121121
To transform this into a "live" component (i.e. one that
122-
can be re-rendered live on the frontend), replace
123-
component's `AsTwigComponent` attribute to `AsLiveComponent`:
122+
can be re-rendered live on the frontend), replace the
123+
component's `AsTwigComponent` attribute with `AsLiveComponent`:
124124

125125
```diff
126126
// src/Components/RandomNumberComponent.php
@@ -690,9 +690,9 @@ class PostFormComponent extends AbstractController
690690
* Needed so the same form can be re-created
691691
* when the component is re-rendered via Ajax.
692692
*
693-
* The fieldName="" option is needed in this situation because
693+
* The `fieldName` option is needed in this situation because
694694
* the form renders fields with names like `name="post[title]"`.
695-
* We set fieldName="" so that this live prop doesn't collide
695+
* We set `fieldName: ''` so that this live prop doesn't collide
696696
* with that data. The value - initialFormData - could be anything.
697697
*/
698698
#[LiveProp(fieldName: 'initialFormData')]
@@ -1016,8 +1016,8 @@ class EditUserComponent
10161016
}
10171017
```
10181018

1019-
Be sure to add the `Assert\IsValid` to any property where you want
1020-
the object on that property to also be validated.
1019+
Be sure to add the `IsValid` attribute/annotation to any property where
1020+
you want the object on that property to also be validated.
10211021

10221022
Thanks to this setup, the component will now be automatically validated
10231023
on each render, but in a smart way: a property will only be validated

src/LiveComponent/src/Attribute/AsLiveComponent.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ final class AsLiveComponent extends AsTwigComponent
2424
/**
2525
* @internal
2626
*
27-
* @param string|object $classOrObject
28-
*
2927
* @return LivePropContext[]
3028
*/
31-
public static function liveProps($classOrObject): \Traversable
29+
public static function liveProps(object $component): \Traversable
3230
{
33-
foreach (self::propertiesFor($classOrObject) as $property) {
31+
foreach (self::propertiesFor($component) as $property) {
3432
if ($attribute = $property->getAttributes(LiveProp::class)[0] ?? null) {
3533
yield new LivePropContext($attribute->newInstance(), $property);
3634
}
@@ -39,12 +37,10 @@ public static function liveProps($classOrObject): \Traversable
3937

4038
/**
4139
* @internal
42-
*
43-
* @param string|object $classOrObject
4440
*/
45-
public static function isActionAllowed($classOrObject, string $action): bool
41+
public static function isActionAllowed(object $component, string $action): bool
4642
{
47-
foreach (self::attributeMethodsFor(LiveAction::class, $classOrObject) as $method) {
43+
foreach (self::attributeMethodsFor(LiveAction::class, $component) as $method) {
4844
if ($action === $method->getName()) {
4945
return true;
5046
}
@@ -56,61 +52,53 @@ public static function isActionAllowed($classOrObject, string $action): bool
5652
/**
5753
* @internal
5854
*
59-
* @param string|object $classOrObject
60-
*
6155
* @return \ReflectionMethod[]
6256
*/
63-
public static function beforeReRenderMethods($classOrObject): \Traversable
57+
public static function beforeReRenderMethods(object $component): \Traversable
6458
{
65-
yield from self::attributeMethodsFor(BeforeReRender::class, $classOrObject);
59+
yield from self::attributeMethodsFor(BeforeReRender::class, $component);
6660
}
6761

6862
/**
6963
* @internal
7064
*
71-
* @param string|object $classOrObject
72-
*
7365
* @return \ReflectionMethod[]
7466
*/
75-
public static function postHydrateMethods($classOrObject): \Traversable
67+
public static function postHydrateMethods(object $component): \Traversable
7668
{
77-
yield from self::attributeMethodsFor(PostHydrate::class, $classOrObject);
69+
yield from self::attributeMethodsFor(PostHydrate::class, $component);
7870
}
7971

8072
/**
8173
* @internal
8274
*
83-
* @param string|object $classOrObject
84-
*
8575
* @return \ReflectionMethod[]
8676
*/
87-
public static function preDehydrateMethods($classOrObject): \Traversable
77+
public static function preDehydrateMethods(object $component): \Traversable
8878
{
89-
yield from self::attributeMethodsFor(PreDehydrate::class, $classOrObject);
79+
yield from self::attributeMethodsFor(PreDehydrate::class, $component);
9080
}
9181

9282
/**
9383
* @param string|object $classOrObject
9484
*
9585
* @return \ReflectionMethod[]
9686
*/
97-
private static function attributeMethodsFor(string $attribute, $classOrObject): \Traversable
87+
private static function attributeMethodsFor(string $attribute, object $component): \Traversable
9888
{
99-
foreach ((new \ReflectionClass($classOrObject))->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
89+
foreach ((new \ReflectionClass($component))->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
10090
if ($method->getAttributes($attribute)[0] ?? null) {
10191
yield $method;
10292
}
10393
}
10494
}
10595

10696
/**
107-
* @param string|object $classOrObject
108-
*
10997
* @return \ReflectionProperty[]
11098
*/
111-
private static function propertiesFor($classOrObject): \Traversable
99+
private static function propertiesFor(object $object): \Traversable
112100
{
113-
$class = $classOrObject instanceof \ReflectionClass ? $classOrObject : new \ReflectionClass($classOrObject);
101+
$class = $object instanceof \ReflectionClass ? $object : new \ReflectionClass($object);
114102

115103
foreach ($class->getProperties() as $property) {
116104
yield $property;

src/LiveComponent/src/EventListener/LiveComponentSubscriber.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class LiveComponentSubscriber implements EventSubscriberInterface, ServiceSubscr
4545
private const JSON_FORMAT = 'live-component-json';
4646
private const JSON_CONTENT_TYPE = 'application/vnd.live-component+json';
4747

48+
/** @var array<string, string> */
4849
private array $componentServiceMap;
4950
private ContainerInterface $container;
5051

0 commit comments

Comments
 (0)