Skip to content

Allow repeated #[LiveListener] attributes on a method #1343

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 1 commit into from
Jan 9, 2024
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
1 change: 1 addition & 0 deletions src/LiveComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.14.0

- Add support for URL binding in `LiveProp`
- Allow multiple `LiveListener` attributes on a single method.

## 2.13.2

Expand Down
2 changes: 1 addition & 1 deletion src/LiveComponent/src/Attribute/LiveListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @experimental
*/
#[\Attribute(\Attribute::TARGET_METHOD)]
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class LiveListener extends LiveAction
{
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;

use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveListener;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent]
class ComponentWithRepeatedLiveListener
{
use DefaultActionTrait;

#[LiveListener('bar')]
public function onBar(): void
{
}

#[LiveListener('foo')]
#[LiveListener('bar')]
#[LiveListener('foo:bar')]
public function onFooBar(): void
{
}
}
33 changes: 33 additions & 0 deletions src/LiveComponent/tests/Unit/Attribute/AsLiveComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\UX\LiveComponent\Attribute\PreDehydrate;
use Symfony\UX\LiveComponent\Attribute\PreReRender;
use Symfony\UX\LiveComponent\Tests\Fixtures\Component\Component5;
use Symfony\UX\LiveComponent\Tests\Fixtures\Component\ComponentWithRepeatedLiveListener;

/**
* @author Kevin Bond <[email protected]>
Expand Down Expand Up @@ -137,6 +138,38 @@ public function testCanGetLiveListenersFromClassString(): void
], $liveListeners[0]);
}

public function testCanGetRepeatedLiveListeners(): void
{
$liveListeners = AsLiveComponent::liveListeners(new ComponentWithRepeatedLiveListener());

$this->assertCount(4, $liveListeners);
$this->assertSame([
[
'action' => 'onBar',
'event' => 'bar',
],
[
'action' => 'onFooBar',
'event' => 'foo',
],
[
'action' => 'onFooBar',
'event' => 'bar',
],
[
'action' => 'onFooBar',
'event' => 'foo:bar',
],
], $liveListeners);
}

public function testCanGetRepeatedLiveListenersFromClassString(): void
{
$liveListeners = AsLiveComponent::liveListeners(ComponentWithRepeatedLiveListener::class);

$this->assertCount(4, $liveListeners);
}

public function testCanCheckIfMethodIsAllowed(): void
{
$component = new Component5();
Expand Down