|
14 | 14 | use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
|
15 | 15 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
16 | 16 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
| 17 | +use Symfony\Component\DependencyInjection\Definition; |
17 | 18 | use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
18 | 19 | use Symfony\Component\DependencyInjection\Reference;
|
| 20 | +use Symfony\Component\EventDispatcher\Event; |
19 | 21 | use Symfony\Component\EventDispatcher\EventDispatcher;
|
| 22 | +use Symfony\Component\EventDispatcher\EventListenerInterface; |
20 | 23 | use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
21 | 24 |
|
22 | 25 | /**
|
@@ -63,22 +66,45 @@ public function process(ContainerBuilder $container)
|
63 | 66 | $definition = $container->findDefinition($this->dispatcherService);
|
64 | 67 |
|
65 | 68 | foreach ($container->findTaggedServiceIds($this->listenerTag, true) as $id => $events) {
|
| 69 | + $reflection = null; |
66 | 70 | foreach ($events as $event) {
|
67 | 71 | $priority = isset($event['priority']) ? $event['priority'] : 0;
|
68 | 72 |
|
69 | 73 | if (!isset($event['event'])) {
|
70 |
| - throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag)); |
| 74 | + if (null === $reflection) { |
| 75 | + $class = $container->getDefinition($id)->getClass(); |
| 76 | + if (!$reflection = $container->getReflectionClass($class)) { |
| 77 | + throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); |
| 78 | + } |
| 79 | + } |
| 80 | + if (!$reflection->implementsInterface(EventListenerInterface::class)) { |
| 81 | + throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags or implements the EventListenerInterface interface .', $id, $this->listenerTag)); |
| 82 | + } |
| 83 | + |
| 84 | + $event['event'] = $this->guessListenedClass($reflection, $id); |
| 85 | + $event['method'] = '__invoke'; |
| 86 | + } else { |
| 87 | + $event['event'] = $aliases[$event['event']] ?? $event['event']; |
71 | 88 | }
|
72 |
| - $event['event'] = $aliases[$event['event']] ?? $event['event']; |
73 | 89 |
|
74 | 90 | if (!isset($event['method'])) {
|
| 91 | + $eventName = $event['event']; |
| 92 | + if ($eventR = $container->getReflectionClass($event['event'])) { |
| 93 | + $eventName = $eventR->getShortName(); |
| 94 | + } |
75 | 95 | $event['method'] = 'on'.preg_replace_callback([
|
76 | 96 | '/(?<=\b)[a-z]/i',
|
77 | 97 | '/[^a-z0-9]/i',
|
78 |
| - ], function ($matches) { return strtoupper($matches[0]); }, $event['event']); |
| 98 | + ], function ($matches) { return strtoupper($matches[0]); }, $eventName); |
79 | 99 | $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
|
80 | 100 |
|
81 |
| - if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method']) && $r->hasMethod('__invoke')) { |
| 101 | + if (null === $reflection) { |
| 102 | + $class = $container->getDefinition($id)->getClass(); |
| 103 | + if (!$reflection = $container->getReflectionClass($class)) { |
| 104 | + throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); |
| 105 | + } |
| 106 | + } |
| 107 | + if (!$reflection->hasMethod($event['method']) && $reflection->hasMethod('__invoke')) { |
82 | 108 | $event['method'] = '__invoke';
|
83 | 109 | }
|
84 | 110 | }
|
@@ -115,13 +141,47 @@ public function process(ContainerBuilder $container)
|
115 | 141 | $definition->addMethodCall('addListener', $args);
|
116 | 142 |
|
117 | 143 | if (isset($this->hotPathEvents[$args[0]])) {
|
118 |
| - $container->getDefinition($id)->addTag('container.hot_path'); |
| 144 | + $container->getDefinition($id)->addTag($this->hotPathTagName); |
119 | 145 | }
|
120 | 146 | }
|
121 | 147 | $extractingDispatcher->listeners = [];
|
122 | 148 | ExtractingEventDispatcher::$aliases = [];
|
123 | 149 | }
|
124 | 150 | }
|
| 151 | + |
| 152 | + private function guessListenedClass(\ReflectionClass $handlerClass, string $serviceId): string |
| 153 | + { |
| 154 | + try { |
| 155 | + $method = $handlerClass->getMethod('__invoke'); |
| 156 | + } catch (\ReflectionException $e) { |
| 157 | + throw new \InvalidArgumentException(sprintf('Invalid EventListener "%s": class "%s" must have an "__invoke()" method.', $serviceId, $handlerClass->getName())); |
| 158 | + } |
| 159 | + |
| 160 | + $parameters = $method->getParameters(); |
| 161 | + if (1 > \count($parameters)) { |
| 162 | + throw new \InvalidArgumentException(sprintf('Invalid EventListener "%s": method "%s::__invoke()" must have, at least, one argument corresponding to the event it handles.', $serviceId, $handlerClass->getName())); |
| 163 | + } |
| 164 | + |
| 165 | + if (!$type = $parameters[0]->getType()) { |
| 166 | + throw new \InvalidArgumentException(sprintf('Invalid EventListener "%s": argument "$%s" of method "%s::__invoke()" must have a type-hint corresponding to the event class it handles.', $serviceId, $parameters[0]->getName(), $handlerClass->getName())); |
| 167 | + } |
| 168 | + |
| 169 | + if ($type->isBuiltin()) { |
| 170 | + throw new \InvalidArgumentException(sprintf('Invalid EventListener "%s": type-hint of argument "$%s" in method "%s::__invoke()" must be a class, "%s" given.', $serviceId, $parameters[0]->getName(), $handlerClass->getName(), $type)); |
| 171 | + } |
| 172 | + |
| 173 | + return $parameters[0]->getType(); |
| 174 | + } |
| 175 | + |
| 176 | + private function registerListenerCall(ContainerBuilder $container, Definition $definition, string $listerId, string $eventName, string $method, int $priority) |
| 177 | + { |
| 178 | + $callable = [new ServiceClosureArgument(new Reference($listerId)), $method]; |
| 179 | + $definition->addMethodCall('addListener', [$eventName, $callable, $priority]); |
| 180 | + |
| 181 | + if (isset($this->hotPathEvents[$eventName])) { |
| 182 | + $container->getDefinition($listerId)->addTag($this->hotPathTagName); |
| 183 | + } |
| 184 | + } |
125 | 185 | }
|
126 | 186 |
|
127 | 187 | /**
|
|
0 commit comments