|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Compiler; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; |
| 15 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 16 | +use Symfony\Component\DependencyInjection\Reference; |
| 17 | +use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; |
| 18 | +use Symfony\Component\DependencyInjection\AutowirableReference; |
| 19 | + |
| 20 | +/** |
| 21 | + * Compiler pass to register tagged services that require a service locator. |
| 22 | + * |
| 23 | + * @author Nicolas Grekas <p@tchwork.com> |
| 24 | + * |
| 25 | + * @experimental in version 3.3 |
| 26 | + */ |
| 27 | +class RegisterServiceSubscribersPass extends AbstractRecursivePass |
| 28 | +{ |
| 29 | + private $serviceLocator; |
| 30 | + |
| 31 | + protected function processValue($value, $isRoot = false) |
| 32 | + { |
| 33 | + if ($value instanceof Reference && $this->serviceLocator && 'service_container' === (string) $value) { |
| 34 | + return new Reference($this->serviceLocator); |
| 35 | + } |
| 36 | + |
| 37 | + if (!$value instanceof Definition || $value->isAbstract() || $value->isSynthetic() || !$value->hasTag('kernel.service_subscriber')) { |
| 38 | + return $value; |
| 39 | + } |
| 40 | + |
| 41 | + $serviceMap = array(); |
| 42 | + |
| 43 | + foreach ($value->getTag($name) as $services) { |
| 44 | + foreach ($services as $key => $service) { |
| 45 | + if (!isset($service[0]) || '?' === $service) { |
| 46 | + throw new InvalidArgumentException(sprintf('A "kernel.service_subscriber" attribute must be a non-empty string for service "%s", key "%s".', $id, $key)); |
| 47 | + } |
| 48 | + if ($isOptional = '?' === $service[0]) { |
| 49 | + $service = substr($service, 1); |
| 50 | + } |
| 51 | + if (is_int($key)) { |
| 52 | + $key = $service; |
| 53 | + } |
| 54 | + $serviceMap[$key] = new Reference($service, $isOptional ? ContainerInterface::IGNORE_ON_INVALID_REFERENCE : ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE); |
| 55 | + } |
| 56 | + } |
| 57 | + $class = $value->getClass(); |
| 58 | + |
| 59 | + if (!is_subclass_of($class, ServiceSubscriberInterface::class)) { |
| 60 | + if (!class_exists($class, false)) { |
| 61 | + throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $this->currentId)); |
| 62 | + } |
| 63 | + |
| 64 | + throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $this->currentId, ServiceSubscriberInterface::class)); |
| 65 | + } |
| 66 | + $this->container->addObjectResource($class); |
| 67 | + |
| 68 | + foreach ($class::getSubscribedServices() as $key => $type) { |
| 69 | + if (!is_string($type) || !isset($type[0]) || '?' === $type) { |
| 70 | + throw new InvalidArgumentException(sprintf('%s::getSubscribedServices() must return types as non-empty strings for service "%s" key "%s", "%s" returned.', $class, $this->currentId, $key, gettype($type))); |
| 71 | + } |
| 72 | + if ($isOptional = '?' === $type[0]) { |
| 73 | + $type = substr($type, 1); |
| 74 | + } |
| 75 | + if (is_int($key)) { |
| 76 | + $key = $type; |
| 77 | + } |
| 78 | + if (!isset($serviceMap[$key])) { |
| 79 | + $serviceMap[$key] = new AutowirableReference($service, $type, $isOptional ? ContainerInterface::IGNORE_ON_INVALID_REFERENCE : ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE); |
| 80 | + } elseif ($isRequired) { |
| 81 | + $serviceMap[$key] = new Reference((string) $serviceMap[$key]); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + $serviceLocator = $this->serviceLocator; |
| 86 | + $this->serviceLocator = 'service_locator.'.$this->currentId.spl_object_hash($value); |
| 87 | + $this->container->setDefinition($this->serviceLocator, new ServiceLocatorArgument($serviceMap))->setPublic(false); |
| 88 | + |
| 89 | + try { |
| 90 | + return $this->processValue($value); |
| 91 | + } finally { |
| 92 | + $this->serviceLocator = $serviceLocator; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments