-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
Configuration.php
598 lines (514 loc) · 19.4 KB
/
Configuration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<?php
declare(strict_types=1);
namespace Doctrine\ODM\MongoDB;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\Psr6\CacheAdapter;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionFactory;
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionGenerator;
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionFactory;
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionGenerator;
use Doctrine\ODM\MongoDB\Proxy\FileLocator;
use Doctrine\ODM\MongoDB\Repository\DefaultGridFSRepository;
use Doctrine\ODM\MongoDB\Repository\DefaultRepositoryFactory;
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
use Doctrine\ODM\MongoDB\Repository\GridFSRepository;
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\ObjectRepository;
use InvalidArgumentException;
use MongoDB\Driver\WriteConcern;
use ProxyManager\Configuration as ProxyManagerConfiguration;
use ProxyManager\Factory\LazyLoadingGhostFactory;
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
use Psr\Cache\CacheItemPoolInterface;
use ReflectionClass;
use function interface_exists;
use function trigger_deprecation;
use function trim;
/**
* Configuration class for the DocumentManager. When setting up your DocumentManager
* you can optionally specify an instance of this class as the second argument.
* If you do not pass a configuration object, a blank one will be created for you.
*
* <?php
*
* $config = new Configuration();
* $dm = DocumentManager::create(new Connection(), $config);
*
* @psalm-import-type CommitOptions from UnitOfWork
*/
class Configuration
{
/**
* Never autogenerate a proxy/hydrator/persistent collection and rely that
* it was generated by some process before deployment. Copied from
* \Doctrine\Common\Proxy\AbstractProxyFactory.
*/
public const AUTOGENERATE_NEVER = 0;
/**
* Always generates a new proxy/hydrator/persistent collection in every request.
*
* This is only sane during development.
* Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
*/
public const AUTOGENERATE_ALWAYS = 1;
/**
* Autogenerate the proxy/hydrator/persistent collection class when the file does not exist.
*
* This strategy causes a file exists call whenever any proxy/hydrator is used the
* first time in a request. Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
*/
public const AUTOGENERATE_FILE_NOT_EXISTS = 2;
/**
* Generate the proxy/hydrator/persistent collection classes using eval().
*
* This strategy is only sane for development.
* Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
*/
public const AUTOGENERATE_EVAL = 3;
/**
* Array of attributes for this configuration instance.
*
* @var array
* @psalm-var array{
* autoGenerateHydratorClasses?: self::AUTOGENERATE_*,
* autoGeneratePersistentCollectionClasses?: self::AUTOGENERATE_*,
* classMetadataFactoryName?: class-string<ClassMetadataFactory>,
* defaultCommitOptions?: CommitOptions,
* defaultDocumentRepositoryClassName?: class-string<ObjectRepository>,
* defaultGridFSRepositoryClassName?: class-string<GridFSRepository>,
* defaultDB?: string,
* documentNamespaces?: array<string, string>,
* filters?: array<string, array{
* class: class-string,
* parameters: array<string, mixed>
* }>,
* hydratorDir?: string,
* hydratorNamespace?: string,
* metadataCacheImpl?: Cache,
* metadataDriverImpl?: MappingDriver,
* persistentCollectionFactory?: PersistentCollectionFactory,
* persistentCollectionGenerator?: PersistentCollectionGenerator,
* persistentCollectionDir?: string,
* persistentCollectionNamespace?: string,
* repositoryFactory?: RepositoryFactory
* }
*/
private $attributes = [];
/** @var CacheItemPoolInterface|null */
private $metadataCache;
/** @var ProxyManagerConfiguration */
private $proxyManagerConfiguration;
/** @var int */
private $autoGenerateProxyClasses = self::AUTOGENERATE_EVAL;
public function __construct()
{
$this->proxyManagerConfiguration = new ProxyManagerConfiguration();
$this->setAutoGenerateProxyClasses(self::AUTOGENERATE_FILE_NOT_EXISTS);
}
/**
* Adds a namespace under a certain alias.
*/
public function addDocumentNamespace(string $alias, string $namespace): void
{
$this->attributes['documentNamespaces'][$alias] = $namespace;
}
/**
* Resolves a registered namespace alias to the full namespace.
*
* @throws MongoDBException
*/
public function getDocumentNamespace(string $documentNamespaceAlias): string
{
trigger_deprecation(
'doctrine/mongodb-odm',
'2.3',
'Document short namespace aliases such as "%s" are deprecated, use ::class constant instead.',
$documentNamespaceAlias
);
if (! isset($this->attributes['documentNamespaces'][$documentNamespaceAlias])) {
throw MongoDBException::unknownDocumentNamespace($documentNamespaceAlias);
}
return trim($this->attributes['documentNamespaces'][$documentNamespaceAlias], '\\');
}
/**
* Retrieves the list of registered document namespace aliases.
*
* @return array<string, string>
*/
public function getDocumentNamespaces(): array
{
return $this->attributes['documentNamespaces'];
}
/**
* Set the document alias map
*
* @param array<string, string> $documentNamespaces
*/
public function setDocumentNamespaces(array $documentNamespaces): void
{
$this->attributes['documentNamespaces'] = $documentNamespaces;
}
/**
* Sets the cache driver implementation that is used for metadata caching.
*
* @todo Force parameter to be a Closure to ensure lazy evaluation
* (as soon as a metadata cache is in effect, the driver never needs to initialize).
*/
public function setMetadataDriverImpl(MappingDriver $driverImpl): void
{
$this->attributes['metadataDriverImpl'] = $driverImpl;
}
/**
* Add a new default annotation driver with a correctly configured annotation reader.
*
* @param string[] $paths
*/
public function newDefaultAnnotationDriver(array $paths = []): AnnotationDriver
{
$reader = new AnnotationReader();
return new AnnotationDriver($reader, $paths);
}
/**
* Gets the cache driver implementation that is used for the mapping metadata.
*/
public function getMetadataDriverImpl(): ?MappingDriver
{
return $this->attributes['metadataDriverImpl'] ?? null;
}
public function getMetadataCacheImpl(): ?Cache
{
trigger_deprecation(
'doctrine/mongodb-odm',
'2.2',
'Using "%s" is deprecated. Please use "%s::getMetadataCache" instead.',
__METHOD__,
self::class
);
return $this->attributes['metadataCacheImpl'] ?? null;
}
public function setMetadataCacheImpl(Cache $cacheImpl): void
{
trigger_deprecation(
'doctrine/mongodb-odm',
'2.2',
'Using "%s" is deprecated. Please use "%s::setMetadataCache" instead.',
__METHOD__,
self::class
);
$this->attributes['metadataCacheImpl'] = $cacheImpl;
$this->metadataCache = CacheAdapter::wrap($cacheImpl);
}
public function getMetadataCache(): ?CacheItemPoolInterface
{
return $this->metadataCache;
}
public function setMetadataCache(CacheItemPoolInterface $cache): void
{
$this->metadataCache = $cache;
$this->attributes['metadataCacheImpl'] = DoctrineProvider::wrap($cache);
}
/**
* Sets the directory where Doctrine generates any necessary proxy class files.
*/
public function setProxyDir(string $dir): void
{
$this->getProxyManagerConfiguration()->setProxiesTargetDir($dir);
// Recreate proxy generator to ensure its path was updated
if ($this->autoGenerateProxyClasses !== self::AUTOGENERATE_FILE_NOT_EXISTS) {
return;
}
$this->setAutoGenerateProxyClasses($this->autoGenerateProxyClasses);
}
/**
* Gets the directory where Doctrine generates any necessary proxy class files.
*/
public function getProxyDir(): ?string
{
return $this->getProxyManagerConfiguration()->getProxiesTargetDir();
}
/**
* Gets an int flag that indicates whether proxy classes should always be regenerated
* during each script execution.
*/
public function getAutoGenerateProxyClasses(): int
{
return $this->autoGenerateProxyClasses;
}
/**
* Sets an int flag that indicates whether proxy classes should always be regenerated
* during each script execution.
*
* @throws InvalidArgumentException If an invalid mode was given.
*/
public function setAutoGenerateProxyClasses(int $mode): void
{
$this->autoGenerateProxyClasses = $mode;
$proxyManagerConfig = $this->getProxyManagerConfiguration();
switch ($mode) {
case self::AUTOGENERATE_FILE_NOT_EXISTS:
$proxyManagerConfig->setGeneratorStrategy(new FileWriterGeneratorStrategy(
new FileLocator($proxyManagerConfig->getProxiesTargetDir())
));
break;
case self::AUTOGENERATE_EVAL:
$proxyManagerConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
break;
default:
throw new InvalidArgumentException('Invalid proxy generation strategy given - only AUTOGENERATE_FILE_NOT_EXISTS and AUTOGENERATE_EVAL are supported.');
}
}
public function getProxyNamespace(): ?string
{
return $this->getProxyManagerConfiguration()->getProxiesNamespace();
}
public function setProxyNamespace(string $ns): void
{
$this->getProxyManagerConfiguration()->setProxiesNamespace($ns);
}
public function setHydratorDir(string $dir): void
{
$this->attributes['hydratorDir'] = $dir;
}
public function getHydratorDir(): ?string
{
return $this->attributes['hydratorDir'] ?? null;
}
/**
* Gets an int flag that indicates whether hydrator classes should always be regenerated
* during each script execution.
*
* @psalm-return self::AUTOGENERATE_*
*/
public function getAutoGenerateHydratorClasses(): int
{
return $this->attributes['autoGenerateHydratorClasses'] ?? self::AUTOGENERATE_ALWAYS;
}
/**
* Sets an int flag that indicates whether hydrator classes should always be regenerated
* during each script execution.
*
* @psalm-param self::AUTOGENERATE_* $mode
*/
public function setAutoGenerateHydratorClasses(int $mode): void
{
$this->attributes['autoGenerateHydratorClasses'] = $mode;
}
public function getHydratorNamespace(): ?string
{
return $this->attributes['hydratorNamespace'] ?? null;
}
public function setHydratorNamespace(string $ns): void
{
$this->attributes['hydratorNamespace'] = $ns;
}
public function setPersistentCollectionDir(string $dir): void
{
$this->attributes['persistentCollectionDir'] = $dir;
}
public function getPersistentCollectionDir(): ?string
{
return $this->attributes['persistentCollectionDir'] ?? null;
}
/**
* Gets a integer flag that indicates how and when persistent collection
* classes should be generated.
*
* @psalm-return self::AUTOGENERATE_*
*/
public function getAutoGeneratePersistentCollectionClasses(): int
{
return $this->attributes['autoGeneratePersistentCollectionClasses'] ?? self::AUTOGENERATE_ALWAYS;
}
/**
* Sets a integer flag that indicates how and when persistent collection
* classes should be generated.
*
* @psalm-param self::AUTOGENERATE_* $mode
*/
public function setAutoGeneratePersistentCollectionClasses(int $mode): void
{
$this->attributes['autoGeneratePersistentCollectionClasses'] = $mode;
}
public function getPersistentCollectionNamespace(): ?string
{
return $this->attributes['persistentCollectionNamespace'] ?? null;
}
public function setPersistentCollectionNamespace(string $ns): void
{
$this->attributes['persistentCollectionNamespace'] = $ns;
}
/**
* Sets the default DB to use for all Documents that do not specify
* a database.
*/
public function setDefaultDB(string $defaultDB): void
{
$this->attributes['defaultDB'] = $defaultDB;
}
/**
* Gets the default DB to use for all Documents that do not specify a database.
*/
public function getDefaultDB(): ?string
{
return $this->attributes['defaultDB'] ?? null;
}
/**
* @psalm-param class-string<ClassMetadataFactory> $cmfName
*/
public function setClassMetadataFactoryName(string $cmfName): void
{
$this->attributes['classMetadataFactoryName'] = $cmfName;
}
/**
* @psalm-return class-string<ClassMetadataFactory>
*/
public function getClassMetadataFactoryName(): string
{
if (! isset($this->attributes['classMetadataFactoryName'])) {
$this->attributes['classMetadataFactoryName'] = ClassMetadataFactory::class;
}
return $this->attributes['classMetadataFactoryName'];
}
/**
* @psalm-return CommitOptions
*/
public function getDefaultCommitOptions(): array
{
if (! isset($this->attributes['defaultCommitOptions'])) {
$this->attributes['defaultCommitOptions'] = ['writeConcern' => new WriteConcern(1)];
}
return $this->attributes['defaultCommitOptions'];
}
/**
* @psalm-param CommitOptions $defaultCommitOptions
*/
public function setDefaultCommitOptions(array $defaultCommitOptions): void
{
$this->attributes['defaultCommitOptions'] = $defaultCommitOptions;
}
/**
* Add a filter to the list of possible filters.
*
* @param array<string, mixed> $parameters
* @psalm-param class-string $className
*/
public function addFilter(string $name, string $className, array $parameters = []): void
{
$this->attributes['filters'][$name] = [
'class' => $className,
'parameters' => $parameters,
];
}
/**
* @psalm-return class-string|null
*/
public function getFilterClassName(string $name): ?string
{
return isset($this->attributes['filters'][$name])
? $this->attributes['filters'][$name]['class']
: null;
}
/**
* @return array<string, mixed>
*/
public function getFilterParameters(string $name): array
{
return isset($this->attributes['filters'][$name])
? $this->attributes['filters'][$name]['parameters']
: [];
}
/**
* @psalm-param class-string<ObjectRepository> $className
*
* @throws MongoDBException If not is a ObjectRepository.
*/
public function setDefaultDocumentRepositoryClassName(string $className): void
{
$reflectionClass = new ReflectionClass($className);
if (! $reflectionClass->implementsInterface(ObjectRepository::class)) {
throw MongoDBException::invalidDocumentRepository($className);
}
$this->attributes['defaultDocumentRepositoryClassName'] = $className;
}
/**
* @psalm-return class-string<ObjectRepository>
*/
public function getDefaultDocumentRepositoryClassName(): string
{
return $this->attributes['defaultDocumentRepositoryClassName'] ?? DocumentRepository::class;
}
/**
* @psalm-param class-string<GridFSRepository> $className
*
* @throws MongoDBException If the class does not implement the GridFSRepository interface.
*/
public function setDefaultGridFSRepositoryClassName(string $className): void
{
$reflectionClass = new ReflectionClass($className);
if (! $reflectionClass->implementsInterface(GridFSRepository::class)) {
throw MongoDBException::invalidGridFSRepository($className);
}
$this->attributes['defaultGridFSRepositoryClassName'] = $className;
}
/**
* @psalm-return class-string<GridFSRepository>
*/
public function getDefaultGridFSRepositoryClassName(): string
{
return $this->attributes['defaultGridFSRepositoryClassName'] ?? DefaultGridFSRepository::class;
}
public function setRepositoryFactory(RepositoryFactory $repositoryFactory): void
{
$this->attributes['repositoryFactory'] = $repositoryFactory;
}
public function getRepositoryFactory(): RepositoryFactory
{
return $this->attributes['repositoryFactory'] ?? new DefaultRepositoryFactory();
}
public function setPersistentCollectionFactory(PersistentCollectionFactory $persistentCollectionFactory): void
{
$this->attributes['persistentCollectionFactory'] = $persistentCollectionFactory;
}
public function getPersistentCollectionFactory(): PersistentCollectionFactory
{
if (! isset($this->attributes['persistentCollectionFactory'])) {
$this->attributes['persistentCollectionFactory'] = new DefaultPersistentCollectionFactory();
}
return $this->attributes['persistentCollectionFactory'];
}
public function setPersistentCollectionGenerator(PersistentCollectionGenerator $persistentCollectionGenerator): void
{
$this->attributes['persistentCollectionGenerator'] = $persistentCollectionGenerator;
}
public function getPersistentCollectionGenerator(): PersistentCollectionGenerator
{
if (! isset($this->attributes['persistentCollectionGenerator'])) {
if ($this->getPersistentCollectionDir() === null) {
throw ConfigurationException::persistentCollectionDirMissing();
}
if ($this->getPersistentCollectionNamespace() === null) {
throw ConfigurationException::persistentCollectionNamespaceMissing();
}
$this->attributes['persistentCollectionGenerator'] = new DefaultPersistentCollectionGenerator(
$this->getPersistentCollectionDir(),
$this->getPersistentCollectionNamespace()
);
}
return $this->attributes['persistentCollectionGenerator'];
}
public function buildGhostObjectFactory(): LazyLoadingGhostFactory
{
return new LazyLoadingGhostFactory(clone $this->getProxyManagerConfiguration());
}
public function getProxyManagerConfiguration(): ProxyManagerConfiguration
{
return $this->proxyManagerConfiguration;
}
}
interface_exists(MappingDriver::class);