Completed
Push — master ( ce8c34...f078de )
by Andreas
49:52 queued 47:00
created

Configuration::setPersistentCollectionNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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