Skip to content

Allow class-string argument in AsLiveComponent static methods #1170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/LiveComponent/src/Attribute/AsLiveComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public function serviceConfig(): array

/**
* @internal
*
* @param object|class-string $component
*/
public static function isActionAllowed(object $component, string $action): bool
public static function isActionAllowed(object|string $component, string $action): bool
{
foreach (self::attributeMethodsFor(LiveAction::class, $component) as $method) {
if ($action === $method->getName()) {
Expand All @@ -63,34 +65,47 @@ public static function isActionAllowed(object $component, string $action): bool
/**
* @internal
*
* @param object|class-string $component
*
* @return \ReflectionMethod[]
*/
public static function preReRenderMethods(object $component): iterable
public static function preReRenderMethods(object|string $component): iterable
{
return self::attributeMethodsByPriorityFor($component, PreReRender::class);
}

/**
* @internal
*
* @param object|class-string $component
*
* @return \ReflectionMethod[]
*/
public static function postHydrateMethods(object $component): iterable
public static function postHydrateMethods(object|string $component): iterable
{
return self::attributeMethodsByPriorityFor($component, PostHydrate::class);
}

/**
* @internal
*
* @param object|class-string $component
*
* @return \ReflectionMethod[]
*/
public static function preDehydrateMethods(object $component): iterable
public static function preDehydrateMethods(object|string $component): iterable
{
return self::attributeMethodsByPriorityFor($component, PreDehydrate::class);
}

public static function liveListeners(object $component): array
/**
* @internal
*
* @param object|class-string $component
*
* @return array<array{action: string, event: string}>
*/
public static function liveListeners(object|string $component): array
{
$listeners = [];
foreach (self::attributeMethodsFor(LiveListener::class, $component) as $method) {
Expand Down
36 changes: 36 additions & 0 deletions src/LiveComponent/tests/Unit/Attribute/AsLiveComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use PHPUnit\Framework\TestCase;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\Attribute\PostHydrate;
use Symfony\UX\LiveComponent\Attribute\PreDehydrate;
use Symfony\UX\LiveComponent\Attribute\PreReRender;
Expand Down Expand Up @@ -104,6 +106,15 @@ public function hook3()
$this->assertSame('hook1', $hooks[2]->name);
}

public function testCanGetPostHydrateMethodsFromClassString(): void
{
$methods = AsLiveComponent::postHydrateMethods(DummyLiveComponent::class);

$this->assertCount(1, $methods);
$this->assertSame('method', $methods[0]->getName());
$this->assertSame(DummyLiveComponent::class, $methods[0]->getDeclaringClass()?->getName());
}

public function testCanGetLiveListeners(): void
{
$liveListeners = AsLiveComponent::liveListeners(new Component5());
Expand All @@ -115,6 +126,17 @@ public function testCanGetLiveListeners(): void
], $liveListeners[0]);
}

public function testCanGetLiveListenersFromClassString(): void
{
$liveListeners = AsLiveComponent::liveListeners(DummyLiveComponent::class);

$this->assertCount(1, $liveListeners);
$this->assertSame([
'action' => 'method',
'event' => 'event_name',
], $liveListeners[0]);
}

public function testCanCheckIfMethodIsAllowed(): void
{
$component = new Component5();
Expand All @@ -124,3 +146,17 @@ public function testCanCheckIfMethodIsAllowed(): void
$this->assertTrue(AsLiveComponent::isActionAllowed($component, 'aListenerActionMethod'));
}
}

#[AsLiveComponent]
class DummyLiveComponent
{
#[PreDehydrate]
#[PreReRender]
#[PostHydrate]
#[LiveListener('event_name')]
#[LiveAction]
public function method(): bool
{
return true;
}
}