Skip to content

Commit

Permalink
Add AsEventListener attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse committed Nov 21, 2022
1 parent a75e50d commit 7d78785
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
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 entity 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
35 changes: 35 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 Down Expand Up @@ -1166,6 +1168,39 @@ 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' => 'postPersist',
'connection' => null,
];
$this->assertSame([$expected], $definition->getTag('doctrine.event_listener'));
}

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

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

use Doctrine\Bundle\DoctrineBundle\Attribute\AsEventListener;

#[AsEventListener('postPersist')]
final class Php8EventListener
{
public function __invoke(): void
{
}

public function postPersist(): void
{
}
}

0 comments on commit 7d78785

Please sign in to comment.