Skip to content

Commit 4907252

Browse files
committed
Allows Symfony EventDispatcherInterface instead of EventManager
1 parent 31a3c5e commit 4907252

File tree

8 files changed

+94
-31
lines changed

8 files changed

+94
-31
lines changed

lib/Doctrine/ODM/MongoDB/DocumentManager.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
use Doctrine\Persistence\ObjectManager;
2424
use InvalidArgumentException;
2525
use Jean85\PrettyVersions;
26+
use LogicException;
2627
use MongoDB\Client;
2728
use MongoDB\Collection;
2829
use MongoDB\Database;
2930
use MongoDB\Driver\ReadPreference;
3031
use MongoDB\GridFS\Bucket;
3132
use ProxyManager\Proxy\GhostObjectInterface;
3233
use RuntimeException;
34+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
3335
use Throwable;
3436

3537
use function array_search;
@@ -79,7 +81,7 @@ class DocumentManager implements ObjectManager
7981
/**
8082
* The event manager that is the central point of the event system.
8183
*/
82-
private EventManager $eventManager;
84+
private EventManager|EventDispatcherInterface $eventManager;
8385

8486
/**
8587
* The Hydrator factory instance.
@@ -141,7 +143,7 @@ class DocumentManager implements ObjectManager
141143
* Creates a new Document that operates on the given Mongo connection
142144
* and uses the given Configuration.
143145
*/
144-
protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null)
146+
protected function __construct(?Client $client = null, ?Configuration $config = null, EventManager|EventDispatcherInterface|null $eventManager = null)
145147
{
146148
$this->config = $config ?: new Configuration();
147149
$this->eventManager = $eventManager ?: new EventManager();
@@ -197,16 +199,27 @@ public function getProxyFactory(): ProxyFactory
197199
* Creates a new Document that operates on the given Mongo connection
198200
* and uses the given Configuration.
199201
*/
200-
public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null): DocumentManager
202+
public static function create(?Client $client = null, ?Configuration $config = null, EventManager|EventDispatcherInterface|null $eventManager = null): DocumentManager
201203
{
202204
return new static($client, $config, $eventManager);
203205
}
204206

207+
public function getEventDispatcher(): EventManager|EventDispatcherInterface
208+
{
209+
return $this->eventManager;
210+
}
211+
205212
/**
206213
* Gets the EventManager used by the DocumentManager.
214+
*
215+
* @deprecated Use getEventDispatcher() instead
207216
*/
208217
public function getEventManager(): EventManager
209218
{
219+
if (! $this->eventManager instanceof EventManager) {
220+
throw new LogicException('Use getEventDispatcher() instead of getEventManager()');
221+
}
222+
210223
return $this->eventManager;
211224
}
212225

lib/Doctrine/ODM/MongoDB/Event/HasDocumentManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Doctrine\ODM\MongoDB\Event;
46

57
use Doctrine\ODM\MongoDB\DocumentManager;
@@ -14,4 +16,4 @@ public function getDocumentManager(): DocumentManager
1416
{
1517
return $this->documentManager;
1618
}
17-
}
19+
}

lib/Doctrine/ODM/MongoDB/Event/LoadClassMetadataEvent.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Doctrine\ODM\MongoDB\DocumentManager;
88
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
9-
use Doctrine\Persistence\ObjectManager;
109
use Symfony\Contracts\EventDispatcher\Event;
1110

1211
/**

lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\ODM\MongoDB\Types\Type;
1515
use Doctrine\ODM\MongoDB\UnitOfWork;
1616
use ProxyManager\Proxy\GhostObjectInterface;
17+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
1718

1819
use function array_key_exists;
1920
use function chmod;
@@ -49,7 +50,7 @@ final class HydratorFactory
4950
/**
5051
* The EventManager associated with this Hydrator
5152
*/
52-
private EventManager $evm;
53+
private EventManager|EventDispatcherInterface $evm;
5354

5455
/**
5556
* Which algorithm to use to automatically (re)generate hydrator classes.
@@ -74,7 +75,7 @@ final class HydratorFactory
7475
private array $hydrators = [];
7576

7677
/** @throws HydratorException */
77-
public function __construct(DocumentManager $dm, EventManager $evm, ?string $hydratorDir, ?string $hydratorNs, int $autoGenerate)
78+
public function __construct(DocumentManager $dm, EventManager|EventDispatcherInterface $evm, ?string $hydratorDir, ?string $hydratorNs, int $autoGenerate)
7879
{
7980
if (! $hydratorDir) {
8081
throw HydratorException::hydratorDirectoryRequired();
@@ -433,7 +434,12 @@ public function hydrate(object $document, array $data, array $hints = []): array
433434
$metadata->invokeLifecycleCallbacks(Events::preLoad, $document, $args);
434435
}
435436

436-
$this->evm->dispatchEvent(Events::preLoad, new PreLoadEventArgs($document, $this->dm, $data));
437+
$eventArgs = new PreLoadEventArgs($document, $this->dm, $data);
438+
if ($this->evm instanceof EventDispatcherInterface) {
439+
$this->evm->dispatch($eventArgs, Events::preLoad);
440+
} else {
441+
$this->evm->dispatchEvent(Events::preLoad, $eventArgs);
442+
}
437443

438444
// alsoLoadMethods may transform the document before hydration
439445
if (! empty($metadata->alsoLoadMethods)) {
@@ -470,8 +476,11 @@ public function hydrate(object $document, array $data, array $hints = []): array
470476
$metadata->invokeLifecycleCallbacks(Events::postLoad, $document, [new LifecycleEventArgs($document, $this->dm)]);
471477
}
472478

473-
$this->evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($document, $this->dm));
474-
475-
return $data;
479+
$eventArgs = new LifecycleEventArgs($document, $this->dm);
480+
if ($this->evm instanceof EventDispatcherInterface) {
481+
$this->evm->dispatch($eventArgs, Events::postLoad);
482+
} else {
483+
$this->evm->dispatchEvent(Events::postLoad, $eventArgs);
484+
}
476485
}
477486
}

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
2222
use Doctrine\Persistence\Mapping\ReflectionService;
2323
use ReflectionException;
24+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2425

2526
use function assert;
2627
use function get_class_methods;
@@ -52,8 +53,8 @@ final class ClassMetadataFactory extends AbstractClassMetadataFactory implements
5253
/** @var MappingDriver The used metadata driver. */
5354
private MappingDriver $driver;
5455

55-
/** @var EventManager The event manager instance */
56-
private EventManager $evm;
56+
/** @var EventManager|EventDispatcherInterface The event manager instance */
57+
private EventManager|EventDispatcherInterface $evm;
5758

5859
public function setDocumentManager(DocumentManager $dm): void
5960
{
@@ -90,7 +91,11 @@ protected function onNotFoundMetadata($className)
9091

9192
$eventArgs = new OnClassMetadataNotFoundEventArgs($className, $this->dm);
9293

93-
$this->evm->dispatchEvent(Events::onClassMetadataNotFound, $eventArgs);
94+
if ($this->evm instanceof EventDispatcherInterface) {
95+
$this->evm->dispatch($eventArgs, Events::onClassMetadataNotFound);
96+
} else {
97+
$this->evm->dispatchEvent(Events::onClassMetadataNotFound, $eventArgs);
98+
}
9499

95100
return $eventArgs->getFoundMetadata();
96101
}
@@ -195,10 +200,12 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
195200

196201
$class->setParentClasses($nonSuperclassParents);
197202

198-
$this->evm->dispatchEvent(
199-
Events::loadClassMetadata,
200-
new LoadClassMetadataEventArgs($class, $this->dm),
201-
);
203+
$eventArgs = new LoadClassMetadataEventArgs($class, $this->dm);
204+
if ($this->evm instanceof EventDispatcherInterface) {
205+
$this->evm->dispatch($eventArgs, Events::loadClassMetadata);
206+
} else {
207+
$this->evm->dispatchEvent(Events::loadClassMetadata, $eventArgs);
208+
}
202209

203210
// phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
204211
if ($class->isChangeTrackingNotify()) {

lib/Doctrine/ODM/MongoDB/Proxy/Factory/StaticProxyFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class StaticProxyFactory implements ProxyFactory
3232
public function __construct(DocumentManager $documentManager)
3333
{
3434
$this->uow = $documentManager->getUnitOfWork();
35-
$this->lifecycleEventManager = new LifecycleEventManager($documentManager, $this->uow, $documentManager->getEventManager());
35+
$this->lifecycleEventManager = new LifecycleEventManager($documentManager, $this->uow, $documentManager->getEventDispatcher());
3636
$this->proxyFactory = $documentManager->getConfiguration()->buildGhostObjectFactory();
3737
}
3838

lib/Doctrine/ODM/MongoDB/UnitOfWork.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use MongoDB\Driver\WriteConcern;
3030
use ProxyManager\Proxy\GhostObjectInterface;
3131
use ReflectionProperty;
32+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
3233
use Throwable;
3334
use UnexpectedValueException;
3435

@@ -236,7 +237,7 @@ final class UnitOfWork implements PropertyChangedListener
236237
/**
237238
* The EventManager used for dispatching events.
238239
*/
239-
private EventManager $evm;
240+
private EventManager|EventDispatcherInterface $evm;
240241

241242
/**
242243
* Additional documents that are scheduled for removal.
@@ -292,7 +293,7 @@ final class UnitOfWork implements PropertyChangedListener
292293
/**
293294
* Initializes a new UnitOfWork instance, bound to the given DocumentManager.
294295
*/
295-
public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory)
296+
public function __construct(DocumentManager $dm, EventManager|EventDispatcherInterface $evm, HydratorFactory $hydratorFactory)
296297
{
297298
$this->dm = $dm;
298299
$this->evm = $evm;
@@ -425,7 +426,12 @@ public function commit(array $options = []): void
425426
}
426427

427428
// Raise preFlush
428-
$this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm));
429+
$eventArgs = new Event\PreFlushEventArgs($this->dm);
430+
if ($this->evm instanceof EventDispatcherInterface) {
431+
$this->evm->dispatch($eventArgs, Events::preFlush);
432+
} else {
433+
$this->evm->dispatchEvent(Events::preFlush, $eventArgs);
434+
}
429435

430436
// Compute changes done since last commit.
431437
$this->computeChangeSets();
@@ -454,7 +460,12 @@ public function commit(array $options = []): void
454460
}
455461
}
456462

457-
$this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm));
463+
$eventArgs = new Event\OnFlushEventArgs($this->dm);
464+
if ($this->evm instanceof EventDispatcherInterface) {
465+
$this->evm->dispatch($eventArgs, Events::onFlush);
466+
} else {
467+
$this->evm->dispatchEvent(Events::onFlush, $eventArgs);
468+
}
458469

459470
if ($this->useTransaction($options)) {
460471
$session = $this->dm->getClient()->startSession();
@@ -473,7 +484,12 @@ function (Session $session) use ($options): void {
473484
}
474485

475486
// Raise postFlush
476-
$this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm));
487+
$eventArgs = new Event\PostFlushEventArgs($this->dm);
488+
if ($this->evm instanceof EventDispatcherInterface) {
489+
$this->evm->dispatch($eventArgs, Events::postFlush);
490+
} else {
491+
$this->evm->dispatchEvent(Events::postFlush, $eventArgs);
492+
}
477493

478494
// Clear up
479495
foreach ($this->visitedCollections as $collections) {
@@ -2430,7 +2446,11 @@ public function clear(?string $documentName = null): void
24302446
$event = new Event\OnClearEventArgs($this->dm, $documentName);
24312447
}
24322448

2433-
$this->evm->dispatchEvent(Events::onClear, $event);
2449+
if ($this->evm instanceof EventDispatcherInterface) {
2450+
$this->evm->dispatch($event, Events::onClear);
2451+
} else {
2452+
$this->evm->dispatchEvent(Events::onClear, $event);
2453+
}
24342454
}
24352455

24362456
/**

lib/Doctrine/ODM/MongoDB/Utility/LifecycleEventManager.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface;
1818
use Doctrine\ODM\MongoDB\UnitOfWork;
1919
use MongoDB\Driver\Session;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2021

2122
use function spl_object_hash;
2223

@@ -30,7 +31,7 @@ final class LifecycleEventManager
3031
/** @var array<string, array<string, true>> */
3132
private array $transactionalEvents = [];
3233

33-
public function __construct(private DocumentManager $dm, private UnitOfWork $uow, private EventManager $evm)
34+
public function __construct(private DocumentManager $dm, private UnitOfWork $uow, private EventManager|EventDispatcherInterface $evm)
3435
{
3536
}
3637

@@ -55,7 +56,11 @@ public function enableTransactionalMode(Session $session): void
5556
public function documentNotFound(object $proxy, $id): bool
5657
{
5758
$eventArgs = new DocumentNotFoundEventArgs($proxy, $this->dm, $id);
58-
$this->evm->dispatchEvent(Events::documentNotFound, $eventArgs);
59+
if ($this->evm instanceof EventDispatcherInterface) {
60+
$this->evm->dispatch(Events::documentNotFound);
61+
} else {
62+
$this->evm->dispatchEvent(Events::documentNotFound, $eventArgs);
63+
}
5964

6065
return $eventArgs->isExceptionDisabled();
6166
}
@@ -68,7 +73,11 @@ public function documentNotFound(object $proxy, $id): bool
6873
public function postCollectionLoad(PersistentCollectionInterface $coll): void
6974
{
7075
$eventArgs = new PostCollectionLoadEventArgs($coll, $this->dm);
71-
$this->evm->dispatchEvent(Events::postCollectionLoad, $eventArgs);
76+
if ($this->evm instanceof EventDispatcherInterface) {
77+
$this->evm->dispatch($eventArgs, Events::postCollectionLoad);
78+
} else {
79+
$this->evm->dispatchEvent(Events::postCollectionLoad, $eventArgs);
80+
}
7281
}
7382

7483
/**
@@ -88,7 +97,7 @@ public function postPersist(ClassMetadata $class, object $document, ?Session $se
8897
$eventArgs = new LifecycleEventArgs($document, $this->dm, $session);
8998

9099
$class->invokeLifecycleCallbacks(Events::postPersist, $document, [$eventArgs]);
91-
$this->dispatchEvent($class, Events::postPersist, $eventArgs);
100+
$this->dispatchEvent($class, $eventArgs);
92101
$this->cascadePostPersist($class, $document, $session);
93102
}
94103

@@ -294,13 +303,17 @@ private function cascadePostPersist(ClassMetadata $class, object $document, ?Ses
294303
}
295304

296305
/** @param ClassMetadata<object> $class */
297-
private function dispatchEvent(ClassMetadata $class, string $eventName, ?EventArgs $eventArgs = null): void
306+
private function dispatchEvent(ClassMetadata $class, ?EventArgs $eventArgs = null): void
298307
{
299308
if ($class->isView()) {
300309
return;
301310
}
302311

303-
$this->evm->dispatchEvent($eventName, $eventArgs);
312+
if ($this->evm instanceof EventDispatcherInterface) {
313+
$this->evm->dispatch($eventArgs, Events::postPersist);
314+
} else {
315+
$this->evm->dispatchEvent(Events::postPersist, $eventArgs);
316+
}
304317
}
305318

306319
private function shouldDispatchEvent(object $document, string $eventName, ?Session $session): bool

0 commit comments

Comments
 (0)