Skip to content

Commit a69f5dd

Browse files
committed
Add support for union types on AsEventListener
1 parent 342063d commit a69f5dd

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Allow using their name without added suffix when using `#[Target]` for custom services
99
* Deprecate `Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of `Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()`
1010
* Add `assertEmailAddressNotContains()` to the `MailerAssertionsTrait`
11+
* Add support for union types with `Symfony\Component\EventDispatcher\Attribute\AsEventListener`
1112

1213
7.3
1314
---

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

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

753753
$container->registerAttributeForAutoconfiguration(AsEventListener::class, static function (ChildDefinition $definition, AsEventListener $attribute, \ReflectionClass|\ReflectionMethod $reflector) {
754754
$tagAttributes = get_object_vars($attribute);
755-
if ($reflector instanceof \ReflectionMethod) {
756-
if (isset($tagAttributes['method'])) {
757-
throw new LogicException(\sprintf('AsEventListener attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
755+
756+
if (!$reflector instanceof \ReflectionMethod) {
757+
$definition->addTag('kernel.event_listener', $tagAttributes);
758+
759+
return;
760+
}
761+
762+
if (isset($tagAttributes['method'])) {
763+
throw new LogicException(\sprintf('AsEventListener attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
764+
}
765+
766+
$tagAttributes['method'] = $reflector->getName();
767+
768+
if (!$eventArg = $reflector->getParameters()[0] ?? null) {
769+
throw new LogicException(\sprintf('AsEventListener attribute requires the first argument of "%s::%s()" to be an event object.', $reflector->class, $reflector->name));
770+
}
771+
772+
$types = ($type = $eventArg->getType() instanceof \ReflectionUnionType ? $eventArg->getType()->getTypes() : [$eventArg->getType()]) ?: [];
773+
774+
foreach ($types as $type) {
775+
if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
776+
$tagAttributes['event'] = $type->getName();
777+
778+
$definition->addTag('kernel.event_listener', $tagAttributes);
758779
}
759-
$tagAttributes['method'] = $reflector->getName();
760780
}
761-
$definition->addTag('kernel.event_listener', $tagAttributes);
762781
});
763782
$container->registerAttributeForAutoconfiguration(AsController::class, static function (ChildDefinition $definition, AsController $attribute): void {
764783
$definition->addTag('controller.service_arguments');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy