Skip to content

Commit 810ae21

Browse files
committed
LiveComponentMetadata.getAllUrlMappings()
1 parent c0ff4ed commit 810ae21

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/LiveComponent/src/Metadata/LiveComponentMetadata.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ public function getOnlyPropsThatAcceptUpdatesFromParent(array $inputProps): arra
6666
return array_intersect_key($inputProps, array_flip($propNames));
6767
}
6868

69+
/**
70+
* @return UrlMapping[]
71+
*/
72+
public function getAllUrlMappings(): iterable
73+
{
74+
$urlMappings = [];
75+
foreach ($this->livePropsMetadata as $livePropMetadata) {
76+
if ($livePropMetadata->urlMapping()) {
77+
$urlMappings[$livePropMetadata->getName()] = $livePropMetadata->urlMapping();
78+
}
79+
}
80+
81+
return $urlMappings;
82+
}
83+
6984
public function hasQueryStringBindings($component): bool
7085
{
7186
foreach ($this->getAllLivePropsMetadata($component) as $livePropMetadata) {

src/LiveComponent/tests/Unit/Metadata/LiveComponentMetadataTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\UX\LiveComponent\Attribute\LiveProp;
1616
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadata;
1717
use Symfony\UX\LiveComponent\Metadata\LivePropMetadata;
18+
use Symfony\UX\LiveComponent\Metadata\UrlMapping;
1819
use Symfony\UX\TwigComponent\ComponentMetadata;
1920

2021
class LiveComponentMetadataTest extends TestCase
@@ -37,4 +38,19 @@ public function testGetOnlyPropsThatAcceptUpdatesFromParent()
3738
$actual = $liveComponentMetadata->getOnlyPropsThatAcceptUpdatesFromParent($inputProps);
3839
$this->assertEquals($expected, $actual);
3940
}
41+
42+
public function testGetAllUrlMappings(): void
43+
{
44+
$aliasUrlMapping = new UrlMapping('alias');
45+
$propMetadas = [
46+
new LivePropMetadata('noUrlMapping', new LiveProp(), null, false, false, null),
47+
new LivePropMetadata('basicUrlMapping', new LiveProp(url: true), null, false, false, null),
48+
new LivePropMetadata('aliasUrlMapping', new LiveProp(url: $aliasUrlMapping), null, false, false, null),
49+
];
50+
$liveComponentMetadata = new LiveComponentMetadata(new ComponentMetadata([]), $propMetadas);
51+
$urlMappings = $liveComponentMetadata->getAllUrlMappings();
52+
$this->assertCount(2, $urlMappings);
53+
$this->assertInstanceOf(UrlMapping::class, $urlMappings['basicUrlMapping']);
54+
$this->assertEquals($aliasUrlMapping, $urlMappings['aliasUrlMapping']);
55+
}
4056
}

0 commit comments

Comments
 (0)