|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of the package bk2k/bootstrap-package. |
| 6 | + * |
| 7 | + * For the full copyright and license information, please read the |
| 8 | + * LICENSE file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace BK2K\BootstrapPackage\Service; |
| 12 | + |
| 13 | +use BK2K\BootstrapPackage\Events\ModifyIconProvidersEvent; |
| 14 | +use BK2K\BootstrapPackage\Icons\IconProviderInterface; |
| 15 | +use Symfony\Component\DependencyInjection\Attribute\AutowireIterator; |
| 16 | + |
| 17 | +final readonly class IconProviderService |
| 18 | +{ |
| 19 | + public function __construct( |
| 20 | + #[AutowireIterator('bootstrapPackage.iconProvider')] |
| 21 | + private iterable $iconProviders |
| 22 | + ) { |
| 23 | + } |
| 24 | + |
| 25 | + public function __invoke(ModifyIconProvidersEvent $event): void |
| 26 | + { |
| 27 | + $classList = []; |
| 28 | + $iconProviders = $event->getIconProviders(); |
| 29 | + |
| 30 | + // add already registered class names to prevent duplicate registration |
| 31 | + array_walk($iconProviders, static function (IconProviderInterface $iconProvider) use (&$classList) { |
| 32 | + $classList[] = get_class($iconProvider); |
| 33 | + }); |
| 34 | + |
| 35 | + /** @var IconProviderInterface $iconProvider */ |
| 36 | + foreach ($this->iconProviders as $iconProvider) { |
| 37 | + $className = get_class($iconProvider); |
| 38 | + if (in_array($className, $classList, true)) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + $iconProviders[] = $iconProvider; |
| 42 | + $classList[] = $className; |
| 43 | + } |
| 44 | + |
| 45 | + $event->setIconProviders($iconProviders); |
| 46 | + } |
| 47 | +} |
0 commit comments