1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
8
|
|
|
use Doctrine\Common\Cache\Cache; |
9
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
10
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
11
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; |
12
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; |
13
|
|
|
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionFactory; |
14
|
|
|
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionGenerator; |
15
|
|
|
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionFactory; |
16
|
|
|
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionGenerator; |
17
|
|
|
use Doctrine\ODM\MongoDB\Repository\DefaultGridFSRepository; |
18
|
|
|
use Doctrine\ODM\MongoDB\Repository\DefaultRepositoryFactory; |
19
|
|
|
use Doctrine\ODM\MongoDB\Repository\DocumentRepository; |
20
|
|
|
use Doctrine\ODM\MongoDB\Repository\GridFSRepository; |
21
|
|
|
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory; |
22
|
|
|
use function trim; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Configuration class for the DocumentManager. When setting up your DocumentManager |
26
|
|
|
* you can optionally specify an instance of this class as the second argument. |
27
|
|
|
* If you do not pass a configuration object, a blank one will be created for you. |
28
|
|
|
* |
29
|
|
|
* <?php |
30
|
|
|
* |
31
|
|
|
* $config = new Configuration(); |
32
|
|
|
* $dm = DocumentManager::create(new Connection(), $config); |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
class Configuration |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Array of attributes for this configuration instance. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $attributes = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Never autogenerate a proxy/hydrator/persistent collection and rely that |
46
|
|
|
* it was generated by some process before deployment. Copied from |
47
|
|
|
* \Doctrine\Common\Proxy\AbstractProxyFactory. |
48
|
|
|
* |
49
|
|
|
* @var integer |
50
|
|
|
*/ |
51
|
|
|
public const AUTOGENERATE_NEVER = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Always generates a new proxy/hydrator/persistent collection in every request. |
55
|
|
|
* |
56
|
|
|
* This is only sane during development. |
57
|
|
|
* Copied from \Doctrine\Common\Proxy\AbstractProxyFactory. |
58
|
|
|
* |
59
|
|
|
* @var integer |
60
|
|
|
*/ |
61
|
|
|
public const AUTOGENERATE_ALWAYS = 1; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Autogenerate the proxy/hydrator/persistent collection class when the file does not exist. |
65
|
|
|
* |
66
|
|
|
* This strategy causes a file exists call whenever any proxy/hydrator is used the |
67
|
|
|
* first time in a request. Copied from \Doctrine\Common\Proxy\AbstractProxyFactory. |
68
|
|
|
* |
69
|
|
|
* @var integer |
70
|
|
|
*/ |
71
|
|
|
public const AUTOGENERATE_FILE_NOT_EXISTS = 2; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Generate the proxy/hydrator/persistent collection classes using eval(). |
75
|
|
|
* |
76
|
|
|
* This strategy is only sane for development. |
77
|
|
|
* Copied from \Doctrine\Common\Proxy\AbstractProxyFactory. |
78
|
|
|
* |
79
|
|
|
* @var integer |
80
|
|
|
*/ |
81
|
|
|
public const AUTOGENERATE_EVAL = 3; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Adds a namespace under a certain alias. |
85
|
|
|
*/ |
86
|
|
|
public function addDocumentNamespace(string $alias, string $namespace): void |
87
|
|
|
{ |
88
|
|
|
$this->attributes['documentNamespaces'][$alias] = $namespace; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Resolves a registered namespace alias to the full namespace. |
93
|
|
|
* |
94
|
|
|
* @throws MongoDBException |
95
|
|
|
*/ |
96
|
|
|
public function getDocumentNamespace(string $documentNamespaceAlias): string |
97
|
|
|
{ |
98
|
|
|
if (! isset($this->attributes['documentNamespaces'][$documentNamespaceAlias])) { |
99
|
|
|
throw MongoDBException::unknownDocumentNamespace($documentNamespaceAlias); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return trim($this->attributes['documentNamespaces'][$documentNamespaceAlias], '\\'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieves the list of registered document namespace aliases. |
107
|
|
|
*/ |
108
|
|
|
public function getDocumentNamespaces(): array |
109
|
|
|
{ |
110
|
|
|
return $this->attributes['documentNamespaces']; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the document alias map |
115
|
|
|
*/ |
116
|
|
|
public function setDocumentNamespaces(array $documentNamespaces): void |
117
|
|
|
{ |
118
|
|
|
$this->attributes['documentNamespaces'] = $documentNamespaces; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Sets the cache driver implementation that is used for metadata caching. |
123
|
|
|
* |
124
|
|
|
* @todo Force parameter to be a Closure to ensure lazy evaluation |
125
|
|
|
* (as soon as a metadata cache is in effect, the driver never needs to initialize). |
126
|
|
|
*/ |
127
|
1641 |
|
public function setMetadataDriverImpl(MappingDriver $driverImpl): void |
128
|
|
|
{ |
129
|
1641 |
|
$this->attributes['metadataDriverImpl'] = $driverImpl; |
130
|
1641 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Add a new default annotation driver with a correctly configured annotation reader. |
134
|
|
|
*/ |
135
|
|
|
public function newDefaultAnnotationDriver(array $paths = []): AnnotationDriver |
136
|
|
|
{ |
137
|
|
|
$reader = new AnnotationReader(); |
138
|
|
|
|
139
|
|
|
return new AnnotationDriver($reader, (array) $paths); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Gets the cache driver implementation that is used for the mapping metadata. |
144
|
|
|
*/ |
145
|
1380 |
|
public function getMetadataDriverImpl(): ?MappingDriver |
146
|
|
|
{ |
147
|
1380 |
|
return $this->attributes['metadataDriverImpl'] ?? null; |
148
|
|
|
} |
149
|
|
|
|
150
|
1595 |
|
public function getMetadataCacheImpl(): ?Cache |
151
|
|
|
{ |
152
|
1595 |
|
return $this->attributes['metadataCacheImpl'] ?? null; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function setMetadataCacheImpl(Cache $cacheImpl): void |
156
|
|
|
{ |
157
|
|
|
$this->attributes['metadataCacheImpl'] = $cacheImpl; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Sets the directory where Doctrine generates any necessary proxy class files. |
162
|
|
|
*/ |
163
|
1595 |
|
public function setProxyDir(string $dir): void |
164
|
|
|
{ |
165
|
1595 |
|
$this->attributes['proxyDir'] = $dir; |
166
|
1595 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Gets the directory where Doctrine generates any necessary proxy class files. |
170
|
|
|
*/ |
171
|
1595 |
|
public function getProxyDir(): ?string |
172
|
|
|
{ |
173
|
1595 |
|
return $this->attributes['proxyDir'] ?? null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Gets an int flag that indicates whether proxy classes should always be regenerated |
178
|
|
|
* during each script execution. |
179
|
|
|
*/ |
180
|
1595 |
|
public function getAutoGenerateProxyClasses(): int |
181
|
|
|
{ |
182
|
1595 |
|
return $this->attributes['autoGenerateProxyClasses'] ?? self::AUTOGENERATE_ALWAYS; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Sets an int flag that indicates whether proxy classes should always be regenerated |
187
|
|
|
* during each script execution. |
188
|
|
|
*/ |
189
|
|
|
public function setAutoGenerateProxyClasses(int $mode): void |
190
|
|
|
{ |
191
|
|
|
$this->attributes['autoGenerateProxyClasses'] = $mode; |
192
|
|
|
} |
193
|
|
|
|
194
|
1595 |
|
public function getProxyNamespace(): ?string |
195
|
|
|
{ |
196
|
1595 |
|
return $this->attributes['proxyNamespace'] ?? null; |
197
|
|
|
} |
198
|
|
|
|
199
|
1595 |
|
public function setProxyNamespace(string $ns): void |
200
|
|
|
{ |
201
|
1595 |
|
$this->attributes['proxyNamespace'] = $ns; |
202
|
1595 |
|
} |
203
|
|
|
|
204
|
1595 |
|
public function setHydratorDir(string $dir): void |
205
|
|
|
{ |
206
|
1595 |
|
$this->attributes['hydratorDir'] = $dir; |
207
|
1595 |
|
} |
208
|
|
|
|
209
|
1595 |
|
public function getHydratorDir(): ?string |
210
|
|
|
{ |
211
|
1595 |
|
return $this->attributes['hydratorDir'] ?? null; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Gets an int flag that indicates whether hydrator classes should always be regenerated |
216
|
|
|
* during each script execution. |
217
|
|
|
*/ |
218
|
1595 |
|
public function getAutoGenerateHydratorClasses(): int |
219
|
|
|
{ |
220
|
1595 |
|
return $this->attributes['autoGenerateHydratorClasses'] ?? self::AUTOGENERATE_ALWAYS; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Sets an int flag that indicates whether hydrator classes should always be regenerated |
225
|
|
|
* during each script execution. |
226
|
|
|
*/ |
227
|
|
|
public function setAutoGenerateHydratorClasses(int $mode): void |
228
|
|
|
{ |
229
|
|
|
$this->attributes['autoGenerateHydratorClasses'] = $mode; |
230
|
|
|
} |
231
|
|
|
|
232
|
1595 |
|
public function getHydratorNamespace(): ?string |
233
|
|
|
{ |
234
|
1595 |
|
return $this->attributes['hydratorNamespace'] ?? null; |
235
|
|
|
} |
236
|
|
|
|
237
|
1595 |
|
public function setHydratorNamespace(string $ns): void |
238
|
|
|
{ |
239
|
1595 |
|
$this->attributes['hydratorNamespace'] = $ns; |
240
|
1595 |
|
} |
241
|
|
|
|
242
|
1595 |
|
public function setPersistentCollectionDir(string $dir): void |
243
|
|
|
{ |
244
|
1595 |
|
$this->attributes['persistentCollectionDir'] = $dir; |
245
|
1595 |
|
} |
246
|
|
|
|
247
|
11 |
|
public function getPersistentCollectionDir(): ?string |
248
|
|
|
{ |
249
|
11 |
|
return $this->attributes['persistentCollectionDir'] ?? null; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Gets a integer flag that indicates how and when persistent collection |
254
|
|
|
* classes should be generated. |
255
|
|
|
*/ |
256
|
7 |
|
public function getAutoGeneratePersistentCollectionClasses(): int |
257
|
|
|
{ |
258
|
7 |
|
return $this->attributes['autoGeneratePersistentCollectionClasses'] ?? self::AUTOGENERATE_ALWAYS; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Sets a integer flag that indicates how and when persistent collection |
263
|
|
|
* classes should be generated. |
264
|
|
|
*/ |
265
|
|
|
public function setAutoGeneratePersistentCollectionClasses(int $mode): void |
266
|
|
|
{ |
267
|
|
|
$this->attributes['autoGeneratePersistentCollectionClasses'] = $mode; |
268
|
|
|
} |
269
|
|
|
|
270
|
11 |
|
public function getPersistentCollectionNamespace(): ?string |
271
|
|
|
{ |
272
|
11 |
|
return $this->attributes['persistentCollectionNamespace'] ?? null; |
273
|
|
|
} |
274
|
|
|
|
275
|
1595 |
|
public function setPersistentCollectionNamespace(string $ns): void |
276
|
|
|
{ |
277
|
1595 |
|
$this->attributes['persistentCollectionNamespace'] = $ns; |
278
|
1595 |
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Sets the default DB to use for all Documents that do not specify |
282
|
|
|
* a database. |
283
|
|
|
*/ |
284
|
1595 |
|
public function setDefaultDB(string $defaultDB): void |
285
|
|
|
{ |
286
|
1595 |
|
$this->attributes['defaultDB'] = $defaultDB; |
287
|
1595 |
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Gets the default DB to use for all Documents that do not specify a database. |
291
|
|
|
*/ |
292
|
1258 |
|
public function getDefaultDB(): ?string |
293
|
|
|
{ |
294
|
1258 |
|
return $this->attributes['defaultDB'] ?? null; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function setClassMetadataFactoryName(string $cmfName): void |
298
|
|
|
{ |
299
|
|
|
$this->attributes['classMetadataFactoryName'] = $cmfName; |
300
|
|
|
} |
301
|
|
|
|
302
|
1595 |
|
public function getClassMetadataFactoryName(): string |
303
|
|
|
{ |
304
|
1595 |
|
if (! isset($this->attributes['classMetadataFactoryName'])) { |
305
|
1595 |
|
$this->attributes['classMetadataFactoryName'] = ClassMetadataFactory::class; |
306
|
|
|
} |
307
|
1595 |
|
return $this->attributes['classMetadataFactoryName']; |
308
|
|
|
} |
309
|
|
|
|
310
|
552 |
|
public function getDefaultCommitOptions(): array |
311
|
|
|
{ |
312
|
552 |
|
return $this->attributes['defaultCommitOptions'] ?? ['w' => 1]; |
313
|
|
|
} |
314
|
|
|
|
315
|
1 |
|
public function setDefaultCommitOptions(array $defaultCommitOptions): void |
316
|
|
|
{ |
317
|
1 |
|
$this->attributes['defaultCommitOptions'] = $defaultCommitOptions; |
318
|
1 |
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Add a filter to the list of possible filters. |
322
|
|
|
*/ |
323
|
1595 |
|
public function addFilter(string $name, string $className, array $parameters = []): void |
324
|
|
|
{ |
325
|
1595 |
|
$this->attributes['filters'][$name] = [ |
326
|
1595 |
|
'class' => $className, |
327
|
1595 |
|
'parameters' => $parameters, |
328
|
|
|
]; |
329
|
1595 |
|
} |
330
|
|
|
|
331
|
24 |
|
public function getFilterClassName(string $name): ?string |
332
|
|
|
{ |
333
|
24 |
|
return isset($this->attributes['filters'][$name]) |
334
|
24 |
|
? $this->attributes['filters'][$name]['class'] |
335
|
24 |
|
: null; |
336
|
|
|
} |
337
|
|
|
|
338
|
23 |
|
public function getFilterParameters(string $name): ?array |
339
|
|
|
{ |
340
|
23 |
|
return isset($this->attributes['filters'][$name]) |
341
|
23 |
|
? $this->attributes['filters'][$name]['parameters'] |
342
|
23 |
|
: null; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @throws MongoDBException If not is a ObjectRepository. |
347
|
|
|
*/ |
348
|
|
|
public function setDefaultDocumentRepositoryClassName(string $className): void |
349
|
|
|
{ |
350
|
|
|
$reflectionClass = new \ReflectionClass($className); |
351
|
|
|
|
352
|
|
|
if (! $reflectionClass->implementsInterface(ObjectRepository::class)) { |
353
|
|
|
throw MongoDBException::invalidDocumentRepository($className); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
$this->attributes['defaultDocumentRepositoryClassName'] = $className; |
357
|
|
|
} |
358
|
|
|
|
359
|
319 |
|
public function getDefaultDocumentRepositoryClassName(): string |
360
|
|
|
{ |
361
|
319 |
|
return $this->attributes['defaultDocumentRepositoryClassName'] ?? DocumentRepository::class; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @throws MongoDBException If the class does not implement the GridFSRepository interface. |
366
|
|
|
*/ |
367
|
|
|
public function setDefaultGridFSRepositoryClassName(string $className): void |
368
|
|
|
{ |
369
|
|
|
$reflectionClass = new \ReflectionClass($className); |
370
|
|
|
|
371
|
|
|
if (! $reflectionClass->implementsInterface(GridFSRepository::class)) { |
372
|
|
|
throw MongoDBException::invalidGridFSRepository($className); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
$this->attributes['defaultGridFSRepositoryClassName'] = $className; |
376
|
|
|
} |
377
|
|
|
|
378
|
7 |
|
public function getDefaultGridFSRepositoryClassName(): string |
379
|
|
|
{ |
380
|
7 |
|
return $this->attributes['defaultGridFSRepositoryClassName'] ?? DefaultGridFSRepository::class; |
381
|
|
|
} |
382
|
|
|
|
383
|
2 |
|
public function setRepositoryFactory(RepositoryFactory $repositoryFactory): void |
384
|
|
|
{ |
385
|
2 |
|
$this->attributes['repositoryFactory'] = $repositoryFactory; |
386
|
2 |
|
} |
387
|
|
|
|
388
|
1595 |
|
public function getRepositoryFactory(): RepositoryFactory |
389
|
|
|
{ |
390
|
1595 |
|
return $this->attributes['repositoryFactory'] ?? new DefaultRepositoryFactory(); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
public function setPersistentCollectionFactory(PersistentCollectionFactory $persistentCollectionFactory): void |
394
|
|
|
{ |
395
|
|
|
$this->attributes['persistentCollectionFactory'] = $persistentCollectionFactory; |
396
|
|
|
} |
397
|
|
|
|
398
|
392 |
|
public function getPersistentCollectionFactory(): PersistentCollectionFactory |
399
|
|
|
{ |
400
|
392 |
|
if (! isset($this->attributes['persistentCollectionFactory'])) { |
401
|
392 |
|
$this->attributes['persistentCollectionFactory'] = new DefaultPersistentCollectionFactory(); |
402
|
|
|
} |
403
|
392 |
|
return $this->attributes['persistentCollectionFactory']; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
public function setPersistentCollectionGenerator(PersistentCollectionGenerator $persistentCollectionGenerator): void |
407
|
|
|
{ |
408
|
|
|
$this->attributes['persistentCollectionGenerator'] = $persistentCollectionGenerator; |
409
|
|
|
} |
410
|
|
|
|
411
|
8 |
|
public function getPersistentCollectionGenerator(): PersistentCollectionGenerator |
412
|
|
|
{ |
413
|
8 |
|
if (! isset($this->attributes['persistentCollectionGenerator'])) { |
414
|
8 |
|
$this->attributes['persistentCollectionGenerator'] = new DefaultPersistentCollectionGenerator( |
415
|
8 |
|
$this->getPersistentCollectionDir(), |
416
|
8 |
|
$this->getPersistentCollectionNamespace() |
417
|
|
|
); |
418
|
|
|
} |
419
|
8 |
|
return $this->attributes['persistentCollectionGenerator']; |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|