MongoDBBuilder::setUpSpecificConfigurations()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8177
c 0
b 0
f 0
cc 6
nc 24
nop 1
1
<?php
2
3
/*
4
 * doctrine-manager-builder (https://github.com/juliangut/doctrine-manager-builder).
5
 * Doctrine2 managers builder.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/doctrine-manager-builder
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Jgut\Doctrine\ManagerBuilder;
13
14
use Doctrine\Common\Annotations\AnnotationReader;
15
use Doctrine\Common\Proxy\AbstractProxyFactory;
16
use Doctrine\MongoDB\Connection;
17
use Doctrine\ODM\MongoDB\Configuration;
18
use Doctrine\ODM\MongoDB\DocumentManager;
19
use Doctrine\ODM\MongoDB\DocumentRepository;
20
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
21
use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver;
22
use Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver;
23
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
24
use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper;
25
use Doctrine\ODM\MongoDB\Types\Type;
26
use Symfony\Component\Console\Command\Command;
27
use Symfony\Component\Console\Helper\HelperSet;
28
29
/**
30
 * Doctrine MongoDB Document Manager builder.
31
 *
32
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
33
 */
34
class MongoDBBuilder extends AbstractManagerBuilder
35
{
36
    /**
37
     * Logger callable.
38
     *
39
     * @var callable
40
     */
41
    protected $loggerCallable;
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function getDefaultOptions()
47
    {
48
        return [
49
            'connection' => [], // Array or \Doctrine\MongoDB\Connection
50
            'proxies_namespace' => 'DoctrineMongoDBODMProxy',
51
            'metadata_cache_namespace' => 'DoctrineMongoDBODMMetadataCache',
52
            'default_repository_class' => DocumentRepository::class,
53
            'hydrators_namespace' => 'DoctrineMongoDBODMHydrator',
54
            'hydrators_auto_generation' => AbstractProxyFactory::AUTOGENERATE_NEVER,
55
            'persistent_collections_namespace' => 'DoctrineMongoDBODMPersistentCollection',
56
            'persistent_collections_auto_generation' => AbstractProxyFactory::AUTOGENERATE_NEVER,
57
        ];
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function wipe()
64
    {
65
        $this->manager = null;
66
        $this->mappingDriver = null;
67
        $this->metadataCacheDriver = null;
68
        $this->eventManager = null;
69
        $this->loggerCallable = null;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     *
75
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
76
     * @throws \InvalidArgumentException
77
     * @throws \RuntimeException
78
     * @throws \UnexpectedValueException
79
     *
80
     * @return DocumentManager
81
     */
82
    protected function buildManager()
83
    {
84
        $config = new Configuration;
85
86
        $this->setUpGeneralConfigurations($config);
87
        $this->setUpSpecificConfigurations($config);
88
89
        $eventManager = $this->getEventManager();
90
        if ($this->getEventSubscribers() !== null) {
91
            /* @var array $eventSubscribers */
92
            $eventSubscribers = $this->getEventSubscribers();
93
94
            foreach ($eventSubscribers as $eventSubscriber) {
95
                $eventManager->addEventSubscriber($eventSubscriber);
96
            }
97
        }
98
99
        return DocumentManager::create($this->getConnection($config), $config, $eventManager);
100
    }
101
102
    /**
103
     * Set up general manager configurations.
104
     *
105
     * @param Configuration $config
106
     */
107
    protected function setUpGeneralConfigurations(Configuration $config)
108
    {
109
        $this->setupAnnotationMetadata();
110
        $config->setMetadataDriverImpl($this->getMetadataMappingDriver());
111
112
        $config->setProxyDir($this->getProxiesPath());
113
        $config->setProxyNamespace($this->getProxiesNamespace());
114
        $config->setAutoGenerateProxyClasses($this->getProxiesAutoGeneration());
115
116
        if ($this->getRepositoryFactory() !== null) {
117
            $config->setRepositoryFactory($this->getRepositoryFactory());
118
        }
119
120
        if ($this->getDefaultRepositoryClass() !== null) {
121
            $config->setDefaultRepositoryClassName($this->getDefaultRepositoryClass());
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\ODM\MongoDB\Con...ltRepositoryClassName() has been deprecated with message: in version 1.2 and will be removed in 2.0. Please use setDefaultDocumentRepositoryClassName instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
122
        }
123
124
        $config->setMetadataCacheImpl($this->getMetadataCacheDriver());
125
    }
126
127
    /**
128
     * Set up manager specific configurations.
129
     *
130
     * @param Configuration $config
131
     */
132
    protected function setUpSpecificConfigurations(Configuration $config)
133
    {
134
        $config->setHydratorDir($this->getHydratorsPath());
135
        $config->setHydratorNamespace($this->getHydratorsNamespace());
136
        $config->setAutoGenerateHydratorClasses($this->getHydratorsAutoGeneration());
137
138
        $config->setPersistentCollectionDir($this->getPersistentCollectionPath());
139
        $config->setPersistentCollectionNamespace($this->getPersistentCollectionNamespace());
140
        $config->setAutoGeneratePersistentCollectionClasses($this->getAutoGeneratePersistentCollection());
141
142
        if ($this->getDefaultDatabase() !== null) {
143
            $config->setDefaultDB($this->getDefaultDatabase());
144
        }
145
146
        if ($this->getLoggerCallable() !== null) {
147
            $config->setLoggerCallable($this->getLoggerCallable());
148
        }
149
150
        foreach ($this->getCustomTypes() as $type => $class) {
151
            if (Type::hasType($type)) {
152
                Type::overrideType($type, $class);
153
            } else {
154
                Type::addType($type, $class);
155
            }
156
        }
157
158
        foreach ($this->getCustomFilters() as $name => $filterClass) {
159
            $config->addFilter($name, $filterClass);
160
        }
161
    }
162
163
    /**
164
     * Create MongoDB Connection.
165
     *
166
     * @param Configuration $config
167
     *
168
     * @throws \InvalidArgumentException
169
     * @throws \RuntimeException
170
     *
171
     * @return Connection
172
     */
173
    protected function getConnection(Configuration $config)
174
    {
175
        $connection = $this->getOption('connection');
176
177
        switch (true) {
178
            case is_array($connection):
179
                $connection = new Connection(
180
                    array_key_exists('server', $connection) ? $connection['server'] : null,
181
                    array_key_exists('options', $connection) ? $connection['options'] : [],
182
                    $config,
183
                    $this->getEventManager()
184
                );
185
                break;
186
187
            case $connection instanceof Connection:
188
                if ($connection->getEventManager() !== $this->getEventManager()) {
189
                    throw new \RuntimeException(
190
                        'Cannot use different EventManager instances for DocumentManager and Connection.'
191
                    );
192
                }
193
                break;
194
195
            default:
196
                throw new \InvalidArgumentException('Invalid argument: ' . $connection);
197
        }
198
199
        return $connection;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    protected function getAnnotationMappingDriver(array $paths)
206
    {
207
        return new AnnotationDriver(new AnnotationReader, $paths);
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    protected function getXmlMappingDriver(array $paths, $extension = null)
214
    {
215
        $extension = $extension ?: XmlDriver::DEFAULT_FILE_EXTENSION;
216
217
        return new XmlDriver($paths, $extension);
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    protected function getYamlMappingDriver(array $paths, $extension = null)
224
    {
225
        $extension = $extension ?: YamlDriver::DEFAULT_FILE_EXTENSION;
226
227
        return new YamlDriver($paths, $extension);
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     *
233
     * @throws \InvalidArgumentException
234
     *
235
     * @return RepositoryFactory|null
236
     */
237
    protected function getRepositoryFactory()
238
    {
239
        if (!array_key_exists('repository_factory', $this->options)) {
240
            return;
241
        }
242
243
        $repositoryFactory = $this->options['repository_factory'];
244
245
        if (!$repositoryFactory instanceof RepositoryFactory) {
246
            throw new \InvalidArgumentException(sprintf(
247
                'Invalid factory class "%s". It must be a Doctrine\ODM\MongoDB\Repository\RepositoryFactory.',
248
                get_class($repositoryFactory)
249
            ));
250
        }
251
252
        return $repositoryFactory;
253
    }
254
255
    /**
256
     * Retrieve hydrators path.
257
     *
258
     * @return string
259
     */
260
    protected function getHydratorsPath()
261
    {
262
        return (string) $this->getOption('hydrators_path', sys_get_temp_dir());
263
    }
264
265
    /**
266
     * Retrieve hydrators namespace.
267
     *
268
     * @return null|string
269
     */
270
    protected function getHydratorsNamespace()
271
    {
272
        $proxyNamespace = $this->getOption('hydrators_namespace');
273
274
        return is_string($proxyNamespace) ? $proxyNamespace : null;
275
    }
276
277
    /**
278
     * Retrieve hydrators generation strategy.
279
     *
280
     * @return int
281
     */
282
    protected function getHydratorsAutoGeneration()
283
    {
284
        return (int) $this->getOption('hydrators_auto_generation');
285
    }
286
287
    /**
288
     * Retrieve persistent collections path.
289
     *
290
     * @return string
291
     */
292
    protected function getPersistentCollectionPath()
293
    {
294
        return (string) $this->getOption('persistent_collections_path', sys_get_temp_dir());
295
    }
296
297
    /**
298
     * Retrieve persistent collections namespace.
299
     *
300
     * @return null|string
301
     */
302
    protected function getPersistentCollectionNamespace()
303
    {
304
        $collectionNamespace = $this->getOption('persistent_collections_namespace');
305
306
        return is_string($collectionNamespace) ? $collectionNamespace : null;
307
    }
308
309
    /**
310
     * Retrieve persistent collections generation strategy.
311
     *
312
     * @return int
313
     */
314
    protected function getAutoGeneratePersistentCollection()
315
    {
316
        return (int) $this->getOption('persistent_collections_auto_generation');
317
    }
318
319
    /**
320
     * Get default database.
321
     *
322
     * @return string|null
323
     */
324
    protected function getDefaultDatabase()
325
    {
326
        return $this->hasOption('default_database') ? (string) $this->getOption('default_database') : null;
327
    }
328
329
    /**
330
     * Retrieve logger callable.
331
     *
332
     * @return callable|null
333
     */
334
    protected function getLoggerCallable()
335
    {
336
        if (!is_callable($this->loggerCallable)) {
337
            $loggerCallable = $this->getOption('logger_callable');
338
339
            if (is_callable($loggerCallable)) {
340
                $this->loggerCallable = $loggerCallable;
341
            }
342
        }
343
344
        return $this->loggerCallable;
345
    }
346
347
    /**
348
     * Retrieve custom types.
349
     *
350
     * @return array
351
     */
352
    protected function getCustomTypes()
353
    {
354
        $types = (array) $this->getOption('custom_types');
355
356
        return array_filter(
357
            $types,
358
            function ($name) {
359
                return is_string($name);
360
            },
361
            ARRAY_FILTER_USE_KEY
362
        );
363
    }
364
365
    /**
366
     * Get custom registered filters.
367
     *
368
     * @return array
369
     */
370
    protected function getCustomFilters()
371
    {
372
        $filters = (array) $this->getOption('custom_filters');
373
374
        return array_filter(
375
            $filters,
376
            function ($name) {
377
                return is_string($name);
378
            },
379
            ARRAY_FILTER_USE_KEY
380
        );
381
    }
382
383
    /**
384
     * {@inheritdoc}
385
     *
386
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
387
     * @throws \InvalidArgumentException
388
     * @throws \RuntimeException
389
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
390
     * @throws \Symfony\Component\Console\Exception\LogicException
391
     * @throws \UnexpectedValueException
392
     *
393
     * @return Command[]
394
     */
395
    public function getConsoleCommands()
396
    {
397
        $commands = [
398
            // ODM
399
            new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateDocumentsCommand,
400
            new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand,
401
            new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand,
402
            new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateRepositoriesCommand,
403
            new \Doctrine\ODM\MongoDB\Tools\Console\Command\QueryCommand,
404
            new \Doctrine\ODM\MongoDB\Tools\Console\Command\ClearCache\MetadataCommand,
405
            new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\CreateCommand,
406
            new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\DropCommand,
407
            new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\UpdateCommand,
408
        ];
409
410
        if (class_exists('Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\ValidateCommand')) {
411
            $commands[] = new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\ValidateCommand();
412
        }
413
414
        $helperSet = $this->getConsoleHelperSet();
415
        $commandPrefix = (string) $this->getName();
416
417
        $commands = array_map(
418
            function (Command $command) use ($helperSet, $commandPrefix) {
419
                if ($commandPrefix !== '') {
420
                    $commandNames = array_map(
421
                        function ($commandName) use ($commandPrefix) {
422
                            return preg_replace('/^odm:/', 'odm:' . $commandPrefix . ':', $commandName);
423
                        },
424
                        array_merge([$command->getName()], $command->getAliases())
425
                    );
426
427
                    $command->setName(array_shift($commandNames));
428
                    $command->setAliases($commandNames);
429
                }
430
431
                $command->setHelperSet($helperSet);
432
433
                return $command;
434
            },
435
            $commands
436
        );
437
438
        return $commands;
439
    }
440
441
    /**
442
     * Get console helper set.
443
     *
444
     * @return \Symfony\Component\Console\Helper\HelperSet
445
     */
446
    protected function getConsoleHelperSet()
447
    {
448
        /* @var DocumentManager $documentManager */
449
        $documentManager = $this->getManager();
450
451
        return new HelperSet([
452
            'dm' => new DocumentManagerHelper($documentManager),
453
        ]);
454
    }
455
}
456