Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| EventSubscriberListenerProviderExtension | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| __invoke | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| resolveEventSubscriber | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| 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\ListenerProvider\EventSubscriberListenerProvider; |
| 23 | use FastForward\EventDispatcher\ServiceProvider\Configuration\ConfiguredListenerProviderCollection; |
| 24 | use Psr\Container\ContainerInterface; |
| 25 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 26 | |
| 27 | /** |
| 28 | * Populate the subscriber-based listener provider from configured listeners. |
| 29 | * |
| 30 | * @internal |
| 31 | */ |
| 32 | class EventSubscriberListenerProviderExtension |
| 33 | { |
| 34 | /** |
| 35 | * Add configured event subscribers to the provider instance. |
| 36 | * |
| 37 | * @param ContainerInterface $container container used to resolve subscriber services |
| 38 | * @param EventSubscriberListenerProvider $eventSubscriberListenerProvider provider to extend |
| 39 | * |
| 40 | * @throws RuntimeException thrown when a configured subscriber cannot be resolved |
| 41 | */ |
| 42 | public function __invoke( |
| 43 | ContainerInterface $container, |
| 44 | EventSubscriberListenerProvider $eventSubscriberListenerProvider, |
| 45 | ): void { |
| 46 | $configuredListeners = $container->get(ConfiguredListenerProviderCollection::class); |
| 47 | |
| 48 | foreach ($configuredListeners->eventSubscribers() as $eventSubscriber) { |
| 49 | $eventSubscriberListenerProvider->addSubscriber( |
| 50 | $this->resolveEventSubscriber($container, $eventSubscriber) |
| 51 | ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Resolve one configured subscriber to a concrete subscriber instance. |
| 57 | * |
| 58 | * @param ContainerInterface $container container used for service resolution |
| 59 | * @param EventSubscriberInterface|string $eventSubscriber subscriber instance or service identifier |
| 60 | * |
| 61 | * @return EventSubscriberInterface resolved subscriber instance |
| 62 | * |
| 63 | * @throws RuntimeException thrown when the resolved value is not an event subscriber |
| 64 | */ |
| 65 | private function resolveEventSubscriber( |
| 66 | ContainerInterface $container, |
| 67 | EventSubscriberInterface|string $eventSubscriber, |
| 68 | ): EventSubscriberInterface { |
| 69 | if (\is_string($eventSubscriber) && $container->has($eventSubscriber)) { |
| 70 | $eventSubscriber = $container->get($eventSubscriber); |
| 71 | } |
| 72 | |
| 73 | if (! $eventSubscriber instanceof EventSubscriberInterface) { |
| 74 | throw RuntimeException::forUnsupportedType($eventSubscriber); |
| 75 | } |
| 76 | |
| 77 | return $eventSubscriber; |
| 78 | } |
| 79 | } |