Skip to content

Commit 1df584a

Browse files
committed
feature #1068 [LiveComponent] Add priority to PreReRender hook (sneakyvv)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] Add priority to PreReRender hook | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | | License | MIT Analogue with Pre- & PostMount hooks in AsTwigComponent, if you have multiple PreReRender methods, some may have to be run first. Commits ------- 73bd74b [LiveComponent] Add priority to PreReRender hook
2 parents 2609c67 + 73bd74b commit 1df584a

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/LiveComponent/src/Attribute/AsLiveComponent.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ public static function isActionAllowed(object $component, string $action): bool
6565
*
6666
* @return \ReflectionMethod[]
6767
*/
68-
public static function preReRenderMethods(object $component): \Traversable
68+
public static function preReRenderMethods(object $component): iterable
6969
{
70-
yield from self::attributeMethodsFor(PreReRender::class, $component);
70+
$methods = iterator_to_array(self::attributeMethodsFor(PreReRender::class, $component));
71+
72+
usort($methods, static function (\ReflectionMethod $a, \ReflectionMethod $b) {
73+
return $a->getAttributes(PreReRender::class)[0]->newInstance()->priority <=> $b->getAttributes(PreReRender::class)[0]->newInstance()->priority;
74+
});
75+
76+
return array_reverse($methods);
7177
}
7278

7379
/**

src/LiveComponent/src/Attribute/PreReRender.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@
2222
#[\Attribute(\Attribute::TARGET_METHOD)]
2323
final class PreReRender
2424
{
25+
/**
26+
* @param int $priority If multiple hooks are registered in a component, use to configure
27+
* the order in which they are called (higher called earlier)
28+
*/
29+
public function __construct(public int $priority = 0)
30+
{
31+
}
2532
}

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
16+
use Symfony\UX\LiveComponent\Attribute\PreReRender;
1617
use Symfony\UX\LiveComponent\Tests\Fixtures\Component\Component5;
1718

1819
/**
@@ -36,12 +37,31 @@ public function testCanGetPostHydrateMethods(): void
3637
$this->assertSame('method5', $methods[0]->getName());
3738
}
3839

39-
public function testCanGetPreReRenderMethods(): void
40+
public function testPreMountHooksAreOrderedByPriority(): void
4041
{
41-
$methods = iterator_to_array(AsLiveComponent::preReRenderMethods(new Component5()));
42+
$hooks = AsLiveComponent::preReRenderMethods(
43+
new class() {
44+
#[PreReRender(priority: -10)]
45+
public function hook1()
46+
{
47+
}
4248

43-
$this->assertCount(1, $methods);
44-
$this->assertSame('method3', $methods[0]->getName());
49+
#[PreReRender(priority: 10)]
50+
public function hook2()
51+
{
52+
}
53+
54+
#[PreReRender]
55+
public function hook3()
56+
{
57+
}
58+
}
59+
);
60+
61+
$this->assertCount(3, $hooks);
62+
$this->assertSame('hook2', $hooks[0]->name);
63+
$this->assertSame('hook3', $hooks[1]->name);
64+
$this->assertSame('hook1', $hooks[2]->name);
4565
}
4666

4767
public function testCanGetLiveListeners(): void

0 commit comments

Comments
 (0)