19
19
use Doctrine \ODM \MongoDB \Repository \GridFSRepository ;
20
20
use Doctrine \ODM \MongoDB \Repository \RepositoryFactory ;
21
21
use Doctrine \ODM \MongoDB \Repository \ViewRepository ;
22
+ use Doctrine \ODM \MongoDB \Utility \EventDispatcher ;
22
23
use Doctrine \Persistence \Mapping \ProxyClassNameResolver ;
23
24
use Doctrine \Persistence \ObjectManager ;
24
25
use InvalidArgumentException ;
@@ -81,7 +82,12 @@ class DocumentManager implements ObjectManager
81
82
/**
82
83
* The event manager that is the central point of the event system.
83
84
*/
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 ;
85
91
86
92
/**
87
93
* The Hydrator factory instance.
@@ -143,11 +149,23 @@ class DocumentManager implements ObjectManager
143
149
* Creates a new Document that operates on the given Mongo connection
144
150
* and uses the given Configuration.
145
151
*/
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 )
147
153
{
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 (
151
169
'mongodb://127.0.0.1 ' ,
152
170
[],
153
171
[
@@ -181,7 +199,7 @@ protected function __construct(?Client $client = null, ?Configuration $config =
181
199
$ this ->config ->getAutoGenerateHydratorClasses (),
182
200
);
183
201
184
- $ this ->unitOfWork = new UnitOfWork ($ this , $ this ->eventManager , $ this ->hydratorFactory );
202
+ $ this ->unitOfWork = new UnitOfWork ($ this , $ this ->eventDispatcher , $ this ->hydratorFactory );
185
203
$ this ->schemaManager = new SchemaManager ($ this , $ this ->metadataFactory );
186
204
$ this ->proxyFactory = new StaticProxyFactory ($ this );
187
205
$ this ->repositoryFactory = $ this ->config ->getRepositoryFactory ();
@@ -204,19 +222,24 @@ public static function create(?Client $client = null, ?Configuration $config = n
204
222
return new static ($ client , $ config , $ eventManager );
205
223
}
206
224
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
208
231
{
209
- return $ this ->eventManager ;
232
+ return $ this ->eventDispatcher ;
210
233
}
211
234
212
235
/**
213
236
* Gets the EventManager used by the DocumentManager.
214
237
*
215
- * @deprecated Use getEventDispatcher() instead
238
+ * @deprecated Use getEventDispatcher() instead.
216
239
*/
217
240
public function getEventManager (): EventManager
218
241
{
219
- if (! $ this ->eventManager instanceof EventManager ) {
242
+ if (! $ this ->eventManager ) {
220
243
throw new LogicException ('Use getEventDispatcher() instead of getEventManager() ' );
221
244
}
222
245
0 commit comments