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