RegisterServiceSubscribersPass.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
  16. use Symfony\Component\DependencyInjection\TypedReference;
  17. /**
  18. * Compiler pass to register tagged services that require a service locator.
  19. *
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. */
  22. class RegisterServiceSubscribersPass extends AbstractRecursivePass
  23. {
  24. protected function processValue($value, $isRoot = false)
  25. {
  26. if (!$value instanceof Definition || $value->isAbstract() || $value->isSynthetic() || !$value->hasTag('container.service_subscriber')) {
  27. return parent::processValue($value, $isRoot);
  28. }
  29. $serviceMap = array();
  30. $autowire = $value->isAutowired();
  31. foreach ($value->getTag('container.service_subscriber') as $attributes) {
  32. if (!$attributes) {
  33. $autowire = true;
  34. continue;
  35. }
  36. ksort($attributes);
  37. if (array() !== array_diff(array_keys($attributes), array('id', 'key'))) {
  38. 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));
  39. }
  40. if (!array_key_exists('id', $attributes)) {
  41. throw new InvalidArgumentException(sprintf('Missing "id" attribute on "container.service_subscriber" tag with key="%s" for service "%s".', $attributes['key'], $this->currentId));
  42. }
  43. if (!array_key_exists('key', $attributes)) {
  44. $attributes['key'] = $attributes['id'];
  45. }
  46. if (isset($serviceMap[$attributes['key']])) {
  47. continue;
  48. }
  49. $serviceMap[$attributes['key']] = new Reference($attributes['id']);
  50. }
  51. $class = $value->getClass();
  52. if (!is_subclass_of($class, ServiceSubscriberInterface::class)) {
  53. if (!class_exists($class, false)) {
  54. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $this->currentId));
  55. }
  56. throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $this->currentId, ServiceSubscriberInterface::class));
  57. }
  58. $this->container->addObjectResource($class);
  59. $subscriberMap = array();
  60. $declaringClass = (new \ReflectionMethod($class, 'getSubscribedServices'))->class;
  61. foreach ($class::getSubscribedServices() as $key => $type) {
  62. 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)) {
  63. 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)));
  64. }
  65. if ($optionalBehavior = '?' === $type[0]) {
  66. $type = substr($type, 1);
  67. $optionalBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  68. }
  69. if (is_int($key)) {
  70. $key = $type;
  71. }
  72. if (!isset($serviceMap[$key])) {
  73. if (!$autowire) {
  74. 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));
  75. }
  76. $serviceMap[$key] = new Reference($type);
  77. }
  78. $subscriberMap[$key] = new TypedReference((string) $serviceMap[$key], $type, $declaringClass, $optionalBehavior ?: ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
  79. unset($serviceMap[$key]);
  80. }
  81. if ($serviceMap = array_keys($serviceMap)) {
  82. $message = sprintf(1 < count($serviceMap) ? 'keys "%s" do' : 'key "%s" does', str_replace('%', '%%', implode('", "', $serviceMap)));
  83. throw new InvalidArgumentException(sprintf('Service %s not exist in the map returned by "%s::getSubscribedServices()" for service "%s".', $message, $class, $this->currentId));
  84. }
  85. $value->addTag('container.service_subscriber.locator', array('id' => (string) ServiceLocatorTagPass::register($this->container, $subscriberMap)));
  86. return parent::processValue($value);
  87. }
  88. }