Skip to content

Commit 7266159

Browse files
smnandreweaverryan
authored andcommitted
Allow class-string argument in AsLiveComponent static methods
1 parent f52d7af commit 7266159

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

src/LiveComponent/src/Attribute/AsLiveComponent.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ public function serviceConfig(): array
4848

4949
/**
5050
* @internal
51+
*
52+
* @param object|class-string $component
5153
*/
52-
public static function isActionAllowed(object $component, string $action): bool
54+
public static function isActionAllowed(object|string $component, string $action): bool
5355
{
5456
foreach (self::attributeMethodsFor(LiveAction::class, $component) as $method) {
5557
if ($action === $method->getName()) {
@@ -63,34 +65,47 @@ public static function isActionAllowed(object $component, string $action): bool
6365
/**
6466
* @internal
6567
*
68+
* @param object|class-string $component
69+
*
6670
* @return \ReflectionMethod[]
6771
*/
68-
public static function preReRenderMethods(object $component): iterable
72+
public static function preReRenderMethods(object|string $component): iterable
6973
{
7074
return self::attributeMethodsByPriorityFor($component, PreReRender::class);
7175
}
7276

7377
/**
7478
* @internal
7579
*
80+
* @param object|class-string $component
81+
*
7682
* @return \ReflectionMethod[]
7783
*/
78-
public static function postHydrateMethods(object $component): iterable
84+
public static function postHydrateMethods(object|string $component): iterable
7985
{
8086
return self::attributeMethodsByPriorityFor($component, PostHydrate::class);
8187
}
8288

8389
/**
8490
* @internal
8591
*
92+
* @param object|class-string $component
93+
*
8694
* @return \ReflectionMethod[]
8795
*/
88-
public static function preDehydrateMethods(object $component): iterable
96+
public static function preDehydrateMethods(object|string $component): iterable
8997
{
9098
return self::attributeMethodsByPriorityFor($component, PreDehydrate::class);
9199
}
92100

93-
public static function liveListeners(object $component): array
101+
/**
102+
* @internal
103+
*
104+
* @param object|class-string $component
105+
*
106+
* @return array<array{action: string, event: string}>
107+
*/
108+
public static function liveListeners(object|string $component): array
94109
{
95110
$listeners = [];
96111
foreach (self::attributeMethodsFor(LiveListener::class, $component) as $method) {

src/LiveComponent/tests/Unit/Attribute/AsLiveComponentTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
16+
use Symfony\UX\LiveComponent\Attribute\LiveAction;
17+
use Symfony\UX\LiveComponent\Attribute\LiveListener;
1618
use Symfony\UX\LiveComponent\Attribute\PostHydrate;
1719
use Symfony\UX\LiveComponent\Attribute\PreDehydrate;
1820
use Symfony\UX\LiveComponent\Attribute\PreReRender;
@@ -104,6 +106,15 @@ public function hook3()
104106
$this->assertSame('hook1', $hooks[2]->name);
105107
}
106108

109+
public function testCanGetPostHydrateMethodsFromClassString(): void
110+
{
111+
$methods = AsLiveComponent::postHydrateMethods(DummyLiveComponent::class);
112+
113+
$this->assertCount(1, $methods);
114+
$this->assertSame('method', $methods[0]->getName());
115+
$this->assertSame(DummyLiveComponent::class, $methods[0]->getDeclaringClass()?->getName());
116+
}
117+
107118
public function testCanGetLiveListeners(): void
108119
{
109120
$liveListeners = AsLiveComponent::liveListeners(new Component5());
@@ -115,6 +126,17 @@ public function testCanGetLiveListeners(): void
115126
], $liveListeners[0]);
116127
}
117128

129+
public function testCanGetLiveListenersFromClassString(): void
130+
{
131+
$liveListeners = AsLiveComponent::liveListeners(DummyLiveComponent::class);
132+
133+
$this->assertCount(1, $liveListeners);
134+
$this->assertSame([
135+
'action' => 'method',
136+
'event' => 'event_name',
137+
], $liveListeners[0]);
138+
}
139+
118140
public function testCanCheckIfMethodIsAllowed(): void
119141
{
120142
$component = new Component5();
@@ -124,3 +146,17 @@ public function testCanCheckIfMethodIsAllowed(): void
124146
$this->assertTrue(AsLiveComponent::isActionAllowed($component, 'aListenerActionMethod'));
125147
}
126148
}
149+
150+
#[AsLiveComponent]
151+
class DummyLiveComponent
152+
{
153+
#[PreDehydrate]
154+
#[PreReRender]
155+
#[PostHydrate]
156+
#[LiveListener('event_name')]
157+
#[LiveAction]
158+
public function method(): bool
159+
{
160+
return true;
161+
}
162+
}

0 commit comments

Comments
 (0)