|
| 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\ServiceClosureArgument; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 16 | +use Symfony\Component\DependencyInjection\Definition; |
| 17 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 18 | +use Symfony\Component\DependencyInjection\Reference; |
| 19 | +use Symfony\Component\DependencyInjection\ServiceSubscriberInterface; |
| 20 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
| 21 | +use Symfony\Component\DependencyInjection\TypedReference; |
| 22 | + |
| 23 | +/** |
| 24 | + * Compiler pass to register tagged services that require a service locator. |
| 25 | + * |
| 26 | + * @author Nicolas Grekas <p@tchwork.com> |
| 27 | + */ |
| 28 | +class RegisterServiceSubscribersPass extends AbstractRecursivePass |
| 29 | +{ |
| 30 | + private $serviceLocator; |
| 31 | + |
| 32 | + protected function processValue($value, $isRoot = false) |
| 33 | + { |
| 34 | + if ($value instanceof Reference && $this->serviceLocator && 'container' === (string) $value) { |
| 35 | + return new Reference($this->serviceLocator); |
| 36 | + } |
| 37 | + |
| 38 | + if (!$value instanceof Definition || $value->isAbstract() || $value->isSynthetic() || !$value->hasTag('container.service_subscriber')) { |
| 39 | + return parent::processValue($value, $isRoot); |
| 40 | + } |
| 41 | + |
| 42 | + $serviceMap = array(); |
| 43 | + |
| 44 | + foreach ($value->getTag('container.service_subscriber') as $attributes) { |
| 45 | + if (!$attributes) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + ksort($attributes); |
| 49 | + if (array() !== array_diff(array_keys($attributes), array('id', 'key'))) { |
| 50 | + 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)); |
| 51 | + } |
| 52 | + if (!array_key_exists('id', $attributes)) { |
| 53 | + throw new InvalidArgumentException(sprintf('Missing "id" attribute on "container.service_subscriber" tag with key="%s" for service "%s".', $attributes['key'], $this->currentId)); |
| 54 | + } |
| 55 | + if (!array_key_exists('key', $attributes)) { |
| 56 | + $attributes['key'] = $attributes['id']; |
| 57 | + } |
| 58 | + if (isset($serviceMap[$attributes['key']])) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + $serviceMap[$attributes['key']] = new Reference($attributes['id']); |
| 62 | + } |
| 63 | + $class = $value->getClass(); |
| 64 | + |
| 65 | + if (!is_subclass_of($class, ServiceSubscriberInterface::class)) { |
| 66 | + if (!class_exists($class, false)) { |
| 67 | + throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $this->currentId)); |
| 68 | + } |
| 69 | + |
| 70 | + throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $this->currentId, ServiceSubscriberInterface::class)); |
| 71 | + } |
| 72 | + $this->container->addObjectResource($class); |
| 73 | + $subscriberMap = array(); |
| 74 | + |
| 75 | + foreach ($class::getSubscribedServices() as $key => $type) { |
| 76 | + 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)) { |
| 77 | + 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))); |
| 78 | + } |
| 79 | + if ($optionalBehavior = '?' === $type[0]) { |
| 80 | + $type = substr($type, 1); |
| 81 | + $optionalBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE; |
| 82 | + } |
| 83 | + if (is_int($key)) { |
| 84 | + $key = $type; |
| 85 | + } |
| 86 | + if (!isset($serviceMap[$key])) { |
| 87 | + $serviceMap[$key] = new Reference($type); |
| 88 | + } |
| 89 | + |
| 90 | + $subscriberMap[$key] = new ServiceClosureArgument(new TypedReference((string) $serviceMap[$key], $type, $optionalBehavior ?: ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)); |
| 91 | + unset($serviceMap[$key]); |
| 92 | + } |
| 93 | + |
| 94 | + if ($serviceMap = array_keys($serviceMap)) { |
| 95 | + $this->container->log($this, sprintf('Service keys "%s" do not exist in the map returned by %s::getSubscribedServices() for service "%s".', implode('", "', $serviceMap), $class, $this->currentId)); |
| 96 | + } |
| 97 | + |
| 98 | + $serviceLocator = $this->serviceLocator; |
| 99 | + $this->serviceLocator = 'container.'.$this->currentId.'.'.md5(serialize($value)); |
| 100 | + $this->container->register($this->serviceLocator, ServiceLocator::class) |
| 101 | + ->addArgument($subscriberMap) |
| 102 | + ->setPublic(false) |
| 103 | + ->setAutowired($value->isAutowired()) |
| 104 | + ->addTag('container.service_locator'); |
| 105 | + |
| 106 | + try { |
| 107 | + return parent::processValue($value); |
| 108 | + } finally { |
| 109 | + $this->serviceLocator = $serviceLocator; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments