4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Symfony\Component\DependencyInjection\Definition;
16 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17 use Symfony\Component\DependencyInjection\Reference;
18 use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
19 use Symfony\Component\DependencyInjection\TypedReference;
22 * Compiler pass to register tagged services that require a service locator.
24 * @author Nicolas Grekas <p@tchwork.com>
26 class RegisterServiceSubscribersPass extends AbstractRecursivePass
28 protected function processValue($value, $isRoot = false)
30 if (!$value instanceof Definition || $value->isAbstract() || $value->isSynthetic() || !$value->hasTag('container.service_subscriber')) {
31 return parent::processValue($value, $isRoot);
34 $serviceMap = array();
35 $autowire = $value->isAutowired();
37 foreach ($value->getTag('container.service_subscriber') as $attributes) {
43 if (array() !== array_diff(array_keys($attributes), array('id', 'key'))) {
44 throw new InvalidArgumentException(sprintf('The "container.service_subscriber" tag accepts only the "key" and "id" attributes, "%s" given for service "%s".', implode('", "', array_keys($attributes)), $this->currentId));
46 if (!array_key_exists('id', $attributes)) {
47 throw new InvalidArgumentException(sprintf('Missing "id" attribute on "container.service_subscriber" tag with key="%s" for service "%s".', $attributes['key'], $this->currentId));
49 if (!array_key_exists('key', $attributes)) {
50 $attributes['key'] = $attributes['id'];
52 if (isset($serviceMap[$attributes['key']])) {
55 $serviceMap[$attributes['key']] = new Reference($attributes['id']);
57 $class = $value->getClass();
59 if (!$r = $this->container->getReflectionClass($class)) {
60 throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $this->currentId));
62 if (!$r->isSubclassOf(ServiceSubscriberInterface::class)) {
63 throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $this->currentId, ServiceSubscriberInterface::class));
67 $subscriberMap = array();
68 $declaringClass = (new \ReflectionMethod($class, 'getSubscribedServices'))->class;
70 foreach ($class::getSubscribedServices() as $key => $type) {
71 if (!\is_string($type) || !preg_match('/^\??[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $type)) {
72 throw new InvalidArgumentException(sprintf('"%s::getSubscribedServices()" must return valid PHP types for service "%s" key "%s", "%s" returned.', $class, $this->currentId, $key, \is_string($type) ? $type : \gettype($type)));
74 if ($optionalBehavior = '?' === $type[0]) {
75 $type = substr($type, 1);
76 $optionalBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
81 if (!isset($serviceMap[$key])) {
83 throw new InvalidArgumentException(sprintf('Service "%s" misses a "container.service_subscriber" tag with "key"/"id" attributes corresponding to entry "%s" as returned by "%s::getSubscribedServices()".', $this->currentId, $key, $class));
85 $serviceMap[$key] = new Reference($type);
88 $subscriberMap[$key] = new TypedReference($this->container->normalizeId($serviceMap[$key]), $type, $declaringClass, $optionalBehavior ?: ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
89 unset($serviceMap[$key]);
92 if ($serviceMap = array_keys($serviceMap)) {
93 $message = sprintf(1 < \count($serviceMap) ? 'keys "%s" do' : 'key "%s" does', str_replace('%', '%%', implode('", "', $serviceMap)));
94 throw new InvalidArgumentException(sprintf('Service %s not exist in the map returned by "%s::getSubscribedServices()" for service "%s".', $message, $class, $this->currentId));
97 $value->addTag('container.service_subscriber.locator', array('id' => (string) ServiceLocatorTagPass::register($this->container, $subscriberMap, $this->currentId)));
99 return parent::processValue($value);