Completed
Pull Request — master (#1219)
by Maciej
09:25
created

Configuration::getClassMetadataFactoryName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

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