Skip to content

[TwigComponent] add priority to PreMount hook #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# CHANGELOG

# 2.1
## 2.1

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

- Add `PreMount` priority parameter.

## 2.0.0

- Support for `stimulus` version 2 was removed and support for `@hotwired/stimulus`
Expand Down
10 changes: 8 additions & 2 deletions src/TwigComponent/src/Attribute/AsTwigComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ public function __construct(string $name, ?string $template = null)
*
* @return \ReflectionMethod[]
*/
public static function preMountMethods(object $component): \Traversable
public static function preMountMethods(object $component): iterable
{
yield from self::attributeMethodsFor(PreMount::class, $component);
$methods = iterator_to_array(self::attributeMethodsFor(PreMount::class, $component));

usort($methods, static function (\ReflectionMethod $a, \ReflectionMethod $b) {
return $a->getAttributes(PreMount::class)[0]->newInstance()->priority <=> $b->getAttributes(PreMount::class)[0]->newInstance()->priority;
});

return array_reverse($methods);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/TwigComponent/src/Attribute/PreMount.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@
#[\Attribute(\Attribute::TARGET_METHOD)]
final class PreMount
{
public int $priority;

/**
* @param int $priority If multiple hooks are registered in a component, use to configure
* the order in which they are called (higher called earlier)
*/
public function __construct(int $priority = 0)
{
$this->priority = $priority;
}
}
6 changes: 6 additions & 0 deletions src/TwigComponent/src/Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ component use a ``PreMount`` hook::
// ...
}

.. note::

If your component has multiple ``PreMount`` hooks, and you'd like to control
the order in which they're called, use the ``priority`` attribute parameter:
``PreMount(priority: 10)`` (higher called earlier).

Fetching Services
-----------------

Expand Down
49 changes: 49 additions & 0 deletions src/TwigComponent/tests/Unit/Attribute/AsTwigComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\TwigComponent\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Attribute\PreMount;

/**
* @author Kevin Bond <[email protected]>
*/
final class AsTwigComponentTest extends TestCase
{
public function testPreMountHooksAreOrderedByPriority(): void
{
$hooks = AsTwigComponent::preMountMethods(
new class() {
#[PreMount(priority: -10)]
public function hook1()
{
}

#[PreMount(priority: 10)]
public function hook2()
{
}

#[PreMount]
public function hook3()
{
}
}
);

$this->assertCount(3, $hooks);
$this->assertSame('hook2', $hooks[0]->name);
$this->assertSame('hook3', $hooks[1]->name);
$this->assertSame('hook1', $hooks[2]->name);
}
}