Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsEventListener attribute #1561

Merged
merged 1 commit into from
Oct 16, 2022
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
19 changes: 19 additions & 0 deletions Attribute/AsEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Attribute;

use Attribute;

/**
* Service tag to autoconfigure event listeners.
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class AsEventListener
{
public function __construct(
public string $event,
public ?int $priority = null,
public ?string $connection = null,
) {
}
}
8 changes: 8 additions & 0 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection;

use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEventListener;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsMiddleware;
use Doctrine\Bundle\DoctrineBundle\CacheWarmer\DoctrineMetadataCacheWarmer;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\ImportDoctrineCommand;
Expand Down Expand Up @@ -652,6 +653,13 @@ protected function ormLoad(array $config, ContainerBuilder $container)
'entity' => $attribute->entity,
]);
});
$container->registerAttributeForAutoconfiguration(AsEventListener::class, static function (ChildDefinition $definition, AsEventListener $attribute) {
$definition->addTag('doctrine.event_listener', [
'event' => $attribute->event,
'priority' => $attribute->priority,
'connection' => $attribute->connection,
]);
});
}

/** @see DoctrineBundle::boot() */
Expand Down
39 changes: 39 additions & 0 deletions Tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use Closure;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEventListener;
use Doctrine\Bundle\DoctrineBundle\CacheWarmer\DoctrineMetadataCacheWarmer;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheCompatibilityPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Doctrine\Bundle\DoctrineBundle\Tests\Builder\BundleConfigurationBuilder;
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\Php8EntityListener;
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\Php8EventListener;
use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
Expand All @@ -29,6 +31,7 @@
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
Expand Down Expand Up @@ -1166,6 +1169,42 @@ public function testAsEntityListenerAttribute()
$this->assertSame([$expected], $definition->getTag('doctrine.orm.entity_listener'));
}

/**
* @requires PHP 8
*/
public function testAsEventListenerAttribute()
{
if (! method_exists(ContainerBuilder::class, 'getAutoconfiguredAttributes')) {
$this->markTestSkipped('symfony/dependency-injection 5.3.0 needed');
}

$container = $this->getContainer();
$extension = new DoctrineExtension();

$config = BundleConfigurationBuilder::createBuilder()
->addBaseConnection()
->addBaseEntityManager()
->build();

$extension->load([$config], $container);

$attributes = $container->getAutoconfiguredAttributes();
$this->assertInstanceOf(Closure::class, $attributes[AsEventListener::class]);

$reflector = new ReflectionClass(Php8EventListener::class);
$definition = new ChildDefinition('');
$attribute = $reflector->getAttributes(AsEventListener::class)[0]->newInstance();

$attributes[AsEventListener::class]($definition, $attribute);

$expected = [
'event' => Events::postFlush,
'priority' => null,
'connection' => null,
];
$this->assertSame([$expected], $definition->getTag('doctrine.event_listener'));
}

/** @return bool[][] */
public function provideRegistrationsWithoutMiddlewares(): array
{
Expand Down
14 changes: 14 additions & 0 deletions Tests/DependencyInjection/Fixtures/Php8EventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures;

use Doctrine\Bundle\DoctrineBundle\Attribute\AsEventListener;
use Doctrine\ORM\Events;

#[AsEventListener(Events::postFlush)]
final class Php8EventListener
{
public function postFlush(): void
{
}
}