/src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php

https://github.com/pulzarraider/symfony · PHP · 152 lines · 109 code · 28 blank · 15 comment · 14 complexity · b2c1317241fd2097968eeaf781b256f8 MD5 · raw file

  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\EventDispatcher\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\EventDispatcher\EventDispatcher;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. /**
  19. * Compiler pass to register tagged services for an event dispatcher.
  20. */
  21. class RegisterListenersPass implements CompilerPassInterface
  22. {
  23. protected $dispatcherService;
  24. protected $listenerTag;
  25. protected $subscriberTag;
  26. protected $eventAliasesParameter;
  27. private $hotPathEvents = [];
  28. private $hotPathTagName;
  29. public function __construct(string $dispatcherService = 'event_dispatcher', string $listenerTag = 'kernel.event_listener', string $subscriberTag = 'kernel.event_subscriber', string $eventAliasesParameter = 'event_dispatcher.event_aliases')
  30. {
  31. $this->dispatcherService = $dispatcherService;
  32. $this->listenerTag = $listenerTag;
  33. $this->subscriberTag = $subscriberTag;
  34. $this->eventAliasesParameter = $eventAliasesParameter;
  35. }
  36. public function setHotPathEvents(array $hotPathEvents, $tagName = 'container.hot_path')
  37. {
  38. $this->hotPathEvents = array_flip($hotPathEvents);
  39. $this->hotPathTagName = $tagName;
  40. return $this;
  41. }
  42. public function process(ContainerBuilder $container)
  43. {
  44. if (!$container->hasDefinition($this->dispatcherService) && !$container->hasAlias($this->dispatcherService)) {
  45. return;
  46. }
  47. if ($container->hasParameter($this->eventAliasesParameter)) {
  48. $aliases = $container->getParameter($this->eventAliasesParameter);
  49. $container->getParameterBag()->remove($this->eventAliasesParameter);
  50. } else {
  51. $aliases = [];
  52. }
  53. $definition = $container->findDefinition($this->dispatcherService);
  54. foreach ($container->findTaggedServiceIds($this->listenerTag, true) as $id => $events) {
  55. foreach ($events as $event) {
  56. $priority = isset($event['priority']) ? $event['priority'] : 0;
  57. if (!isset($event['event'])) {
  58. throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
  59. }
  60. $event['event'] = $aliases[$event['event']] ?? $event['event'];
  61. if (!isset($event['method'])) {
  62. $event['method'] = 'on'.preg_replace_callback([
  63. '/(?<=\b)[a-z]/i',
  64. '/[^a-z0-9]/i',
  65. ], function ($matches) { return strtoupper($matches[0]); }, $event['event']);
  66. $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
  67. if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method']) && $r->hasMethod('__invoke')) {
  68. $event['method'] = '__invoke';
  69. }
  70. }
  71. $definition->addMethodCall('addListener', [$event['event'], [new ServiceClosureArgument(new Reference($id)), $event['method']], $priority]);
  72. if (isset($this->hotPathEvents[$event['event']])) {
  73. $container->getDefinition($id)->addTag($this->hotPathTagName);
  74. }
  75. }
  76. }
  77. $extractingDispatcher = new ExtractingEventDispatcher();
  78. foreach ($container->findTaggedServiceIds($this->subscriberTag, true) as $id => $attributes) {
  79. $def = $container->getDefinition($id);
  80. // We must assume that the class value has been correctly filled, even if the service is created by a factory
  81. $class = $def->getClass();
  82. if (!$r = $container->getReflectionClass($class)) {
  83. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  84. }
  85. if (!$r->isSubclassOf(EventSubscriberInterface::class)) {
  86. throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, EventSubscriberInterface::class));
  87. }
  88. $class = $r->name;
  89. ExtractingEventDispatcher::$aliases = $aliases;
  90. ExtractingEventDispatcher::$subscriber = $class;
  91. $extractingDispatcher->addSubscriber($extractingDispatcher);
  92. foreach ($extractingDispatcher->listeners as $args) {
  93. $args[1] = [new ServiceClosureArgument(new Reference($id)), $args[1]];
  94. $definition->addMethodCall('addListener', $args);
  95. if (isset($this->hotPathEvents[$args[0]])) {
  96. $container->getDefinition($id)->addTag($this->hotPathTagName);
  97. }
  98. }
  99. $extractingDispatcher->listeners = [];
  100. ExtractingEventDispatcher::$aliases = [];
  101. }
  102. }
  103. }
  104. /**
  105. * @internal
  106. */
  107. class ExtractingEventDispatcher extends EventDispatcher implements EventSubscriberInterface
  108. {
  109. public $listeners = [];
  110. public static $aliases = [];
  111. public static $subscriber;
  112. public function addListener($eventName, $listener, $priority = 0)
  113. {
  114. $this->listeners[] = [$eventName, $listener[1], $priority];
  115. }
  116. public static function getSubscribedEvents()
  117. {
  118. $events = [];
  119. foreach ([self::$subscriber, 'getSubscribedEvents']() as $eventName => $params) {
  120. $events[self::$aliases[$eventName] ?? $eventName] = $params;
  121. }
  122. return $events;
  123. }
  124. }