Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
PrioritizedListenerProviderExtension
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 __invoke
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 resolveCallable
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of php-fast-forward/event-dispatcher.
7 *
8 * This source file is subject to the license bundled
9 * with this source code in the file LICENSE.
10 *
11 * @copyright Copyright (c) 2025-2026 Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 *
14 * @see       https://github.com/php-fast-forward/event-dispatcher
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\EventDispatcher\ServiceProvider\Extension;
20
21use FastForward\EventDispatcher\Exception\RuntimeException;
22use FastForward\EventDispatcher\ServiceProvider\Configuration\ConfiguredListenerProviderCollection;
23use Phly\EventDispatcher\LazyListener;
24use Phly\EventDispatcher\ListenerProvider\PrioritizedListenerProvider;
25use Psr\Container\ContainerInterface;
26
27/**
28 * Populate the prioritized listener provider from configured listeners.
29 *
30 * @internal
31 */
32  class PrioritizedListenerProviderExtension
33{
34    /**
35     * Register configured prioritized listeners with the provider.
36     *
37     * @param ContainerInterface $container container used to resolve listener services
38     * @param PrioritizedListenerProvider $prioritizedListenerProvider provider to extend
39     *
40     * @throws RuntimeException thrown when a configured listener cannot be resolved to a callable
41     */
42    public function __invoke(
43        ContainerInterface $container,
44        PrioritizedListenerProvider $prioritizedListenerProvider,
45    ): void {
46        $configuredListeners = $container->get(ConfiguredListenerProviderCollection::class);
47
48        foreach ($configuredListeners->prioritizedListeners() as $listener) {
49            $prioritizedListenerProvider->listen(
50                $listener->eventType,
51                $this->resolveCallable($container, $listener->listener, $listener->method),
52                $listener->priority,
53            );
54        }
55    }
56
57    /**
58     * Resolve a configured prioritized listener to a callable value.
59     *
60     * @param ContainerInterface $container container used for service resolution
61     * @param object|string|callable $listener listener value or service identifier
62     * @param string|null $method listener method to call when the listener is not directly callable
63     *
64     * @return callable resolved callable listener
65     *
66     * @throws RuntimeException thrown when the listener cannot be resolved to a callable
67     */
68    private function resolveCallable(
69        ContainerInterface $container,
70        object|string|callable $listener,
71        ?string $method = null,
72    ): callable {
73        if (\is_string($listener) && $container->has($listener)) {
74            return new LazyListener($container, $listener, $method);
75        }
76
77        if (null !== $method) {
78            $listener = [$listener, $method];
79        }
80
81        if (! \is_callable($listener)) {
82            throw RuntimeException::forUnsupportedType($listener);
83        }
84
85        return $listener;
86    }
87}