Completed
Pull Request — master (#1219)
by Maciej
10:06
created

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