Skip to content

Commit 81f5ee7

Browse files
committed
Allow repeated #[LiveListener] attributes on a method
1 parent 23100d0 commit 81f5ee7

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/LiveComponent/src/Attribute/LiveListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @experimental
1616
*/
17-
#[\Attribute(\Attribute::TARGET_METHOD)]
17+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
1818
class LiveListener extends LiveAction
1919
{
2020
public function __construct(private string $eventName)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;
13+
14+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
15+
use Symfony\UX\LiveComponent\Attribute\LiveListener;
16+
use Symfony\UX\LiveComponent\DefaultActionTrait;
17+
18+
#[AsLiveComponent]
19+
class ComponentWithRepeatedLiveListener
20+
{
21+
use DefaultActionTrait;
22+
23+
#[LiveListener('bar')]
24+
public function onBar(): void
25+
{
26+
}
27+
28+
#[LiveListener('foo')]
29+
#[LiveListener('bar')]
30+
#[LiveListener('foo:bar')]
31+
public function onFooBar(): void
32+
{
33+
}
34+
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\UX\LiveComponent\Attribute\PreDehydrate;
2020
use Symfony\UX\LiveComponent\Attribute\PreReRender;
2121
use Symfony\UX\LiveComponent\Tests\Fixtures\Component\Component5;
22+
use Symfony\UX\LiveComponent\Tests\Fixtures\Component\ComponentWithRepeatedLiveListener;
2223

2324
/**
2425
* @author Kevin Bond <[email protected]>
@@ -137,6 +138,38 @@ public function testCanGetLiveListenersFromClassString(): void
137138
], $liveListeners[0]);
138139
}
139140

141+
public function testCanGetRepeatedLiveListeners(): void
142+
{
143+
$liveListeners = AsLiveComponent::liveListeners(new ComponentWithRepeatedLiveListener());
144+
145+
$this->assertCount(4, $liveListeners);
146+
$this->assertSame([
147+
[
148+
'action' => 'onBar',
149+
'event' => 'bar',
150+
],
151+
[
152+
'action' => 'onFooBar',
153+
'event' => 'foo',
154+
],
155+
[
156+
'action' => 'onFooBar',
157+
'event' => 'bar',
158+
],
159+
[
160+
'action' => 'onFooBar',
161+
'event' => 'foo:bar',
162+
],
163+
], $liveListeners);
164+
}
165+
166+
public function testCanGetRepeatedLiveListenersFromClassString(): void
167+
{
168+
$liveListeners = AsLiveComponent::liveListeners(ComponentWithRepeatedLiveListener::class);
169+
170+
$this->assertCount(4, $liveListeners);
171+
}
172+
140173
public function testCanCheckIfMethodIsAllowed(): void
141174
{
142175
$component = new Component5();

0 commit comments

Comments
 (0)