|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Bundle\DoctrineBundle\DependencyInjection; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
|
|
15 |
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
|
16 |
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
|
17 |
|
|
|
18 |
/** |
|
|
19 |
* This class contains the configuration information for the bundle |
|
|
20 |
* |
|
|
21 |
* This information is solely responsible for how the different configuration |
|
|
22 |
* sections are normalized, and merged. |
|
|
23 |
* |
|
|
24 |
* @author Christophe Coevoet <stof@notk.org> |
|
|
25 |
*/ |
|
|
26 |
class Configuration implements ConfigurationInterface |
|
|
27 |
{ |
|
|
28 |
private $debug; |
|
|
29 |
|
|
|
30 |
/** |
|
|
31 |
* Constructor |
|
|
32 |
* |
|
|
33 |
* @param Boolean $debug Whether to use the debug mode |
|
|
34 |
*/ |
|
|
35 |
public function __construct($debug) |
|
|
36 |
{ |
|
|
37 |
$this->debug = (Boolean) $debug; |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
/** |
|
|
41 |
* Generates the configuration tree builder. |
|
|
42 |
* |
|
|
43 |
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder |
|
|
44 |
*/ |
|
|
45 |
public function getConfigTreeBuilder() |
|
|
46 |
{ |
|
|
47 |
$treeBuilder = new TreeBuilder(); |
|
|
48 |
$rootNode = $treeBuilder->root('doctrine'); |
|
|
49 |
|
|
|
50 |
$this->addDbalSection($rootNode); |
|
|
51 |
$this->addOrmSection($rootNode); |
|
|
52 |
|
|
|
53 |
return $treeBuilder; |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
private function addDbalSection(ArrayNodeDefinition $node) |
|
|
57 |
{ |
|
|
58 |
$node |
|
|
59 |
->children() |
|
|
60 |
->arrayNode('dbal') |
|
|
61 |
->beforeNormalization() |
|
|
62 |
->ifTrue(function ($v) { return is_array($v) && !array_key_exists('connections', $v) && !array_key_exists('connection', $v); }) |
|
|
63 |
->then(function ($v) { |
|
|
64 |
$connection = array(); |
|
|
65 |
foreach (array( |
|
|
66 |
'dbname', |
|
|
67 |
'host', |
|
|
68 |
'port', |
|
|
69 |
'user', |
|
|
70 |
'password', |
|
|
71 |
'driver', |
|
|
72 |
'driver_class', |
|
|
73 |
'options', |
|
|
74 |
'path', |
|
|
75 |
'memory', |
|
|
76 |
'unix_socket', |
|
|
77 |
'wrapper_class', |
|
|
78 |
'platform_service', |
|
|
79 |
'charset', |
|
|
80 |
'logging', |
|
|
81 |
'mapping_types', |
|
|
82 |
) as $key) { |
|
|
83 |
if (array_key_exists($key, $v)) { |
|
|
84 |
$connection[$key] = $v[$key]; |
|
|
85 |
unset($v[$key]); |
|
|
86 |
} |
|
|
87 |
} |
|
|
88 |
$v['default_connection'] = isset($v['default_connection']) ? (string) $v['default_connection'] : 'default'; |
|
|
89 |
$v['connections'] = array($v['default_connection'] => $connection); |
|
|
90 |
|
|
|
91 |
return $v; |
|
|
92 |
}) |
|
|
93 |
->end() |
|
|
94 |
->children() |
|
|
95 |
->scalarNode('default_connection')->end() |
|
|
96 |
->end() |
|
|
97 |
->fixXmlConfig('type') |
|
|
98 |
->children() |
|
|
99 |
->arrayNode('types') |
|
|
100 |
->useAttributeAsKey('name') |
|
|
101 |
->prototype('scalar')->end() |
|
|
102 |
->end() |
|
|
103 |
->end() |
|
|
104 |
->fixXmlConfig('connection') |
|
|
105 |
->append($this->getDbalConnectionsNode()) |
|
|
106 |
->end() |
|
|
107 |
; |
|
|
108 |
} |
|
|
109 |
|
|
|
110 |
private function getDbalConnectionsNode() |
|
|
111 |
{ |
|
|
112 |
$treeBuilder = new TreeBuilder(); |
|
|
113 |
$node = $treeBuilder->root('connections'); |
|
|
114 |
|
|
|
115 |
$node |
|
|
116 |
->requiresAtLeastOneElement() |
|
|
117 |
->useAttributeAsKey('name') |
|
|
118 |
->prototype('array') |
|
|
119 |
->fixXmlConfig('mapping_type') |
|
|
120 |
->children() |
|
|
121 |
->scalarNode('dbname')->end() |
|
|
122 |
->scalarNode('host')->defaultValue('localhost')->end() |
|
|
123 |
->scalarNode('port')->defaultNull()->end() |
|
|
124 |
->scalarNode('user')->defaultValue('root')->end() |
|
|
125 |
->scalarNode('password')->defaultNull()->end() |
|
|
126 |
->scalarNode('driver')->defaultValue('pdo_mysql')->end() |
|
|
127 |
->scalarNode('path')->end() |
|
|
128 |
->booleanNode('memory')->end() |
|
|
129 |
->scalarNode('unix_socket')->end() |
|
|
130 |
->scalarNode('platform_service')->end() |
|
|
131 |
->scalarNode('charset')->end() |
|
|
132 |
->booleanNode('logging')->defaultValue($this->debug)->end() |
|
|
133 |
->scalarNode('driver_class')->end() |
|
|
134 |
->scalarNode('wrapper_class')->end() |
|
|
135 |
->arrayNode('options') |
|
|
136 |
->useAttributeAsKey('key') |
|
|
137 |
->prototype('scalar')->end() |
|
|
138 |
->end() |
|
|
139 |
->arrayNode('mapping_types') |
|
|
140 |
->useAttributeAsKey('name') |
|
|
141 |
->prototype('scalar')->end() |
|
|
142 |
->end() |
|
|
143 |
->end() |
|
|
144 |
->end() |
|
|
145 |
; |
|
|
146 |
|
|
|
147 |
return $node; |
|
|
148 |
} |
|
|
149 |
|
|
|
150 |
private function addOrmSection(ArrayNodeDefinition $node) |
|
|
151 |
{ |
|
|
152 |
$node |
|
|
153 |
->children() |
|
|
154 |
->arrayNode('orm') |
|
|
155 |
->beforeNormalization() |
|
|
156 |
->ifTrue(function ($v) { return null === $v || (is_array($v) && !array_key_exists('entity_managers', $v) && !array_key_exists('entity_manager', $v)); }) |
|
|
157 |
->then(function ($v) { |
|
|
158 |
$v = (array) $v; |
|
|
159 |
$entityManager = array(); |
|
|
160 |
foreach (array( |
|
|
161 |
'result_cache_driver', 'result-cache-driver', |
|
|
162 |
'metadata_cache_driver', 'metadata-cache-driver', |
|
|
163 |
'query_cache_driver', 'query-cache-driver', |
|
|
164 |
'auto_mapping', 'auto-mapping', |
|
|
165 |
'mappings', 'mapping', |
|
|
166 |
'connection', 'dql' |
|
|
167 |
) as $key) { |
|
|
168 |
if (array_key_exists($key, $v)) { |
|
|
169 |
$entityManager[$key] = $v[$key]; |
|
|
170 |
unset($v[$key]); |
|
|
171 |
} |
|
|
172 |
} |
|
|
173 |
$v['default_entity_manager'] = isset($v['default_entity_manager']) ? (string) $v['default_entity_manager'] : 'default'; |
|
|
174 |
$v['entity_managers'] = array($v['default_entity_manager'] => $entityManager); |
|
|
175 |
|
|
|
176 |
return $v; |
|
|
177 |
}) |
|
|
178 |
->end() |
|
|
179 |
->children() |
|
|
180 |
->scalarNode('default_entity_manager')->end() |
|
|
181 |
->booleanNode('auto_generate_proxy_classes')->defaultFalse()->end() |
|
|
182 |
->scalarNode('proxy_dir')->defaultValue('%kernel.cache_dir%/doctrine/orm/Proxies')->end() |
|
|
183 |
->scalarNode('proxy_namespace')->defaultValue('Proxies')->end() |
|
|
184 |
->end() |
|
|
185 |
->fixXmlConfig('entity_manager') |
|
|
186 |
->append($this->getOrmEntityManagersNode()) |
|
|
187 |
->end() |
|
|
188 |
->end() |
|
|
189 |
; |
|
|
190 |
} |
|
|
191 |
|
|
|
192 |
private function getOrmEntityManagersNode() |
|
|
193 |
{ |
|
|
194 |
$treeBuilder = new TreeBuilder(); |
|
|
195 |
$node = $treeBuilder->root('entity_managers'); |
|
|
196 |
|
|
|
197 |
$node |
|
|
198 |
->requiresAtLeastOneElement() |
|
|
199 |
->useAttributeAsKey('name') |
|
|
200 |
->prototype('array') |
|
|
201 |
->addDefaultsIfNotSet() |
|
|
202 |
->append($this->getOrmCacheDriverNode('query_cache_driver')) |
|
|
203 |
->append($this->getOrmCacheDriverNode('metadata_cache_driver')) |
|
|
204 |
->append($this->getOrmCacheDriverNode('result_cache_driver')) |
|
|
205 |
->children() |
|
|
206 |
->scalarNode('connection')->end() |
|
|
207 |
->scalarNode('class_metadata_factory_name')->defaultValue('Doctrine\ORM\Mapping\ClassMetadataFactory')->end() |
|
|
208 |
->scalarNode('auto_mapping')->defaultFalse()->end() |
|
|
209 |
->end() |
|
|
210 |
->fixXmlConfig('hydrator') |
|
|
211 |
->children() |
|
|
212 |
->arrayNode('hydrators') |
|
|
213 |
->useAttributeAsKey('name') |
|
|
214 |
->prototype('scalar')->end() |
|
|
215 |
->end() |
|
|
216 |
->end() |
|
|
217 |
->fixXmlConfig('mapping') |
|
|
218 |
->children() |
|
|
219 |
->arrayNode('mappings') |
|
|
220 |
->useAttributeAsKey('name') |
|
|
221 |
->prototype('array') |
|
|
222 |
->beforeNormalization() |
|
|
223 |
->ifString() |
|
|
224 |
->then(function($v) { return array('type' => $v); }) |
|
|
225 |
->end() |
|
|
226 |
->treatNullLike(array()) |
|
|
227 |
->treatFalseLike(array('mapping' => false)) |
|
|
228 |
->performNoDeepMerging() |
|
|
229 |
->children() |
|
|
230 |
->scalarNode('mapping')->defaultValue(true)->end() |
|
|
231 |
->scalarNode('type')->end() |
|
|
232 |
->scalarNode('dir')->end() |
|
|
233 |
->scalarNode('alias')->end() |
|
|
234 |
->scalarNode('prefix')->end() |
|
|
235 |
->booleanNode('is_bundle')->end() |
|
|
236 |
->end() |
|
|
237 |
->end() |
|
|
238 |
->end() |
|
|
239 |
->arrayNode('dql') |
|
|
240 |
->fixXmlConfig('string_function') |
|
|
241 |
->fixXmlConfig('numeric_function') |
|
|
242 |
->fixXmlConfig('datetime_function') |
|
|
243 |
->children() |
|
|
244 |
->arrayNode('string_functions') |
|
|
245 |
->useAttributeAsKey('name') |
|
|
246 |
->prototype('scalar')->end() |
|
|
247 |
->end() |
|
|
248 |
->arrayNode('numeric_functions') |
|
|
249 |
->useAttributeAsKey('name') |
|
|
250 |
->prototype('scalar')->end() |
|
|
251 |
->end() |
|
|
252 |
->arrayNode('datetime_functions') |
|
|
253 |
->useAttributeAsKey('name') |
|
|
254 |
->prototype('scalar')->end() |
|
|
255 |
->end() |
|
|
256 |
->end() |
|
|
257 |
->end() |
|
|
258 |
->end() |
|
|
259 |
->end() |
|
|
260 |
; |
|
|
261 |
|
|
|
262 |
return $node; |
|
|
263 |
} |
|
|
264 |
|
|
|
265 |
private function getOrmCacheDriverNode($name) |
|
|
266 |
{ |
|
|
267 |
$treeBuilder = new TreeBuilder(); |
|
|
268 |
$node = $treeBuilder->root($name); |
|
|
269 |
|
|
|
270 |
$node |
|
|
271 |
->addDefaultsIfNotSet() |
|
|
272 |
->beforeNormalization() |
|
|
273 |
->ifString() |
|
|
274 |
->then(function($v) { return array('type' => $v); }) |
|
|
275 |
->end() |
|
|
276 |
->children() |
|
|
277 |
->scalarNode('type')->defaultValue('array')->isRequired()->end() |
|
|
278 |
->scalarNode('host')->end() |
|
|
279 |
->scalarNode('port')->end() |
|
|
280 |
->scalarNode('instance_class')->end() |
|
|
281 |
->scalarNode('class')->end() |
|
|
282 |
->end() |
|
|
283 |
; |
|
|
284 |
|
|
|
285 |
return $node; |
|
|
286 |
} |
|
|
287 |
} |