1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Storage Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\StorageBundle\DependencyInjection\Bundle; |
16
|
|
|
|
17
|
|
|
use SWP\Component\Storage\Bundle\BundleInterface; |
18
|
|
|
use SWP\Bundle\StorageBundle\Drivers; |
19
|
|
|
use SWP\Component\Storage\Exception\InvalidDriverException; |
20
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
21
|
|
|
use Symfony\Component\DependencyInjection\Container; |
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
23
|
|
|
use Symfony\Component\HttpKernel\Bundle\Bundle as BaseBundle; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Arnaud Langlade <[email protected]> |
27
|
|
|
* @author Gustavo Perdomo <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
abstract class Bundle extends BaseBundle implements BundleInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Default format of mapping files. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $mappingFormat = BundleInterface::MAPPING_YAML; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
1 |
|
public function build(ContainerBuilder $container) |
42
|
|
|
{ |
43
|
1 |
|
if (null !== $this->getModelClassNamespace()) { |
44
|
1 |
|
foreach ($this->getSupportedDrivers() as $driver) { |
45
|
1 |
|
list($compilerPassClassName, $compilerPassMethod) = $this->getMappingCompilerPassInfo($driver); |
46
|
1 |
|
if (class_exists($compilerPassClassName)) { |
47
|
1 |
|
if (!method_exists($compilerPassClassName, $compilerPassMethod)) { |
48
|
|
|
throw new InvalidConfigurationException( |
49
|
|
|
"The 'mappingFormat' value is invalid, must be 'xml', 'yml' or 'annotation'." |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
switch ($this->mappingFormat) { |
54
|
1 |
|
case BundleInterface::MAPPING_XML: |
55
|
1 |
View Code Duplication |
case BundleInterface::MAPPING_YAML: |
|
|
|
|
56
|
1 |
|
$container->addCompilerPass($compilerPassClassName::$compilerPassMethod( |
57
|
1 |
|
[$this->getConfigFilesPath($driver) => $this->getModelClassNamespace()], |
58
|
1 |
|
[sprintf('%s.persistence.manager_name', $this->getBundlePrefix())], |
59
|
1 |
|
sprintf('%s.backend_type_%s', $this->getBundlePrefix(), $driver) |
60
|
|
|
)); |
61
|
1 |
|
|
62
|
|
|
break; |
63
|
|
View Code Duplication |
case BundleInterface::MAPPING_ANNOTATION: |
|
|
|
|
64
|
|
|
$container->addCompilerPass($compilerPassClassName::$compilerPassMethod( |
65
|
|
|
[$this->getModelClassNamespace()], |
66
|
|
|
[$this->getConfigFilesPath($driver)], |
67
|
|
|
[sprintf('%s.persistence.manager_name', $this->getBundlePrefix())], |
68
|
|
|
sprintf('%s.backend_type_%s', $this->getBundlePrefix(), $driver) |
69
|
1 |
|
)); |
70
|
|
|
|
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return the prefix of the bundle. |
80
|
|
|
* |
81
|
1 |
|
* @return string |
82
|
|
|
*/ |
83
|
1 |
|
protected function getBundlePrefix() |
84
|
|
|
{ |
85
|
|
|
return Container::underscore(substr(strrchr(get_class($this), '\\'), 1, -6)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return the model namespace. |
90
|
|
|
* |
91
|
|
|
* @return string|null |
92
|
|
|
*/ |
93
|
|
|
protected function getModelClassNamespace() |
94
|
|
|
{ |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return mapping compiler pass class depending on driver. |
100
|
|
|
* |
101
|
|
|
* @param string $driverType |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
* |
105
|
1 |
|
* @throws InvalidDriverException |
106
|
|
|
*/ |
107
|
|
|
protected function getMappingCompilerPassInfo($driverType) |
108
|
1 |
|
{ |
109
|
|
|
switch ($driverType) { |
110
|
|
|
case Drivers::DRIVER_DOCTRINE_MONGODB_ODM: |
111
|
1 |
|
$mappingsPassClassName = 'Doctrine\\Bundle\\MongoDBBundle\\DependencyInjection\\Compiler\\DoctrineMongoDBMappingsPass'; |
112
|
1 |
|
|
113
|
1 |
|
break; |
114
|
1 |
|
case Drivers::DRIVER_DOCTRINE_ORM: |
115
|
1 |
|
$mappingsPassClassName = 'Doctrine\\Bundle\\DoctrineBundle\\DependencyInjection\\Compiler\\DoctrineOrmMappingsPass'; |
116
|
1 |
|
|
117
|
|
|
break; |
118
|
|
|
case Drivers::DRIVER_DOCTRINE_PHPCR_ODM: |
119
|
|
|
$mappingsPassClassName = 'Doctrine\\Bundle\\PHPCRBundle\\DependencyInjection\\Compiler\\DoctrinePhpcrMappingsPass'; |
120
|
|
|
|
121
|
1 |
|
break; |
122
|
|
|
default: |
123
|
1 |
|
throw new InvalidDriverException($driverType); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$compilerPassMethod = sprintf('create%sMappingDriver', ucfirst($this->mappingFormat)); |
127
|
|
|
|
128
|
|
|
return [$mappingsPassClassName, $compilerPassMethod]; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Return the absolute path where are stored the doctrine mapping. |
133
|
1 |
|
* |
134
|
|
|
* @param string $suffix |
135
|
1 |
|
* |
136
|
1 |
|
* @return string |
137
|
1 |
|
*/ |
138
|
|
|
protected function getConfigFilesPath($suffix) |
139
|
|
|
{ |
140
|
|
|
return sprintf( |
141
|
|
|
'%s/Resources/config/doctrine-%s', |
142
|
|
|
$this->getPath(), |
143
|
|
|
strtolower($suffix) |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.