Skip to content

Commit dc0913f

Browse files
committed
[EventDispatcher][FrameworkBundle] Rework union types on #[AsEventListener]
1 parent 280d4bb commit dc0913f

File tree

1 file changed

+5
-24
lines changed

1 file changed

+5
-24
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -779,32 +779,13 @@ public function load(array $configs, ContainerBuilder $container): void
779779

780780
$container->registerAttributeForAutoconfiguration(AsEventListener::class, static function (ChildDefinition $definition, AsEventListener $attribute, \ReflectionClass|\ReflectionMethod $reflector) {
781781
$tagAttributes = get_object_vars($attribute);
782-
783-
if (!$reflector instanceof \ReflectionMethod) {
784-
$definition->addTag('kernel.event_listener', $tagAttributes);
785-
786-
return;
787-
}
788-
789-
if (isset($tagAttributes['method'])) {
790-
throw new LogicException(\sprintf('AsEventListener attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
791-
}
792-
793-
$tagAttributes['method'] = $reflector->getName();
794-
795-
if (!$eventArg = $reflector->getParameters()[0] ?? null) {
796-
throw new LogicException(\sprintf('AsEventListener attribute requires the first argument of "%s::%s()" to be an event object.', $reflector->class, $reflector->name));
797-
}
798-
799-
$types = ($type = $eventArg->getType() instanceof \ReflectionUnionType ? $eventArg->getType()->getTypes() : [$eventArg->getType()]) ?: [];
800-
801-
foreach ($types as $type) {
802-
if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
803-
$tagAttributes['event'] = $type->getName();
804-
805-
$definition->addTag('kernel.event_listener', $tagAttributes);
782+
if ($reflector instanceof \ReflectionMethod) {
783+
if (isset($tagAttributes['method'])) {
784+
throw new LogicException(\sprintf('AsEventListener attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
806785
}
786+
$tagAttributes['method'] = $reflector->getName();
807787
}
788+
$definition->addTag('kernel.event_listener', $tagAttributes);
808789
});
809790
$container->registerAttributeForAutoconfiguration(AsController::class, static function (ChildDefinition $definition, AsController $attribute): void {
810791
$definition->addTag('controller.service_arguments');

0 commit comments

Comments
 (0)