Skip to content

Commit

Permalink
Merge pull request #11272 from greg0ire/sa-attach-entity-listener
Browse files Browse the repository at this point in the history
Improve static analysis on AttachEntityListenersListener
  • Loading branch information
greg0ire authored Feb 20, 2024
2 parents 0c4aac5 + 4bd574d commit b1f553e
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Tools/AttachEntityListenersListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@
namespace Doctrine\ORM\Tools;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder;

use function assert;
use function ltrim;

/**
* Mechanism to programmatically attach entity listeners.
*/
class AttachEntityListenersListener
{
/** @var mixed[][] */
/**
* @var array<class-string, list<array{
* event: Events::*|null,
* class: class-string,
* method: string|null,
* }>>
*/
private array $entityListeners = [];

/**
* Adds an entity listener for a specific entity.
*
* @param string $entityClass The entity to attach the listener.
* @param string $listenerClass The listener class.
* @param string|null $eventName The entity lifecycle event.
* @param string|null $listenerCallback The listener callback method or NULL to use $eventName.
* @param class-string $entityClass The entity to attach the listener.
* @param class-string $listenerClass The listener class.
* @param Events::*|null $eventName The entity lifecycle event.
* @param non-falsy-string|null $listenerCallback The listener callback method or NULL to use $eventName.
*/
public function addEntityListener(
string $entityClass,
Expand All @@ -34,7 +42,7 @@ public function addEntityListener(
$this->entityListeners[ltrim($entityClass, '\\')][] = [
'event' => $eventName,
'class' => $listenerClass,
'method' => $listenerCallback ?: $eventName,
'method' => $listenerCallback ?? $eventName,
];
}

Expand All @@ -53,6 +61,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void
if ($listener['event'] === null) {
EntityListenerBuilder::bindEntityListener($metadata, $listener['class']);
} else {
assert($listener['method'] !== null);
$metadata->addEntityListener($listener['event'], $listener['class'], $listener['method']);
}
}
Expand Down

0 comments on commit b1f553e

Please sign in to comment.