Skip to content

Commit

Permalink
Allow class-string argument in AsLiveComponent static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
smnandre authored and weaverryan committed Oct 11, 2023
1 parent f52d7af commit 7266159
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
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;
}
}

0 comments on commit 7266159

Please sign in to comment.