|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\DependencyInjection\Attribute; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Argument\IteratorArgument; |
| 15 | +use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 17 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 18 | +use Symfony\Component\DependencyInjection\TypedReference; |
| 19 | +use Symfony\Contracts\Service\Attribute\SubscribedService; |
| 20 | +use Symfony\Contracts\Service\ServiceSubscriberInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Autowires an iterator of services based on a tag name or an explicit list of key => service-type pairs. |
| 24 | + */ |
| 25 | +#[\Attribute(\Attribute::TARGET_PARAMETER)] |
| 26 | +class AutowireIterator extends Autowire |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @see ServiceSubscriberInterface::getSubscribedServices() |
| 30 | + * |
| 31 | + * @param string|array<string|SubscribedService> $services A tag name or an explicit list of services |
| 32 | + * @param string|string[] $exclude A service or a list of services to exclude |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + string|array $services, |
| 36 | + string $indexAttribute = null, |
| 37 | + string $defaultIndexMethod = null, |
| 38 | + string $defaultPriorityMethod = null, |
| 39 | + string|array $exclude = [], |
| 40 | + bool $excludeSelf = true, |
| 41 | + ) { |
| 42 | + if (\is_string($services)) { |
| 43 | + parent::__construct(new TaggedIteratorArgument($services, $indexAttribute, $defaultIndexMethod, false, $defaultPriorityMethod, (array) $exclude, $excludeSelf)); |
| 44 | + |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + $references = []; |
| 49 | + |
| 50 | + foreach ($services as $key => $type) { |
| 51 | + $attributes = []; |
| 52 | + |
| 53 | + if ($type instanceof SubscribedService) { |
| 54 | + $key = $type->key ?? $key; |
| 55 | + $attributes = $type->attributes; |
| 56 | + $type = ($type->nullable ? '?' : '').($type->type ?? throw new InvalidArgumentException(sprintf('When "%s" is used, a type must be set.', SubscribedService::class))); |
| 57 | + } |
| 58 | + |
| 59 | + if (!\is_string($type) || !preg_match('/(?(DEFINE)(?<cn>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+))(?(DEFINE)(?<fqcn>(?&cn)(?:\\\\(?&cn))*+))^\??(?&fqcn)(?:(?:\|(?&fqcn))*+|(?:&(?&fqcn))*+)$/', $type)) { |
| 60 | + throw new InvalidArgumentException(sprintf('"%s" is not a PHP type for key "%s".', \is_string($type) ? $type : get_debug_type($type), $key)); |
| 61 | + } |
| 62 | + $optionalBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; |
| 63 | + if ('?' === $type[0]) { |
| 64 | + $type = substr($type, 1); |
| 65 | + $optionalBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE; |
| 66 | + } |
| 67 | + if (\is_int($name = $key)) { |
| 68 | + $key = $type; |
| 69 | + $name = null; |
| 70 | + } |
| 71 | + |
| 72 | + $references[$key] = new TypedReference($type, $type, $optionalBehavior, $name, $attributes); |
| 73 | + } |
| 74 | + |
| 75 | + parent::__construct(new IteratorArgument($references)); |
| 76 | + } |
| 77 | +} |
0 commit comments