Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ CHANGELOG
* Add support for configuring workflow places with glob patterns matching consts/backed enums
* Add support for configuring the `CachingHttpClient`
* Add support for weighted transitions in workflows
* Add support for union types with `Symfony\Component\EventDispatcher\Attribute\AsEventListener`

7.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,13 +774,32 @@ public function load(array $configs, ContainerBuilder $container): void

$container->registerAttributeForAutoconfiguration(AsEventListener::class, static function (ChildDefinition $definition, AsEventListener $attribute, \ReflectionClass|\ReflectionMethod $reflector) {
$tagAttributes = get_object_vars($attribute);
if ($reflector instanceof \ReflectionMethod) {
if (isset($tagAttributes['method'])) {
throw new LogicException(\sprintf('AsEventListener attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));

if (!$reflector instanceof \ReflectionMethod) {
$definition->addTag('kernel.event_listener', $tagAttributes);

return;
}

if (isset($tagAttributes['method'])) {
throw new LogicException(\sprintf('AsEventListener attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
}

$tagAttributes['method'] = $reflector->getName();

if (!$eventArg = $reflector->getParameters()[0] ?? null) {
throw new LogicException(\sprintf('AsEventListener attribute requires the first argument of "%s::%s()" to be an event object.', $reflector->class, $reflector->name));
}

$types = ($type = $eventArg->getType() instanceof \ReflectionUnionType ? $eventArg->getType()->getTypes() : [$eventArg->getType()]) ?: [];

foreach ($types as $type) {
if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
$tagAttributes['event'] = $type->getName();

$definition->addTag('kernel.event_listener', $tagAttributes);
}
$tagAttributes['method'] = $reflector->getName();
}
$definition->addTag('kernel.event_listener', $tagAttributes);
});
$container->registerAttributeForAutoconfiguration(AsController::class, static function (ChildDefinition $definition, AsController $attribute): void {
$definition->addTag('controller.service_arguments');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Tests\Fixtures\CustomEvent;
use Symfony\Component\EventDispatcher\Tests\Fixtures\DummyEvent;
use Symfony\Component\EventDispatcher\Tests\Fixtures\TaggedInvokableListener;
use Symfony\Component\EventDispatcher\Tests\Fixtures\TaggedMultiListener;
use Symfony\Component\EventDispatcher\Tests\Fixtures\TaggedUnionTypeListener;

class RegisterListenersPassTest extends TestCase
{
Expand Down Expand Up @@ -356,6 +358,70 @@ static function (ChildDefinition $definition, AsEventListener $attribute, \Refle
$this->assertEquals($expectedCalls, $definition->getMethodCalls());
}

public function testTaggedMethodUnionTypeEventListener()
{
$container = new ContainerBuilder();
$container->registerAttributeForAutoconfiguration(AsEventListener::class,
static function (ChildDefinition $definition, AsEventListener $attribute, \ReflectionClass|\ReflectionMethod $reflector) {
$tagAttributes = get_object_vars($attribute);

if (!$reflector instanceof \ReflectionMethod) {
$definition->addTag('kernel.event_listener', $tagAttributes);

return;
}

if (isset($tagAttributes['method'])) {
throw new \LogicException(\sprintf('AsEventListener attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
}

$tagAttributes['method'] = $reflector->getName();

if (!$eventArg = $reflector->getParameters()[0] ?? null) {
throw new \LogicException(\sprintf('AsEventListener attribute requires the first argument of "%s::%s()" to be an event object.', $reflector->class, $reflector->name));
}

$types = ($type = $eventArg->getType() instanceof \ReflectionUnionType ? $eventArg->getType()->getTypes() : [$eventArg->getType()]) ?: [];

foreach ($types as $type) {
if ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) {
$tagAttributes['event'] = $type->getName();

$definition->addTag('kernel.event_listener', $tagAttributes);
}
}
});

$container->register('foo', TaggedUnionTypeListener::class)->setAutoconfigured(true);
$container->register('event_dispatcher', \stdClass::class);

(new AttributeAutoconfigurationPass())->process($container);
(new ResolveInstanceofConditionalsPass())->process($container);
(new RegisterListenersPass())->process($container);

$definition = $container->getDefinition('event_dispatcher');
$expectedCalls = [
[
'addListener',
[
CustomEvent::class,
[new ServiceClosureArgument(new Reference('foo')), 'onUnionEvent'],
0,
],
],
[
'addListener',
[
DummyEvent::class,
[new ServiceClosureArgument(new Reference('foo')), 'onUnionEvent'],
0,
],
],
];

$this->assertEquals($expectedCalls, $definition->getMethodCalls());
}

public function testAliasedEventListener()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\EventDispatcher\Tests\Fixtures;

final class DummyEvent
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\EventDispatcher\Tests\Fixtures;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

final class TaggedUnionTypeListener
{
#[AsEventListener]
public function onUnionEvent(CustomEvent|DummyEvent $event): void
{
}
}
Loading