Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ReflectionBasedListenerProviderExtension | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
| __invoke | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| resolveCallable | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(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 | |
| 19 | namespace FastForward\EventDispatcher\ServiceProvider\Extension; |
| 20 | |
| 21 | use FastForward\EventDispatcher\Exception\RuntimeException; |
| 22 | use FastForward\EventDispatcher\ServiceProvider\Configuration\ConfiguredListenerProviderCollection; |
| 23 | use Phly\EventDispatcher\LazyListener; |
| 24 | use Phly\EventDispatcher\ListenerProvider\ReflectionBasedListenerProvider; |
| 25 | use Psr\Container\ContainerInterface; |
| 26 | |
| 27 | /** |
| 28 | * Populate the reflection-based listener provider from configured listeners. |
| 29 | * |
| 30 | * @internal |
| 31 | */ |
| 32 | class ReflectionBasedListenerProviderExtension |
| 33 | { |
| 34 | /** |
| 35 | * Register configured reflection listeners with the provider. |
| 36 | * |
| 37 | * @param ContainerInterface $container container used to resolve listener services |
| 38 | * @param ReflectionBasedListenerProvider $reflectionBasedListenerProvider 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 | ReflectionBasedListenerProvider $reflectionBasedListenerProvider, |
| 45 | ): void { |
| 46 | $configuredListeners = $container->get(ConfiguredListenerProviderCollection::class); |
| 47 | |
| 48 | foreach ($configuredListeners->reflectionListeners() as $listener) { |
| 49 | $reflectionBasedListenerProvider->listen( |
| 50 | $this->resolveCallable($container, $listener->listener), |
| 51 | $listener->eventType, |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Resolve a configured reflection listener to a callable value. |
| 58 | * |
| 59 | * @param ContainerInterface $container container used for service resolution |
| 60 | * @param mixed $listener listener value or service identifier |
| 61 | * @param string|null $method listener method to call when the listener is not directly callable |
| 62 | * |
| 63 | * @return callable resolved callable listener |
| 64 | * |
| 65 | * @throws RuntimeException thrown when the listener cannot be resolved to a callable |
| 66 | */ |
| 67 | private function resolveCallable( |
| 68 | ContainerInterface $container, |
| 69 | mixed $listener, |
| 70 | ?string $method = null, |
| 71 | ): callable { |
| 72 | if (\is_string($listener) && $container->has($listener)) { |
| 73 | return new LazyListener($container, $listener, $method); |
| 74 | } |
| 75 | |
| 76 | if (null !== $method) { |
| 77 | $listener = [$listener, $method]; |
| 78 | } |
| 79 | |
| 80 | if (! \is_callable($listener)) { |
| 81 | throw RuntimeException::forUnsupportedType($listener); |
| 82 | } |
| 83 | |
| 84 | return $listener; |
| 85 | } |
| 86 | } |