|
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,41 @@ 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 implement interface "%s".', $id, $this->listenerTag, EventListenerInterface::class)); |
| 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'])) {
|
75 | 91 | $event['method'] = 'on'.preg_replace_callback([
|
76 | 92 | '/(?<=\b)[a-z]/i',
|
77 | 93 | '/[^a-z0-9]/i',
|
78 |
| - ], function ($matches) { return strtoupper($matches[0]); }, $event['event']); |
| 94 | + ], function ($matches) { return strtoupper($matches[0]); }, (false === $p = \strrpos($event['event'], '\\')) ? $event['event'] : \substr($event['event'], $p + 1)); |
79 | 95 | $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
|
80 | 96 |
|
81 |
| - if (null !== ($class = $container->getDefinition($id)->getClass()) && ($r = $container->getReflectionClass($class, false)) && !$r->hasMethod($event['method']) && $r->hasMethod('__invoke')) { |
| 97 | + if (null === $reflection) { |
| 98 | + $class = $container->getDefinition($id)->getClass(); |
| 99 | + if (!$reflection = $container->getReflectionClass($class)) { |
| 100 | + throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); |
| 101 | + } |
| 102 | + } |
| 103 | + if (!$reflection->hasMethod($event['method']) && $reflection->hasMethod('__invoke')) { |
82 | 104 | $event['method'] = '__invoke';
|
83 | 105 | }
|
84 | 106 | }
|
@@ -122,6 +144,40 @@ public function process(ContainerBuilder $container)
|
122 | 144 | ExtractingEventDispatcher::$aliases = [];
|
123 | 145 | }
|
124 | 146 | }
|
| 147 | + |
| 148 | + private function guessListenedClass(\ReflectionClass $handlerClass, string $serviceId): string |
| 149 | + { |
| 150 | + try { |
| 151 | + $method = $handlerClass->getMethod('__invoke'); |
| 152 | + } catch (\ReflectionException $e) { |
| 153 | + throw new \InvalidArgumentException(sprintf('Invalid EventListener "%s": class "%s" must have an "__invoke()" method.', $serviceId, $handlerClass->getName())); |
| 154 | + } |
| 155 | + |
| 156 | + $parameters = $method->getParameters(); |
| 157 | + if (1 > \count($parameters)) { |
| 158 | + 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())); |
| 159 | + } |
| 160 | + |
| 161 | + if (!$type = $parameters[0]->getType()) { |
| 162 | + 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())); |
| 163 | + } |
| 164 | + |
| 165 | + if ($type->isBuiltin()) { |
| 166 | + 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)); |
| 167 | + } |
| 168 | + |
| 169 | + return $parameters[0]->getType(); |
| 170 | + } |
| 171 | + |
| 172 | + private function registerListenerCall(ContainerBuilder $container, Definition $definition, string $listerId, string $eventName, string $method, int $priority) |
| 173 | + { |
| 174 | + $callable = [new ServiceClosureArgument(new Reference($listerId)), $method]; |
| 175 | + $definition->addMethodCall('addListener', [$eventName, $callable, $priority]); |
| 176 | + |
| 177 | + if (isset($this->hotPathEvents[$eventName])) { |
| 178 | + $container->getDefinition($listerId)->addTag($this->hotPathTagName); |
| 179 | + } |
| 180 | + } |
125 | 181 | }
|
126 | 182 |
|
127 | 183 | /**
|
|
0 commit comments