Completed
Push — master ( 4b5952...b8f7dd )
by Andreas
11s
created

Configuration::getPersistentCollectionGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
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 InvalidArgumentException;
23
use ProxyManager\Configuration as ProxyManagerConfiguration;
24
use ProxyManager\Factory\LazyLoadingGhostFactory;
25
use ProxyManager\FileLocator\FileLocator;
26
use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy;
27
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
28
use ReflectionClass;
29
use function trim;
30
31
/**
32
 * Configuration class for the DocumentManager. When setting up your DocumentManager
33
 * you can optionally specify an instance of this class as the second argument.
34
 * If you do not pass a configuration object, a blank one will be created for you.
35
 *
36
 *     <?php
37
 *
38
 *     $config = new Configuration();
39
 *     $dm = DocumentManager::create(new Connection(), $config);
40
 */
41
class Configuration
42
{
43
    /**
44
     * Never autogenerate a proxy/hydrator/persistent collection and rely that
45
     * it was generated by some process before deployment. Copied from
46
     * \Doctrine\Common\Proxy\AbstractProxyFactory.
47
     */
48
    public const AUTOGENERATE_NEVER = 0;
49
50
    /**
51
     * Always generates a new proxy/hydrator/persistent collection in every request.
52
     *
53
     * This is only sane during development.
54
     * Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
55
     */
56
    public 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
    public const AUTOGENERATE_FILE_NOT_EXISTS = 2;
65
66
    /**
67
     * Generate the proxy/hydrator/persistent collection classes using eval().
68
     *
69
     * This strategy is only sane for development.
70
     * Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
71
     */
72
    public const AUTOGENERATE_EVAL = 3;
73
74
    /**
75
     * Array of attributes for this configuration instance.
76
     *
77
     * @var array
78
     */
79
    private $attributes = [];
80
81
    /** @var ProxyManagerConfiguration|null */
82
    private $proxyManagerConfiguration;
83
84
    /** @var int */
85
    private $autoGenerateProxyClasses = self::AUTOGENERATE_EVAL;
86
87 1697
    public function __construct()
88
    {
89 1697
        $this->proxyManagerConfiguration = new ProxyManagerConfiguration();
90 1697
        $this->setAutoGenerateProxyClasses(self::AUTOGENERATE_FILE_NOT_EXISTS);
91 1697
    }
92
93
    /**
94
     * Adds a namespace under a certain alias.
95
     */
96
    public function addDocumentNamespace(string $alias, string $namespace) : void
97
    {
98
        $this->attributes['documentNamespaces'][$alias] = $namespace;
99
    }
100
101
    /**
102
     * Resolves a registered namespace alias to the full namespace.
103
     *
104
     * @throws MongoDBException
105
     */
106
    public function getDocumentNamespace(string $documentNamespaceAlias) : string
107
    {
108
        if (! isset($this->attributes['documentNamespaces'][$documentNamespaceAlias])) {
109
            throw MongoDBException::unknownDocumentNamespace($documentNamespaceAlias);
110
        }
111
112
        return trim($this->attributes['documentNamespaces'][$documentNamespaceAlias], '\\');
113
    }
114
115
    /**
116
     * Retrieves the list of registered document namespace aliases.
117
     */
118
    public function getDocumentNamespaces() : array
119
    {
120
        return $this->attributes['documentNamespaces'];
121
    }
122
123
    /**
124
     * Set the document alias map
125
     */
126
    public function setDocumentNamespaces(array $documentNamespaces) : void
127
    {
128
        $this->attributes['documentNamespaces'] = $documentNamespaces;
129
    }
130
131
    /**
132
     * Sets the cache driver implementation that is used for metadata caching.
133
     *
134
     * @todo Force parameter to be a Closure to ensure lazy evaluation
135
     *       (as soon as a metadata cache is in effect, the driver never needs to initialize).
136
     */
137 1697
    public function setMetadataDriverImpl(MappingDriver $driverImpl) : void
138
    {
139 1697
        $this->attributes['metadataDriverImpl'] = $driverImpl;
140 1697
    }
141
142
    /**
143
     * Add a new default annotation driver with a correctly configured annotation reader.
144
     */
145
    public function newDefaultAnnotationDriver(array $paths = []) : AnnotationDriver
146
    {
147
        $reader = new AnnotationReader();
148
149
        return new AnnotationDriver($reader, $paths);
150
    }
151
152
    /**
153
     * Gets the cache driver implementation that is used for the mapping metadata.
154
     */
155 1432
    public function getMetadataDriverImpl() : ?MappingDriver
156
    {
157 1432
        return $this->attributes['metadataDriverImpl'] ?? null;
158
    }
159
160 1634
    public function getMetadataCacheImpl() : ?Cache
161
    {
162 1634
        return $this->attributes['metadataCacheImpl'] ?? null;
163
    }
164
165
    public function setMetadataCacheImpl(Cache $cacheImpl) : void
166
    {
167
        $this->attributes['metadataCacheImpl'] = $cacheImpl;
168
    }
169
170
    /**
171
     * Sets the directory where Doctrine generates any necessary proxy class files.
172
     */
173 1634
    public function setProxyDir(string $dir) : void
174
    {
175 1634
        $this->getProxyManagerConfiguration()->setProxiesTargetDir($dir);
176
177
        // Recreate proxy generator to ensure its path was updated
178 1634
        if ($this->autoGenerateProxyClasses !== self::AUTOGENERATE_FILE_NOT_EXISTS) {
179
            return;
180
        }
181
182 1634
        $this->setAutoGenerateProxyClasses($this->autoGenerateProxyClasses);
183 1634
    }
184
185
    /**
186
     * Gets the directory where Doctrine generates any necessary proxy class files.
187
     */
188
    public function getProxyDir() : ?string
189
    {
190
        return $this->getProxyManagerConfiguration()->getProxiesTargetDir();
191
    }
192
193
    /**
194
     * Gets an int flag that indicates whether proxy classes should always be regenerated
195
     * during each script execution.
196
     */
197
    public function getAutoGenerateProxyClasses() : int
198
    {
199
        return $this->autoGenerateProxyClasses;
200
    }
201
202
    /**
203
     * Sets an int flag that indicates whether proxy classes should always be regenerated
204
     * during each script execution.
205
     *
206
     * @throws InvalidArgumentException If an invalid mode was given.
207
     */
208 1697
    public function setAutoGenerateProxyClasses(int $mode) : void
209
    {
210 1697
        $this->autoGenerateProxyClasses = $mode;
211 1697
        $proxyManagerConfig             = $this->getProxyManagerConfiguration();
212
213
        switch ($mode) {
214 1697
            case self::AUTOGENERATE_FILE_NOT_EXISTS:
215 1697
                $proxyManagerConfig->setGeneratorStrategy(new FileWriterGeneratorStrategy(
216 1697
                    new FileLocator($proxyManagerConfig->getProxiesTargetDir())
217
                ));
218
219 1697
                break;
220
            case self::AUTOGENERATE_EVAL:
221
                $proxyManagerConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
222
223
                break;
224
            default:
225
                throw new InvalidArgumentException('Invalid proxy generation strategy given - only AUTOGENERATE_FILE_NOT_EXISTS and AUTOGENERATE_EVAL are supported.');
226
        }
227 1697
    }
228
229
    public function getProxyNamespace() : ?string
230
    {
231
        return $this->getProxyManagerConfiguration()->getProxiesNamespace();
232
    }
233
234 1634
    public function setProxyNamespace(string $ns) : void
235
    {
236 1634
        $this->getProxyManagerConfiguration()->setProxiesNamespace($ns);
237 1634
    }
238
239 1634
    public function setHydratorDir(string $dir) : void
240
    {
241 1634
        $this->attributes['hydratorDir'] = $dir;
242 1634
    }
243
244 1634
    public function getHydratorDir() : ?string
245
    {
246 1634
        return $this->attributes['hydratorDir'] ?? null;
247
    }
248
249
    /**
250
     * Gets an int flag that indicates whether hydrator classes should always be regenerated
251
     * during each script execution.
252
     */
253 1634
    public function getAutoGenerateHydratorClasses() : int
254
    {
255 1634
        return $this->attributes['autoGenerateHydratorClasses'] ?? self::AUTOGENERATE_ALWAYS;
256
    }
257
258
    /**
259
     * Sets an int flag that indicates whether hydrator classes should always be regenerated
260
     * during each script execution.
261
     */
262
    public function setAutoGenerateHydratorClasses(int $mode) : void
263
    {
264
        $this->attributes['autoGenerateHydratorClasses'] = $mode;
265
    }
266
267 1634
    public function getHydratorNamespace() : ?string
268
    {
269 1634
        return $this->attributes['hydratorNamespace'] ?? null;
270
    }
271
272 1634
    public function setHydratorNamespace(string $ns) : void
273
    {
274 1634
        $this->attributes['hydratorNamespace'] = $ns;
275 1634
    }
276
277 1634
    public function setPersistentCollectionDir(string $dir) : void
278
    {
279 1634
        $this->attributes['persistentCollectionDir'] = $dir;
280 1634
    }
281
282 11
    public function getPersistentCollectionDir() : ?string
283
    {
284 11
        return $this->attributes['persistentCollectionDir'] ?? null;
285
    }
286
287
    /**
288
     * Gets a integer flag that indicates how and when persistent collection
289
     * classes should be generated.
290
     */
291 7
    public function getAutoGeneratePersistentCollectionClasses() : int
292
    {
293 7
        return $this->attributes['autoGeneratePersistentCollectionClasses'] ?? self::AUTOGENERATE_ALWAYS;
294
    }
295
296
    /**
297
     * Sets a integer flag that indicates how and when persistent collection
298
     * classes should be generated.
299
     */
300
    public function setAutoGeneratePersistentCollectionClasses(int $mode) : void
301
    {
302
        $this->attributes['autoGeneratePersistentCollectionClasses'] = $mode;
303
    }
304
305 11
    public function getPersistentCollectionNamespace() : ?string
306
    {
307 11
        return $this->attributes['persistentCollectionNamespace'] ?? null;
308
    }
309
310 1634
    public function setPersistentCollectionNamespace(string $ns) : void
311
    {
312 1634
        $this->attributes['persistentCollectionNamespace'] = $ns;
313 1634
    }
314
315
    /**
316
     * Sets the default DB to use for all Documents that do not specify
317
     * a database.
318
     */
319 1634
    public function setDefaultDB(string $defaultDB) : void
320
    {
321 1634
        $this->attributes['defaultDB'] = $defaultDB;
322 1634
    }
323
324
    /**
325
     * Gets the default DB to use for all Documents that do not specify a database.
326
     */
327 1293
    public function getDefaultDB() : ?string
328
    {
329 1293
        return $this->attributes['defaultDB'] ?? null;
330
    }
331
332
    public function setClassMetadataFactoryName(string $cmfName) : void
333
    {
334
        $this->attributes['classMetadataFactoryName'] = $cmfName;
335
    }
336
337 1634
    public function getClassMetadataFactoryName() : string
338
    {
339 1634
        if (! isset($this->attributes['classMetadataFactoryName'])) {
340 1634
            $this->attributes['classMetadataFactoryName'] = ClassMetadataFactory::class;
341
        }
342 1634
        return $this->attributes['classMetadataFactoryName'];
343
    }
344
345 583
    public function getDefaultCommitOptions() : array
346
    {
347 583
        return $this->attributes['defaultCommitOptions'] ?? ['w' => 1];
348
    }
349
350 1
    public function setDefaultCommitOptions(array $defaultCommitOptions) : void
351
    {
352 1
        $this->attributes['defaultCommitOptions'] = $defaultCommitOptions;
353 1
    }
354
355
    /**
356
     * Add a filter to the list of possible filters.
357
     */
358 1634
    public function addFilter(string $name, string $className, array $parameters = []) : void
359
    {
360 1634
        $this->attributes['filters'][$name] = [
361 1634
            'class' => $className,
362 1634
            'parameters' => $parameters,
363
        ];
364 1634
    }
365
366 24
    public function getFilterClassName(string $name) : ?string
367
    {
368 24
        return isset($this->attributes['filters'][$name])
369 24
            ? $this->attributes['filters'][$name]['class']
370 24
            : null;
371
    }
372
373 23
    public function getFilterParameters(string $name) : ?array
374
    {
375 23
        return isset($this->attributes['filters'][$name])
376 23
            ? $this->attributes['filters'][$name]['parameters']
377 23
            : null;
378
    }
379
380
    /**
381
     * @throws MongoDBException If not is a ObjectRepository.
382
     */
383
    public function setDefaultDocumentRepositoryClassName(string $className) : void
384
    {
385
        $reflectionClass = new ReflectionClass($className);
386
387
        if (! $reflectionClass->implementsInterface(ObjectRepository::class)) {
388
            throw MongoDBException::invalidDocumentRepository($className);
389
        }
390
391
        $this->attributes['defaultDocumentRepositoryClassName'] = $className;
392
    }
393
394 339
    public function getDefaultDocumentRepositoryClassName() : string
395
    {
396 339
        return $this->attributes['defaultDocumentRepositoryClassName'] ?? DocumentRepository::class;
397
    }
398
399
    /**
400
     * @throws MongoDBException If the class does not implement the GridFSRepository interface.
401
     */
402
    public function setDefaultGridFSRepositoryClassName(string $className) : void
403
    {
404
        $reflectionClass = new ReflectionClass($className);
405
406
        if (! $reflectionClass->implementsInterface(GridFSRepository::class)) {
407
            throw MongoDBException::invalidGridFSRepository($className);
408
        }
409
410
        $this->attributes['defaultGridFSRepositoryClassName'] = $className;
411
    }
412
413 10
    public function getDefaultGridFSRepositoryClassName() : string
414
    {
415 10
        return $this->attributes['defaultGridFSRepositoryClassName'] ?? DefaultGridFSRepository::class;
416
    }
417
418 2
    public function setRepositoryFactory(RepositoryFactory $repositoryFactory) : void
419
    {
420 2
        $this->attributes['repositoryFactory'] = $repositoryFactory;
421 2
    }
422
423 1634
    public function getRepositoryFactory() : RepositoryFactory
424
    {
425 1634
        return $this->attributes['repositoryFactory'] ?? new DefaultRepositoryFactory();
426
    }
427
428
    public function setPersistentCollectionFactory(PersistentCollectionFactory $persistentCollectionFactory) : void
429
    {
430
        $this->attributes['persistentCollectionFactory'] = $persistentCollectionFactory;
431
    }
432
433 414
    public function getPersistentCollectionFactory() : PersistentCollectionFactory
434
    {
435 414
        if (! isset($this->attributes['persistentCollectionFactory'])) {
436 414
            $this->attributes['persistentCollectionFactory'] = new DefaultPersistentCollectionFactory();
437
        }
438 414
        return $this->attributes['persistentCollectionFactory'];
439
    }
440
441
    public function setPersistentCollectionGenerator(PersistentCollectionGenerator $persistentCollectionGenerator) : void
442
    {
443
        $this->attributes['persistentCollectionGenerator'] = $persistentCollectionGenerator;
444
    }
445
446 8
    public function getPersistentCollectionGenerator() : PersistentCollectionGenerator
447
    {
448 8
        if (! isset($this->attributes['persistentCollectionGenerator'])) {
449 8
            $this->attributes['persistentCollectionGenerator'] = new DefaultPersistentCollectionGenerator(
450 8
                $this->getPersistentCollectionDir(),
451 8
                $this->getPersistentCollectionNamespace()
452
            );
453
        }
454 8
        return $this->attributes['persistentCollectionGenerator'];
455
    }
456
457 1634
    public function buildGhostObjectFactory() : LazyLoadingGhostFactory
458
    {
459 1634
        return new LazyLoadingGhostFactory(clone $this->getProxyManagerConfiguration());
460
    }
461
462 1697
    public function getProxyManagerConfiguration() : ProxyManagerConfiguration
463
    {
464 1697
        return $this->proxyManagerConfiguration;
465
    }
466
}
467