From 048e3511655644ca278b903f0d695b03d995e94e Mon Sep 17 00:00:00 2001 From: Quentin Dequippe Date: Mon, 16 Jun 2025 12:20:45 +0200 Subject: [PATCH] Lazy loaded Twig Extension # Conflicts: # src/Twig/MercureExtension.php --- src/Twig/MercureExtension.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Twig/MercureExtension.php b/src/Twig/MercureExtension.php index 0081b22..5007615 100644 --- a/src/Twig/MercureExtension.php +++ b/src/Twig/MercureExtension.php @@ -16,15 +16,14 @@ use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Mercure\Authorization; use Symfony\Component\Mercure\HubRegistry; -use Twig\Extension\AbstractExtension; -use Twig\TwigFunction; +use Twig\Attribute\AsTwigFunction; /** * Registers the Twig helper function. * * @author Kévin Dunglas */ -final class MercureExtension extends AbstractExtension +final class MercureExtension { public function __construct( private readonly HubRegistry $hubRegistry, @@ -33,17 +32,13 @@ public function __construct( ) { } - public function getFunctions(): array - { - return [new TwigFunction('mercure', [$this, 'mercure'])]; - } - /** * @param string|string[]|null $topics A topic or an array of topics to subscribe for. If this parameter is omitted or `null` is passed, the URL of the hub will be returned (useful for publishing in JavaScript). * @param array{subscribe?: string[]|string, publish?: string[]|string, additionalClaims?: array, lastEventId?: string, hub?: string} $options The options to pass to the JWT factory * * @return string The URL of the hub with the appropriate "topic" query parameters (if any) */ + #[AsTwigFunction('mercure')] public function mercure(string|array|null $topics = null, array $options = []): string { $hub = $options['hub'] ?? null;