Completed
Pull Request — master (#1790)
by Andreas
17:42
created

setDefaultGridFSRepositoryClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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
     * @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
     */
136 1637
    public function setMetadataDriverImpl(MappingDriver $driverImpl)
137
    {
138 1637
        $this->attributes['metadataDriverImpl'] = $driverImpl;
139 1637
    }
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
     *
157
     * @return MappingDriver
158
     */
159 1376
    public function getMetadataDriverImpl()
160
    {
161 1376
        return $this->attributes['metadataDriverImpl'] ?? null;
162
    }
163
164
    /**
165
     * Gets the cache driver implementation that is used for metadata caching.
166
     *
167
     * @return Cache
168
     */
169 1591
    public function getMetadataCacheImpl()
170
    {
171 1591
        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
     *
186
     * @param string $dir
187
     */
188 1591
    public function setProxyDir($dir)
189
    {
190 1591
        $this->attributes['proxyDir'] = $dir;
191 1591
    }
192
193
    /**
194
     * Gets the directory where Doctrine generates any necessary proxy class files.
195
     *
196
     * @return string
197
     */
198 1591
    public function getProxyDir()
199
    {
200 1591
        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
     *
207
     * @return bool|int
208
     */
209 1591
    public function getAutoGenerateProxyClasses()
210
    {
211 1591
        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
     *
228
     * @return string
229
     */
230 1591
    public function getProxyNamespace()
231
    {
232 1591
        return $this->attributes['proxyNamespace'] ?? null;
233
    }
234
235
    /**
236
     * Sets the namespace where proxy classes reside.
237
     *
238
     * @param string $ns
239
     */
240 1591
    public function setProxyNamespace($ns)
241
    {
242 1591
        $this->attributes['proxyNamespace'] = $ns;
243 1591
    }
244
245
    /**
246
     * Sets the directory where Doctrine generates hydrator class files.
247
     *
248
     * @param string $dir
249
     */
250 1591
    public function setHydratorDir($dir)
251
    {
252 1591
        $this->attributes['hydratorDir'] = $dir;
253 1591
    }
254
255
    /**
256
     * Gets the directory where Doctrine generates hydrator class files.
257
     *
258
     * @return string
259
     */
260 1591
    public function getHydratorDir()
261
    {
262 1591
        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
     *
269
     * @return bool|int Possible values are defined constants
270
     */
271 1591
    public function getAutoGenerateHydratorClasses()
272
    {
273 1591
        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
     *
290
     * @return string
291
     */
292 1591
    public function getHydratorNamespace()
293
    {
294 1591
        return $this->attributes['hydratorNamespace'] ?? null;
295
    }
296
297
    /**
298
     * Sets the namespace where hydrator classes reside.
299
     *
300
     * @param string $ns
301
     */
302 1591
    public function setHydratorNamespace($ns)
303
    {
304 1591
        $this->attributes['hydratorNamespace'] = $ns;
305 1591
    }
306
307
    /**
308
     * Sets the directory where Doctrine generates persistent collection class files.
309
     *
310
     * @param string $dir
311
     */
312 1591
    public function setPersistentCollectionDir($dir)
313
    {
314 1591
        $this->attributes['persistentCollectionDir'] = $dir;
315 1591
    }
316
317
    /**
318
     * Gets the directory where Doctrine generates persistent collection class files.
319
     *
320
     * @return string
321
     */
322 11
    public function getPersistentCollectionDir()
323
    {
324 11
        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
     *
331
     * @return int Possible values are defined constants
332
     */
333 7
    public function getAutoGeneratePersistentCollectionClasses()
334
    {
335 7
        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
     *
352
     * @return string
353
     */
354 11
    public function getPersistentCollectionNamespace()
355
    {
356 11
        return $this->attributes['persistentCollectionNamespace'] ?? null;
357
    }
358
359
    /**
360
     * Sets the namespace where persistent collection classes reside.
361
     *
362
     * @param string $ns
363
     */
364 1591
    public function setPersistentCollectionNamespace($ns)
365
    {
366 1591
        $this->attributes['persistentCollectionNamespace'] = $ns;
367 1591
    }
368
369
    /**
370
     * Sets the default DB to use for all Documents that do not specify
371
     * a database.
372
     *
373
     * @param string $defaultDB
374
     */
375 1591
    public function setDefaultDB($defaultDB)
376
    {
377 1591
        $this->attributes['defaultDB'] = $defaultDB;
378 1591
    }
379
380
    /**
381
     * Gets the default DB to use for all Documents that do not specify a database.
382
     *
383
     * @return string $defaultDB
384
     */
385 1254
    public function getDefaultDB()
386
    {
387 1254
        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
     *
403
     * @return string
404
     */
405 1591
    public function getClassMetadataFactoryName()
406
    {
407 1591
        if (! isset($this->attributes['classMetadataFactoryName'])) {
408 1591
            $this->attributes['classMetadataFactoryName'] = ClassMetadataFactory::class;
409
        }
410 1591
        return $this->attributes['classMetadataFactoryName'];
411
    }
412
413
    /**
414
     * Gets array of default commit options.
415
     *
416
     * @return array
417
     */
418 549
    public function getDefaultCommitOptions()
419
    {
420 549
        return $this->attributes['defaultCommitOptions'] ?? ['w' => 1];
421
    }
422
423
    /**
424
     * Sets array of default commit options.
425
     *
426
     * @param array $defaultCommitOptions
427
     */
428 1
    public function setDefaultCommitOptions($defaultCommitOptions)
429
    {
430 1
        $this->attributes['defaultCommitOptions'] = $defaultCommitOptions;
431 1
    }
432
433
    /**
434
     * Add a filter to the list of possible filters.
435
     *
436
     * @param string $name       The name of the filter.
437
     * @param string $className  The class name of the filter.
438
     * @param array  $parameters The parameters for the filter.
439
     */
440 1591
    public function addFilter($name, $className, array $parameters = [])
441
    {
442 1591
        $this->attributes['filters'][$name] = [
443 1591
            'class' => $className,
444 1591
            'parameters' => $parameters,
445
        ];
446 1591
    }
447
448
    /**
449
     * Gets the class name for a given filter name.
450
     *
451
     * @param string $name The name of the filter.
452
     *
453
     * @return string|null The filter class name, or null if it is undefined
454
     */
455 24
    public function getFilterClassName($name)
456
    {
457 24
        return isset($this->attributes['filters'][$name])
458 24
            ? $this->attributes['filters'][$name]['class']
459 24
            : null;
460
    }
461
462
    /**
463
     * Gets the parameters for a given filter name.
464
     *
465
     * @param string $name The name of the filter.
466
     *
467
     * @return array|null The filter parameters, or null if it is undefined
468
     */
469 23
    public function getFilterParameters($name)
470
    {
471 23
        return isset($this->attributes['filters'][$name])
472 23
            ? $this->attributes['filters'][$name]['parameters']
473 23
            : 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
     * @return string
498
     */
499 317
    public function getDefaultDocumentRepositoryClassName()
500
    {
501 317
        return $this->attributes['defaultDocumentRepositoryClassName'] ?? DocumentRepository::class;
502
    }
503
504
    /**
505
     * Sets default repository class for GridFS files.
506
     *
507
     * @param string $className
508
     *
509
     * @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
            throw MongoDBException::invalidGridFSRepository($className);
517
        }
518
519
        $this->attributes['defaultGridFSRepositoryClassName'] = $className;
520
    }
521
522
    /**
523
     * Get default repository class for GridFS files.
524
     *
525
     * @return string
526
     */
527 7
    public function getDefaultGridFSRepositoryClassName()
528
    {
529 7
        return $this->attributes['defaultGridFSRepositoryClassName'] ?? DefaultGridFSRepository::class;
530
    }
531
532
    /**
533
     * Set the document repository factory.
534
     *
535
     */
536 2
    public function setRepositoryFactory(RepositoryFactory $repositoryFactory)
537
    {
538 2
        $this->attributes['repositoryFactory'] = $repositoryFactory;
539 2
    }
540
541
    /**
542
     * Get the document repository factory.
543
     *
544
     * @return RepositoryFactory
545
     */
546 1591
    public function getRepositoryFactory()
547
    {
548 1591
        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
        $this->attributes['persistentCollectionFactory'] = $persistentCollectionFactory;
558
    }
559
560
    /**
561
     * Get the persistent collection factory.
562
     *
563
     * @return DefaultPersistentCollectionFactory
564
     */
565 389
    public function getPersistentCollectionFactory()
566
    {
567 389
        if (! isset($this->attributes['persistentCollectionFactory'])) {
568 389
            $this->attributes['persistentCollectionFactory'] = new DefaultPersistentCollectionFactory();
569
        }
570 389
        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 8
    public function getPersistentCollectionGenerator()
588
    {
589 8
        if (! isset($this->attributes['persistentCollectionGenerator'])) {
590 8
            $this->attributes['persistentCollectionGenerator'] = new DefaultPersistentCollectionGenerator(
591 8
                $this->getPersistentCollectionDir(),
592 8
                $this->getPersistentCollectionNamespace()
593
            );
594
        }
595 8
        return $this->attributes['persistentCollectionGenerator'];
596
    }
597
}
598