Skip to content

Commit f701e13

Browse files
committed
feature #233 [TwigComponent] add priority to PreMount hook (kbond, weaverryan)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] add priority to PreMount hook | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | n/a | License | MIT Adds the ability to control the order in which `PreMount` methods are called via a priority parameter on the attribute. Commits ------- b018104 [TwigComponent] add priority to PreMount hook
2 parents 9f1ec06 + b018104 commit f701e13

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

src/TwigComponent/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# CHANGELOG
22

3-
# 2.1
3+
## 2.1
44

55
- Make public component properties available directly in the template (`{{ prop }}` vs `{{ this.prop }}`).
66

7+
- Add `PreMount` priority parameter.
8+
79
## 2.0.0
810

911
- Support for `stimulus` version 2 was removed and support for `@hotwired/stimulus`

src/TwigComponent/src/Attribute/AsTwigComponent.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ public function __construct(string $name, ?string $template = null)
3333
*
3434
* @return \ReflectionMethod[]
3535
*/
36-
public static function preMountMethods(object $component): \Traversable
36+
public static function preMountMethods(object $component): iterable
3737
{
38-
yield from self::attributeMethodsFor(PreMount::class, $component);
38+
$methods = iterator_to_array(self::attributeMethodsFor(PreMount::class, $component));
39+
40+
usort($methods, static function (\ReflectionMethod $a, \ReflectionMethod $b) {
41+
return $a->getAttributes(PreMount::class)[0]->newInstance()->priority <=> $b->getAttributes(PreMount::class)[0]->newInstance()->priority;
42+
});
43+
44+
return array_reverse($methods);
3945
}
4046

4147
/**

src/TwigComponent/src/Attribute/PreMount.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@
1919
#[\Attribute(\Attribute::TARGET_METHOD)]
2020
final class PreMount
2121
{
22+
public int $priority;
23+
24+
/**
25+
* @param int $priority If multiple hooks are registered in a component, use to configure
26+
* the order in which they are called (higher called earlier)
27+
*/
28+
public function __construct(int $priority = 0)
29+
{
30+
$this->priority = $priority;
31+
}
2232
}

src/TwigComponent/src/Resources/doc/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ component use a ``PreMount`` hook::
258258
// ...
259259
}
260260

261+
.. note::
262+
263+
If your component has multiple ``PreMount`` hooks, and you'd like to control
264+
the order in which they're called, use the ``priority`` attribute parameter:
265+
``PreMount(priority: 10)`` (higher called earlier).
266+
261267
Fetching Services
262268
-----------------
263269

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\TwigComponent\Tests\Unit;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
16+
use Symfony\UX\TwigComponent\Attribute\PreMount;
17+
18+
/**
19+
* @author Kevin Bond <[email protected]>
20+
*/
21+
final class AsTwigComponentTest extends TestCase
22+
{
23+
public function testPreMountHooksAreOrderedByPriority(): void
24+
{
25+
$hooks = AsTwigComponent::preMountMethods(
26+
new class() {
27+
#[PreMount(priority: -10)]
28+
public function hook1()
29+
{
30+
}
31+
32+
#[PreMount(priority: 10)]
33+
public function hook2()
34+
{
35+
}
36+
37+
#[PreMount]
38+
public function hook3()
39+
{
40+
}
41+
}
42+
);
43+
44+
$this->assertCount(3, $hooks);
45+
$this->assertSame('hook2', $hooks[0]->name);
46+
$this->assertSame('hook3', $hooks[1]->name);
47+
$this->assertSame('hook1', $hooks[2]->name);
48+
}
49+
}

0 commit comments

Comments
 (0)