Skip to content

Commit affbab3

Browse files
committed
-
1 parent 4907252 commit affbab3

19 files changed

+106
-485
lines changed

composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"psr/cache": "^1.0 || ^2.0 || ^3.0",
3535
"symfony/console": "^5.4 || ^6.0 || ^7.0",
3636
"symfony/deprecation-contracts": "^2.2 || ^3.0",
37+
"symfony/event-dispatcher": "^7.1",
3738
"symfony/var-dumper": "^5.4 || ^6.0 || ^7.0"
3839
},
3940
"require-dev": {
@@ -48,6 +49,7 @@
4849
"phpunit/phpunit": "^10.4",
4950
"squizlabs/php_codesniffer": "^3.5",
5051
"symfony/cache": "^5.4 || ^6.0 || ^7.0",
52+
"symfony/event-dispatcher-contracts": "^3.5",
5153
"vimeo/psalm": "~5.24.0"
5254
},
5355
"conflict": {

lib/Doctrine/ODM/MongoDB/DocumentManager.php

+33-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Doctrine\ODM\MongoDB\Repository\GridFSRepository;
2020
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
2121
use Doctrine\ODM\MongoDB\Repository\ViewRepository;
22+
use Doctrine\ODM\MongoDB\Utility\EventDispatcher;
2223
use Doctrine\Persistence\Mapping\ProxyClassNameResolver;
2324
use Doctrine\Persistence\ObjectManager;
2425
use InvalidArgumentException;
@@ -81,7 +82,12 @@ class DocumentManager implements ObjectManager
8182
/**
8283
* The event manager that is the central point of the event system.
8384
*/
84-
private EventManager|EventDispatcherInterface $eventManager;
85+
private ?EventManager $eventManager;
86+
87+
/**
88+
* The event dispatcher that is the central point of the event system.
89+
*/
90+
private EventDispatcherInterface $eventDispatcher;
8591

8692
/**
8793
* The Hydrator factory instance.
@@ -143,11 +149,23 @@ class DocumentManager implements ObjectManager
143149
* Creates a new Document that operates on the given Mongo connection
144150
* and uses the given Configuration.
145151
*/
146-
protected function __construct(?Client $client = null, ?Configuration $config = null, EventManager|EventDispatcherInterface|null $eventManager = null)
152+
protected function __construct(?Client $client = null, ?Configuration $config = null, EventManager|EventDispatcherInterface|null $eventDispatcher = null)
147153
{
148-
$this->config = $config ?: new Configuration();
149-
$this->eventManager = $eventManager ?: new EventManager();
150-
$this->client = $client ?: new Client(
154+
$this->config = $config ?: new Configuration();
155+
156+
if ($eventDispatcher instanceof EventDispatcherInterface) {
157+
// This is a new feature, we can accept that the EventManager
158+
// is not available when and EventDispatcher is injected.
159+
$this->eventManager = null;
160+
$this->eventDispatcher = $eventDispatcher;
161+
} else {
162+
// Backward compatibility with Doctrine EventManager
163+
// @todo deprecate and create a new Symfony EventDispatcher instance
164+
$this->eventManager = $eventDispatcher ?? new EventManager();
165+
$this->eventDispatcher = new EventDispatcher($this->eventManager);
166+
}
167+
168+
$this->client = $client ?: new Client(
151169
'mongodb://127.0.0.1',
152170
[],
153171
[
@@ -181,7 +199,7 @@ protected function __construct(?Client $client = null, ?Configuration $config =
181199
$this->config->getAutoGenerateHydratorClasses(),
182200
);
183201

184-
$this->unitOfWork = new UnitOfWork($this, $this->eventManager, $this->hydratorFactory);
202+
$this->unitOfWork = new UnitOfWork($this, $this->eventDispatcher, $this->hydratorFactory);
185203
$this->schemaManager = new SchemaManager($this, $this->metadataFactory);
186204
$this->proxyFactory = new StaticProxyFactory($this);
187205
$this->repositoryFactory = $this->config->getRepositoryFactory();
@@ -204,19 +222,24 @@ public static function create(?Client $client = null, ?Configuration $config = n
204222
return new static($client, $config, $eventManager);
205223
}
206224

207-
public function getEventDispatcher(): EventManager|EventDispatcherInterface
225+
/**
226+
* @todo should we return a {@see Symfony\Component\EventDispatcher\EventDispatcherInterface} instead?
227+
* So that it's explicitly possible to add/remove listeners. Or we just rely on
228+
* the object that is injected and let the user validate the subtype.
229+
*/
230+
public function getEventDispatcher(): EventDispatcherInterface
208231
{
209-
return $this->eventManager;
232+
return $this->eventDispatcher;
210233
}
211234

212235
/**
213236
* Gets the EventManager used by the DocumentManager.
214237
*
215-
* @deprecated Use getEventDispatcher() instead
238+
* @deprecated Use getEventDispatcher() instead.
216239
*/
217240
public function getEventManager(): EventManager
218241
{
219-
if (! $this->eventManager instanceof EventManager) {
242+
if (! $this->eventManager) {
220243
throw new LogicException('Use getEventDispatcher() instead of getEventManager()');
221244
}
222245

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

-49
This file was deleted.

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

-40
This file was deleted.

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

-31
This file was deleted.

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

-23
This file was deleted.

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

-48
This file was deleted.

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

-19
This file was deleted.

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

-12
This file was deleted.

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

-33
This file was deleted.

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

-12
This file was deleted.

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

-12
This file was deleted.

0 commit comments

Comments
 (0)