Completed
Pull Request — master (#1790)
by Andreas
21:41
created

Configuration::getFilterParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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
     * @param string $alias
87
     * @param string $namespace
88
     */
89
    public function addDocumentNamespace($alias, $namespace)
90
    {
91
        $this->attributes['documentNamespaces'][$alias] = $namespace;
92
    }
93
94
    /**
95
     * Resolves a registered namespace alias to the full namespace.
96
     *
97
     * @param string $documentNamespaceAlias
98
     * @return string
99
     * @throws MongoDBException
100
     */
101
    public function getDocumentNamespace($documentNamespaceAlias)
102
    {
103
        if (! isset($this->attributes['documentNamespaces'][$documentNamespaceAlias])) {
104
            throw MongoDBException::unknownDocumentNamespace($documentNamespaceAlias);
105
        }
106
107
        return trim($this->attributes['documentNamespaces'][$documentNamespaceAlias], '\\');
108
    }
109
110
    /**
111
     * Retrieves the list of registered document namespace aliases.
112
     *
113
     * @return array
114
     */
115
    public function getDocumentNamespaces()
116
    {
117
        return $this->attributes['documentNamespaces'];
118
    }
119
120
    /**
121
     * Set the document alias map
122
     *
123
     * @param array $documentNamespaces
124
     */
125
    public function setDocumentNamespaces(array $documentNamespaces)
126
    {
127
        $this->attributes['documentNamespaces'] = $documentNamespaces;
128
    }
129
130
    /**
131
     * Sets the cache driver implementation that is used for metadata caching.
132
     *
133 1623
     * @todo Force parameter to be a Closure to ensure lazy evaluation
134
     *       (as soon as a metadata cache is in effect, the driver never needs to initialize).
135 1623
     */
136 1623
    public function setMetadataDriverImpl(MappingDriver $driverImpl)
137
    {
138
        $this->attributes['metadataDriverImpl'] = $driverImpl;
139
    }
140
141
    /**
142
     * Add a new default annotation driver with a correctly configured annotation reader.
143
     *
144
     * @param array $paths
145
     * @return Mapping\Driver\AnnotationDriver
146
     */
147
    public function newDefaultAnnotationDriver($paths = [])
148
    {
149
        $reader = new AnnotationReader();
150
151
        return new AnnotationDriver($reader, (array) $paths);
152
    }
153
154
    /**
155
     * Gets the cache driver implementation that is used for the mapping metadata.
156 1365
     *
157
     * @return MappingDriver
158 1365
     */
159
    public function getMetadataDriverImpl()
160
    {
161
        return $this->attributes['metadataDriverImpl'] ?? null;
162
    }
163
164
    /**
165
     * Gets the cache driver implementation that is used for metadata caching.
166 1580
     *
167
     * @return Cache
168 1580
     */
169
    public function getMetadataCacheImpl()
170
    {
171
        return $this->attributes['metadataCacheImpl'] ?? null;
172
    }
173
174
    /**
175
     * Sets the cache driver implementation that is used for metadata caching.
176
     *
177
     */
178
    public function setMetadataCacheImpl(Cache $cacheImpl)
179
    {
180
        $this->attributes['metadataCacheImpl'] = $cacheImpl;
181
    }
182
183
    /**
184
     * Sets the directory where Doctrine generates any necessary proxy class files.
185 1580
     *
186
     * @param string $dir
187 1580
     */
188 1580
    public function setProxyDir($dir)
189
    {
190
        $this->attributes['proxyDir'] = $dir;
191
    }
192
193
    /**
194
     * Gets the directory where Doctrine generates any necessary proxy class files.
195 1580
     *
196
     * @return string
197 1580
     */
198
    public function getProxyDir()
199
    {
200
        return $this->attributes['proxyDir'] ?? null;
201
    }
202
203
    /**
204
     * Gets a boolean flag that indicates whether proxy classes should always be regenerated
205
     * during each script execution.
206 1580
     *
207
     * @return bool|int
208 1580
     */
209
    public function getAutoGenerateProxyClasses()
210
    {
211
        return $this->attributes['autoGenerateProxyClasses'] ?? true;
212
    }
213
214
    /**
215
     * Sets a boolean flag that indicates whether proxy classes should always be regenerated
216
     * during each script execution.
217
     *
218
     * @param bool|int $bool Possible values are constants of Doctrine\Common\Proxy\AbstractProxyFactory
219
     */
220
    public function setAutoGenerateProxyClasses($bool)
221
    {
222
        $this->attributes['autoGenerateProxyClasses'] = $bool;
223
    }
224
225
    /**
226
     * Gets the namespace where proxy classes reside.
227 1580
     *
228
     * @return string
229 1580
     */
230
    public function getProxyNamespace()
231
    {
232
        return $this->attributes['proxyNamespace'] ?? null;
233
    }
234
235
    /**
236
     * Sets the namespace where proxy classes reside.
237 1580
     *
238
     * @param string $ns
239 1580
     */
240 1580
    public function setProxyNamespace($ns)
241
    {
242
        $this->attributes['proxyNamespace'] = $ns;
243
    }
244
245
    /**
246
     * Sets the directory where Doctrine generates hydrator class files.
247 1580
     *
248
     * @param string $dir
249 1580
     */
250 1580
    public function setHydratorDir($dir)
251
    {
252
        $this->attributes['hydratorDir'] = $dir;
253
    }
254
255
    /**
256
     * Gets the directory where Doctrine generates hydrator class files.
257 1580
     *
258
     * @return string
259 1580
     */
260
    public function getHydratorDir()
261
    {
262
        return $this->attributes['hydratorDir'] ?? null;
263
    }
264
265
    /**
266
     * Gets a boolean flag that indicates whether hydrator classes should always be regenerated
267
     * during each script execution.
268 1580
     *
269
     * @return bool|int Possible values are defined constants
270 1580
     */
271
    public function getAutoGenerateHydratorClasses()
272
    {
273
        return $this->attributes['autoGenerateHydratorClasses'] ?? true;
274
    }
275
276
    /**
277
     * Sets a boolean flag that indicates whether hydrator classes should always be regenerated
278
     * during each script execution.
279
     *
280
     * @param bool|int $bool
281
     */
282
    public function setAutoGenerateHydratorClasses($bool)
283
    {
284
        $this->attributes['autoGenerateHydratorClasses'] = $bool;
285
    }
286
287
    /**
288
     * Gets the namespace where hydrator classes reside.
289 1580
     *
290
     * @return string
291 1580
     */
292
    public function getHydratorNamespace()
293
    {
294
        return $this->attributes['hydratorNamespace'] ?? null;
295
    }
296
297
    /**
298
     * Sets the namespace where hydrator classes reside.
299 1580
     *
300
     * @param string $ns
301 1580
     */
302 1580
    public function setHydratorNamespace($ns)
303
    {
304
        $this->attributes['hydratorNamespace'] = $ns;
305
    }
306
307
    /**
308
     * Sets the directory where Doctrine generates persistent collection class files.
309 1580
     *
310
     * @param string $dir
311 1580
     */
312 1580
    public function setPersistentCollectionDir($dir)
313
    {
314
        $this->attributes['persistentCollectionDir'] = $dir;
315
    }
316
317
    /**
318
     * Gets the directory where Doctrine generates persistent collection class files.
319 11
     *
320
     * @return string
321 11
     */
322
    public function getPersistentCollectionDir()
323
    {
324
        return $this->attributes['persistentCollectionDir'] ?? null;
325
    }
326
327
    /**
328
     * Gets a integer flag that indicates how and when persistent collection
329
     * classes should be generated.
330 7
     *
331
     * @return int Possible values are defined constants
332 7
     */
333
    public function getAutoGeneratePersistentCollectionClasses()
334
    {
335
        return $this->attributes['autoGeneratePersistentCollectionClasses'] ?? self::AUTOGENERATE_ALWAYS;
336
    }
337
338
    /**
339
     * Sets a integer flag that indicates how and when persistent collection
340
     * classes should be generated.
341
     *
342
     * @param int $mode Possible values are defined constants
343
     */
344
    public function setAutoGeneratePersistentCollectionClasses($mode)
345
    {
346
        $this->attributes['autoGeneratePersistentCollectionClasses'] = $mode;
347
    }
348
349
    /**
350
     * Gets the namespace where persistent collection classes reside.
351 11
     *
352
     * @return string
353 11
     */
354
    public function getPersistentCollectionNamespace()
355
    {
356
        return $this->attributes['persistentCollectionNamespace'] ?? null;
357
    }
358
359
    /**
360
     * Sets the namespace where persistent collection classes reside.
361 1580
     *
362
     * @param string $ns
363 1580
     */
364 1580
    public function setPersistentCollectionNamespace($ns)
365
    {
366
        $this->attributes['persistentCollectionNamespace'] = $ns;
367
    }
368
369
    /**
370
     * Sets the default DB to use for all Documents that do not specify
371
     * a database.
372 1580
     *
373
     * @param string $defaultDB
374 1580
     */
375 1580
    public function setDefaultDB($defaultDB)
376
    {
377
        $this->attributes['defaultDB'] = $defaultDB;
378
    }
379
380
    /**
381
     * Gets the default DB to use for all Documents that do not specify a database.
382 1248
     *
383
     * @return string $defaultDB
384 1248
     */
385
    public function getDefaultDB()
386
    {
387
        return $this->attributes['defaultDB'] ?? null;
388
    }
389
390
    /**
391
     * Set the class metadata factory class name.
392
     *
393
     * @param string $cmfName
394
     */
395
    public function setClassMetadataFactoryName($cmfName)
396
    {
397
        $this->attributes['classMetadataFactoryName'] = $cmfName;
398
    }
399
400
    /**
401
     * Gets the class metadata factory class name.
402 1580
     *
403
     * @return string
404 1580
     */
405 1580
    public function getClassMetadataFactoryName()
406
    {
407 1580
        if (! isset($this->attributes['classMetadataFactoryName'])) {
408
            $this->attributes['classMetadataFactoryName'] = ClassMetadataFactory::class;
409
        }
410
        return $this->attributes['classMetadataFactoryName'];
411
    }
412
413
    /**
414
     * Gets array of default commit options.
415 548
     *
416
     * @return array
417 548
     */
418
    public function getDefaultCommitOptions()
419
    {
420
        return $this->attributes['defaultCommitOptions'] ?? ['w' => 1];
421
    }
422
423
    /**
424
     * Sets array of default commit options.
425 1
     *
426
     * @param array $defaultCommitOptions
427 1
     */
428 1
    public function setDefaultCommitOptions($defaultCommitOptions)
429
    {
430
        $this->attributes['defaultCommitOptions'] = $defaultCommitOptions;
431
    }
432
433
    /**
434
     * Add a filter to the list of possible filters.
435
     *
436
     * @param string $name       The name of the filter.
437 1580
     * @param string $className  The class name of the filter.
438
     * @param array  $parameters The parameters for the filter.
439 1580
     */
440 1580
    public function addFilter($name, $className, array $parameters = [])
441 1580
    {
442
        $this->attributes['filters'][$name] = [
443 1580
            'class' => $className,
444
            'parameters' => $parameters,
445
        ];
446
    }
447
448
    /**
449
     * Gets the class name for a given filter name.
450
     *
451
     * @param string $name The name of the filter.
452 24
     *
453
     * @return string|null The filter class name, or null if it is undefined
454 24
     */
455 24
    public function getFilterClassName($name)
456 24
    {
457
        return isset($this->attributes['filters'][$name])
458
            ? $this->attributes['filters'][$name]['class']
459
            : null;
460
    }
461
462
    /**
463
     * Gets the parameters for a given filter name.
464
     *
465
     * @param string $name The name of the filter.
466 23
     *
467
     * @return array|null The filter parameters, or null if it is undefined
468 23
     */
469 23
    public function getFilterParameters($name)
470 23
    {
471
        return isset($this->attributes['filters'][$name])
472
            ? $this->attributes['filters'][$name]['parameters']
473
            : null;
474
    }
475
476
    /**
477
     * Sets default repository class for documents.
478
     *
479
     * @param string $className
480
     *
481
     * @throws MongoDBException If the class does not implement the ObjectRepository interface.
482
     */
483
    public function setDefaultDocumentRepositoryClassName($className)
484
    {
485
        $reflectionClass = new \ReflectionClass($className);
486
487
        if (! $reflectionClass->implementsInterface(ObjectRepository::class)) {
488
            throw MongoDBException::invalidDocumentRepository($className);
489
        }
490
491
        $this->attributes['defaultDocumentRepositoryClassName'] = $className;
492
    }
493
494
    /**
495
     * Get default repository class for documents.
496
     *
497 317
     * @return string
498
     */
499 317
    public function getDefaultDocumentRepositoryClassName()
500
    {
501
        return $this->attributes['defaultDocumentRepositoryClassName'] ?? DocumentRepository::class;
502
    }
503
504
    /**
505
     * Sets default repository class for GridFS files.
506 2
     *
507
     * @param string $className
508 2
     *
509 2
     * @throws MongoDBException If the class does not implement the GridFSRepository interface.
510
     */
511
    public function setDefaultGridFSRepositoryClassName($className)
512
    {
513
        $reflectionClass = new \ReflectionClass($className);
514
515
        if (! $reflectionClass->implementsInterface(GridFSRepository::class)) {
516 1580
            throw MongoDBException::invalidGridFSRepository($className);
517
        }
518 1580
519
        $this->attributes['defaultGridFSRepositoryClassName'] = $className;
520
    }
521
522
    /**
523
     * Get default repository class for GridFS files.
524
     *
525
     * @return string
526
     */
527
    public function getDefaultGridFSRepositoryClassName()
528
    {
529
        return $this->attributes['defaultGridFSRepositoryClassName'] ?? DefaultGridFSRepository::class;
530
    }
531
532
    /**
533
     * Set the document repository factory.
534
     *
535 387
     */
536
    public function setRepositoryFactory(RepositoryFactory $repositoryFactory)
537 387
    {
538 387
        $this->attributes['repositoryFactory'] = $repositoryFactory;
539
    }
540 387
541
    /**
542
     * Get the document repository factory.
543
     *
544
     * @return RepositoryFactory
545
     */
546
    public function getRepositoryFactory()
547
    {
548
        return $this->attributes['repositoryFactory'] ?? new DefaultRepositoryFactory();
549
    }
550
551
    /**
552
     * Set the persistent collection factory.
553
     *
554
     */
555
    public function setPersistentCollectionFactory(PersistentCollectionFactory $persistentCollectionFactory)
556
    {
557 8
        $this->attributes['persistentCollectionFactory'] = $persistentCollectionFactory;
558
    }
559 8
560 8
    /**
561 8
     * Get the persistent collection factory.
562 8
     *
563
     * @return DefaultPersistentCollectionFactory
564
     */
565 8
    public function getPersistentCollectionFactory()
566
    {
567
        if (! isset($this->attributes['persistentCollectionFactory'])) {
568
            $this->attributes['persistentCollectionFactory'] = new DefaultPersistentCollectionFactory();
569
        }
570
        return $this->attributes['persistentCollectionFactory'];
571
    }
572
573
    /**
574
     * Set the persistent collection generator.
575
     *
576
     */
577
    public function setPersistentCollectionGenerator(PersistentCollectionGenerator $persistentCollectionGenerator)
578
    {
579
        $this->attributes['persistentCollectionGenerator'] = $persistentCollectionGenerator;
580
    }
581
582
    /**
583
     * Get the persistent collection generator.
584
     *
585
     * @return DefaultPersistentCollectionGenerator
586
     */
587
    public function getPersistentCollectionGenerator()
588
    {
589
        if (! isset($this->attributes['persistentCollectionGenerator'])) {
590
            $this->attributes['persistentCollectionGenerator'] = new DefaultPersistentCollectionGenerator(
591
                $this->getPersistentCollectionDir(),
592
                $this->getPersistentCollectionNamespace()
593
            );
594
        }
595
        return $this->attributes['persistentCollectionGenerator'];
596
    }
597
}
598