Skip to content

Commit d8c0068

Browse files
committed
feature #1343 Allow repeated #[LiveListener] attributes on a method (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- Allow repeated #[LiveListener] attributes on a method | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Feature | Request #1323 | License | MIT Allow repeated #[LiveListener] attributes on a method ```php #[LiveListener('foo')] #[LiveListener('bar')] #[LiveListener('foo:bar')] public function onFooBar(): void { } ``` Commits ------- 3e6adb9 allow repeated #[LiveListener] attributes on a method
2 parents 6f0843f + 3e6adb9 commit d8c0068

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

src/LiveComponent/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.14.0
44

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

78
## 2.13.2
89

src/LiveComponent/src/Attribute/LiveListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @experimental
2323
*/
24-
#[\Attribute(\Attribute::TARGET_METHOD)]
24+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
2525
class LiveListener extends LiveAction
2626
{
2727
/**
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)