|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony framework. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * This source file is subject to the MIT license that is bundled |
|
9 * with this source code in the file LICENSE. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Bundle\FrameworkBundle\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 * FrameworkExtension configuration structure. |
|
20 * |
|
21 * @author Jeremy Mikola <jmikola@gmail.com> |
|
22 */ |
|
23 class Configuration implements ConfigurationInterface |
|
24 { |
|
25 private $debug; |
|
26 |
|
27 /** |
|
28 * Constructor |
|
29 * |
|
30 * @param Boolean $debug Whether to use the debug mode |
|
31 */ |
|
32 public function __construct($debug) |
|
33 { |
|
34 $this->debug = (Boolean) $debug; |
|
35 } |
|
36 |
|
37 /** |
|
38 * Generates the configuration tree builder. |
|
39 * |
|
40 * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder |
|
41 */ |
|
42 public function getConfigTreeBuilder() |
|
43 { |
|
44 $treeBuilder = new TreeBuilder(); |
|
45 $rootNode = $treeBuilder->root('framework'); |
|
46 |
|
47 $rootNode |
|
48 ->children() |
|
49 ->scalarNode('charset')->end() |
|
50 ->scalarNode('trust_proxy_headers')->defaultFalse()->end() |
|
51 ->scalarNode('secret')->isRequired()->end() |
|
52 ->scalarNode('ide')->defaultNull()->end() |
|
53 ->booleanNode('test')->end() |
|
54 ->end() |
|
55 ; |
|
56 |
|
57 $this->addFormSection($rootNode); |
|
58 $this->addEsiSection($rootNode); |
|
59 $this->addProfilerSection($rootNode); |
|
60 $this->addRouterSection($rootNode); |
|
61 $this->addSessionSection($rootNode); |
|
62 $this->addTemplatingSection($rootNode); |
|
63 $this->addTranslatorSection($rootNode); |
|
64 $this->addValidationSection($rootNode); |
|
65 $this->addAnnotationsSection($rootNode); |
|
66 |
|
67 return $treeBuilder; |
|
68 } |
|
69 |
|
70 private function addFormSection(ArrayNodeDefinition $rootNode) |
|
71 { |
|
72 $rootNode |
|
73 ->children() |
|
74 ->arrayNode('form') |
|
75 ->canBeUnset() |
|
76 ->treatNullLike(array('enabled' => true)) |
|
77 ->treatTrueLike(array('enabled' => true)) |
|
78 ->children() |
|
79 ->booleanNode('enabled')->defaultTrue()->end() |
|
80 ->end() |
|
81 ->end() |
|
82 ->arrayNode('csrf_protection') |
|
83 ->canBeUnset() |
|
84 ->treatNullLike(array('enabled' => true)) |
|
85 ->treatTrueLike(array('enabled' => true)) |
|
86 ->children() |
|
87 ->booleanNode('enabled')->defaultTrue()->end() |
|
88 ->scalarNode('field_name')->defaultValue('_token')->end() |
|
89 ->end() |
|
90 ->end() |
|
91 ->end() |
|
92 ; |
|
93 } |
|
94 |
|
95 private function addEsiSection(ArrayNodeDefinition $rootNode) |
|
96 { |
|
97 $rootNode |
|
98 ->children() |
|
99 ->arrayNode('esi') |
|
100 ->canBeUnset() |
|
101 ->treatNullLike(array('enabled' => true)) |
|
102 ->treatTrueLike(array('enabled' => true)) |
|
103 ->children() |
|
104 ->booleanNode('enabled')->defaultTrue()->end() |
|
105 ->end() |
|
106 ->end() |
|
107 ->end() |
|
108 ; |
|
109 } |
|
110 |
|
111 private function addProfilerSection(ArrayNodeDefinition $rootNode) |
|
112 { |
|
113 $rootNode |
|
114 ->children() |
|
115 ->arrayNode('profiler') |
|
116 ->canBeUnset() |
|
117 ->children() |
|
118 ->booleanNode('only_exceptions')->defaultFalse()->end() |
|
119 ->booleanNode('only_master_requests')->defaultFalse()->end() |
|
120 ->scalarNode('dsn')->defaultValue('sqlite:%kernel.cache_dir%/profiler.db')->end() |
|
121 ->scalarNode('username')->defaultValue('')->end() |
|
122 ->scalarNode('password')->defaultValue('')->end() |
|
123 ->scalarNode('lifetime')->defaultValue(86400)->end() |
|
124 ->arrayNode('matcher') |
|
125 ->canBeUnset() |
|
126 ->performNoDeepMerging() |
|
127 ->children() |
|
128 ->scalarNode('ip')->end() |
|
129 ->scalarNode('path')->end() |
|
130 ->scalarNode('service')->end() |
|
131 ->end() |
|
132 ->end() |
|
133 ->end() |
|
134 ->end() |
|
135 ->end() |
|
136 ; |
|
137 } |
|
138 |
|
139 private function addRouterSection(ArrayNodeDefinition $rootNode) |
|
140 { |
|
141 $rootNode |
|
142 ->children() |
|
143 ->arrayNode('router') |
|
144 ->canBeUnset() |
|
145 ->children() |
|
146 ->scalarNode('resource')->isRequired()->end() |
|
147 ->scalarNode('type')->end() |
|
148 ->scalarNode('http_port')->defaultValue(80)->end() |
|
149 ->scalarNode('https_port')->defaultValue(443)->end() |
|
150 ->end() |
|
151 ->end() |
|
152 ->end() |
|
153 ; |
|
154 } |
|
155 |
|
156 private function addSessionSection(ArrayNodeDefinition $rootNode) |
|
157 { |
|
158 $rootNode |
|
159 ->children() |
|
160 ->arrayNode('session') |
|
161 ->canBeUnset() |
|
162 ->children() |
|
163 ->booleanNode('auto_start')->defaultFalse()->end() |
|
164 ->scalarNode('default_locale')->defaultValue('en')->end() |
|
165 ->scalarNode('storage_id')->defaultValue('session.storage.native')->end() |
|
166 ->scalarNode('name')->end() |
|
167 ->scalarNode('lifetime')->end() |
|
168 ->scalarNode('path')->end() |
|
169 ->scalarNode('domain')->end() |
|
170 ->booleanNode('secure')->end() |
|
171 ->booleanNode('httponly')->end() |
|
172 ->end() |
|
173 ->end() |
|
174 ->end() |
|
175 ; |
|
176 } |
|
177 |
|
178 private function addTemplatingSection(ArrayNodeDefinition $rootNode) |
|
179 { |
|
180 $organizeUrls = function($urls) |
|
181 { |
|
182 $urls += array( |
|
183 'http' => array(), |
|
184 'ssl' => array(), |
|
185 ); |
|
186 |
|
187 foreach ($urls as $i => $url) { |
|
188 if (is_integer($i)) { |
|
189 if (0 === strpos($url, 'https://') || 0 === strpos($url, '//')) { |
|
190 $urls['http'][] = $urls['ssl'][] = $url; |
|
191 } else { |
|
192 $urls['http'][] = $url; |
|
193 } |
|
194 unset($urls[$i]); |
|
195 } |
|
196 } |
|
197 |
|
198 return $urls; |
|
199 }; |
|
200 |
|
201 $rootNode |
|
202 ->children() |
|
203 ->arrayNode('templating') |
|
204 ->canBeUnset() |
|
205 ->children() |
|
206 ->scalarNode('assets_version')->defaultValue(null)->end() |
|
207 ->scalarNode('assets_version_format')->defaultValue(null)->end() |
|
208 ->arrayNode('form') |
|
209 ->addDefaultsIfNotSet() |
|
210 ->fixXmlConfig('resource') |
|
211 ->children() |
|
212 ->arrayNode('resources') |
|
213 ->addDefaultsIfNotSet() |
|
214 ->defaultValue(array('FrameworkBundle:Form')) |
|
215 ->validate() |
|
216 ->ifTrue(function($v) {return !in_array('FrameworkBundle:Form', $v); }) |
|
217 ->then(function($v){ |
|
218 return array_merge(array('FrameworkBundle:Form'), $v); |
|
219 }) |
|
220 ->end() |
|
221 ->prototype('scalar')->end() |
|
222 ->end() |
|
223 ->end() |
|
224 ->end() |
|
225 ->end() |
|
226 ->fixXmlConfig('assets_base_url') |
|
227 ->children() |
|
228 ->arrayNode('assets_base_urls') |
|
229 ->addDefaultsIfNotSet() |
|
230 ->defaultValue(array('http' => array(), 'ssl' => array())) |
|
231 ->beforeNormalization() |
|
232 ->ifTrue(function($v) { return !is_array($v); }) |
|
233 ->then(function($v) { return array($v); }) |
|
234 ->end() |
|
235 ->beforeNormalization() |
|
236 ->always() |
|
237 ->then($organizeUrls) |
|
238 ->end() |
|
239 ->children() |
|
240 ->arrayNode('http') |
|
241 ->prototype('scalar')->end() |
|
242 ->end() |
|
243 ->arrayNode('ssl') |
|
244 ->prototype('scalar')->end() |
|
245 ->end() |
|
246 ->end() |
|
247 ->end() |
|
248 ->scalarNode('cache')->end() |
|
249 ->end() |
|
250 ->fixXmlConfig('engine') |
|
251 ->children() |
|
252 ->arrayNode('engines') |
|
253 ->isRequired() |
|
254 ->requiresAtLeastOneElement() |
|
255 ->beforeNormalization() |
|
256 ->ifTrue(function($v){ return !is_array($v); }) |
|
257 ->then(function($v){ return array($v); }) |
|
258 ->end() |
|
259 ->prototype('scalar')->end() |
|
260 ->end() |
|
261 ->end() |
|
262 ->fixXmlConfig('loader') |
|
263 ->children() |
|
264 ->arrayNode('loaders') |
|
265 ->beforeNormalization() |
|
266 ->ifTrue(function($v){ return !is_array($v); }) |
|
267 ->then(function($v){ return array($v); }) |
|
268 ->end() |
|
269 ->prototype('scalar')->end() |
|
270 ->end() |
|
271 ->end() |
|
272 ->fixXmlConfig('package') |
|
273 ->children() |
|
274 ->arrayNode('packages') |
|
275 ->useAttributeAsKey('name') |
|
276 ->prototype('array') |
|
277 ->fixXmlConfig('base_url') |
|
278 ->children() |
|
279 ->scalarNode('version')->defaultNull()->end() |
|
280 ->scalarNode('version_format')->defaultNull()->end() |
|
281 ->arrayNode('base_urls') |
|
282 ->addDefaultsIfNotSet() |
|
283 ->defaultValue(array('http' => array(), 'ssl' => array())) |
|
284 ->beforeNormalization() |
|
285 ->ifTrue(function($v) { return !is_array($v); }) |
|
286 ->then(function($v) { return array($v); }) |
|
287 ->end() |
|
288 ->beforeNormalization() |
|
289 ->always() |
|
290 ->then($organizeUrls) |
|
291 ->end() |
|
292 ->children() |
|
293 ->arrayNode('http') |
|
294 ->prototype('scalar')->end() |
|
295 ->end() |
|
296 ->arrayNode('ssl') |
|
297 ->prototype('scalar')->end() |
|
298 ->end() |
|
299 ->end() |
|
300 ->end() |
|
301 ->end() |
|
302 ->end() |
|
303 ->end() |
|
304 ->end() |
|
305 ->end() |
|
306 ->end() |
|
307 ; |
|
308 } |
|
309 |
|
310 private function addTranslatorSection(ArrayNodeDefinition $rootNode) |
|
311 { |
|
312 $rootNode |
|
313 ->children() |
|
314 ->arrayNode('translator') |
|
315 ->canBeUnset() |
|
316 ->treatNullLike(array('enabled' => true)) |
|
317 ->treatTrueLike(array('enabled' => true)) |
|
318 ->children() |
|
319 ->booleanNode('enabled')->defaultTrue()->end() |
|
320 ->scalarNode('fallback')->defaultValue('en')->end() |
|
321 ->end() |
|
322 ->end() |
|
323 ->end() |
|
324 ; |
|
325 } |
|
326 |
|
327 private function addValidationSection(ArrayNodeDefinition $rootNode) |
|
328 { |
|
329 $rootNode |
|
330 ->children() |
|
331 ->arrayNode('validation') |
|
332 ->canBeUnset() |
|
333 ->treatNullLike(array('enabled' => true)) |
|
334 ->treatTrueLike(array('enabled' => true)) |
|
335 ->children() |
|
336 ->booleanNode('enabled')->defaultTrue()->end() |
|
337 ->scalarNode('cache')->end() |
|
338 ->booleanNode('enable_annotations')->defaultFalse()->end() |
|
339 ->end() |
|
340 ->end() |
|
341 ->end() |
|
342 ; |
|
343 } |
|
344 |
|
345 private function addAnnotationsSection(ArrayNodeDefinition $rootNode) |
|
346 { |
|
347 $rootNode |
|
348 ->children() |
|
349 ->arrayNode('annotations') |
|
350 ->addDefaultsIfNotSet() |
|
351 ->children() |
|
352 ->scalarNode('cache')->defaultValue('file')->end() |
|
353 ->scalarNode('file_cache_dir')->defaultValue('%kernel.cache_dir%/annotations')->end() |
|
354 ->booleanNode('debug')->defaultValue($this->debug)->end() |
|
355 ->end() |
|
356 ->end() |
|
357 ->end() |
|
358 ; |
|
359 } |
|
360 } |