PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

https://github.com/stepanets/symfony
PHP | 398 lines | 359 code | 21 blank | 18 comment | 8 complexity | 9300ccc254b22accaf0e73814be2999e MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
  11. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  12. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  13. use Symfony\Component\Config\Definition\ConfigurationInterface;
  14. /**
  15. * FrameworkExtension configuration structure.
  16. *
  17. * @author Jeremy Mikola <jmikola@gmail.com>
  18. */
  19. class Configuration implements ConfigurationInterface
  20. {
  21. /**
  22. * Generates the configuration tree builder.
  23. *
  24. * @return TreeBuilder The tree builder
  25. */
  26. public function getConfigTreeBuilder()
  27. {
  28. $treeBuilder = new TreeBuilder();
  29. $rootNode = $treeBuilder->root('framework');
  30. $rootNode
  31. ->children()
  32. ->scalarNode('secret')->end()
  33. ->scalarNode('http_method_override')
  34. ->info("Set true to enable support for the '_method' request parameter to determine the intended HTTP method on POST requests.")
  35. ->defaultTrue()
  36. ->end()
  37. ->arrayNode('trusted_proxies')
  38. ->beforeNormalization()
  39. ->ifTrue(function($v) { return !is_array($v) && !is_null($v); })
  40. ->then(function($v) { return is_bool($v) ? array() : preg_split('/\s*,\s*/', $v); })
  41. ->end()
  42. ->prototype('scalar')
  43. ->validate()
  44. ->ifTrue(function($v) { return !empty($v) && !filter_var($v, FILTER_VALIDATE_IP); })
  45. ->thenInvalid('Invalid proxy IP "%s"')
  46. ->end()
  47. ->end()
  48. ->end()
  49. ->scalarNode('ide')->defaultNull()->end()
  50. ->booleanNode('test')->end()
  51. ->scalarNode('default_locale')->defaultValue('en')->end()
  52. ->end()
  53. ;
  54. $this->addFormSection($rootNode);
  55. $this->addEsiSection($rootNode);
  56. $this->addFragmentsSection($rootNode);
  57. $this->addProfilerSection($rootNode);
  58. $this->addRouterSection($rootNode);
  59. $this->addSessionSection($rootNode);
  60. $this->addTemplatingSection($rootNode);
  61. $this->addTranslatorSection($rootNode);
  62. $this->addValidationSection($rootNode);
  63. $this->addAnnotationsSection($rootNode);
  64. $this->addSerializerSection($rootNode);
  65. return $treeBuilder;
  66. }
  67. private function addFormSection(ArrayNodeDefinition $rootNode)
  68. {
  69. $rootNode
  70. ->children()
  71. ->arrayNode('form')
  72. ->info('form configuration')
  73. ->canBeEnabled()
  74. ->end()
  75. ->arrayNode('csrf_protection')
  76. ->canBeDisabled()
  77. ->children()
  78. ->scalarNode('field_name')->defaultValue('_token')->end()
  79. ->end()
  80. ->end()
  81. ->end()
  82. ;
  83. }
  84. private function addEsiSection(ArrayNodeDefinition $rootNode)
  85. {
  86. $rootNode
  87. ->children()
  88. ->arrayNode('esi')
  89. ->info('esi configuration')
  90. ->canBeEnabled()
  91. ->end()
  92. ->end()
  93. ;
  94. }
  95. private function addFragmentsSection(ArrayNodeDefinition $rootNode)
  96. {
  97. $rootNode
  98. ->children()
  99. ->arrayNode('fragments')
  100. ->info('fragments configuration')
  101. ->canBeEnabled()
  102. ->children()
  103. ->scalarNode('path')->defaultValue('/_fragment')->end()
  104. ->end()
  105. ->end()
  106. ->end()
  107. ;
  108. }
  109. private function addProfilerSection(ArrayNodeDefinition $rootNode)
  110. {
  111. $rootNode
  112. ->children()
  113. ->arrayNode('profiler')
  114. ->info('profiler configuration')
  115. ->canBeEnabled()
  116. ->children()
  117. ->booleanNode('only_exceptions')->defaultFalse()->end()
  118. ->booleanNode('only_master_requests')->defaultFalse()->end()
  119. ->scalarNode('dsn')->defaultValue('file:%kernel.cache_dir%/profiler')->end()
  120. ->scalarNode('username')->defaultValue('')->end()
  121. ->scalarNode('password')->defaultValue('')->end()
  122. ->scalarNode('lifetime')->defaultValue(86400)->end()
  123. ->arrayNode('matcher')
  124. ->canBeUnset()
  125. ->performNoDeepMerging()
  126. ->children()
  127. ->scalarNode('ip')->end()
  128. ->scalarNode('path')
  129. ->info('use the urldecoded format')
  130. ->example('^/path to resource/')
  131. ->end()
  132. ->scalarNode('service')->end()
  133. ->end()
  134. ->end()
  135. ->end()
  136. ->end()
  137. ->end()
  138. ;
  139. }
  140. private function addRouterSection(ArrayNodeDefinition $rootNode)
  141. {
  142. $rootNode
  143. ->children()
  144. ->arrayNode('router')
  145. ->info('router configuration')
  146. ->canBeUnset()
  147. ->children()
  148. ->scalarNode('resource')->isRequired()->end()
  149. ->scalarNode('type')->end()
  150. ->scalarNode('http_port')->defaultValue(80)->end()
  151. ->scalarNode('https_port')->defaultValue(443)->end()
  152. ->scalarNode('strict_requirements')
  153. ->info(
  154. "set to true to throw an exception when a parameter does not match the requirements\n".
  155. "set to false to disable exceptions when a parameter does not match the requirements (and return null instead)\n".
  156. "set to null to disable parameter checks against requirements\n".
  157. "'true' is the preferred configuration in development mode, while 'false' or 'null' might be preferred in production"
  158. )
  159. ->defaultTrue()
  160. ->end()
  161. ->end()
  162. ->end()
  163. ->end()
  164. ;
  165. }
  166. private function addSessionSection(ArrayNodeDefinition $rootNode)
  167. {
  168. $rootNode
  169. ->children()
  170. ->arrayNode('session')
  171. ->info('session configuration')
  172. ->canBeUnset()
  173. ->children()
  174. ->scalarNode('storage_id')->defaultValue('session.storage.native')->end()
  175. ->scalarNode('handler_id')->defaultValue('session.handler.native_file')->end()
  176. ->scalarNode('name')->end()
  177. ->scalarNode('cookie_lifetime')->end()
  178. ->scalarNode('cookie_path')->end()
  179. ->scalarNode('cookie_domain')->end()
  180. ->booleanNode('cookie_secure')->end()
  181. ->booleanNode('cookie_httponly')->end()
  182. ->scalarNode('gc_divisor')->end()
  183. ->scalarNode('gc_probability')->end()
  184. ->scalarNode('gc_maxlifetime')->end()
  185. ->scalarNode('save_path')->defaultValue('%kernel.cache_dir%/sessions')->end()
  186. ->end()
  187. ->end()
  188. ->end()
  189. ;
  190. }
  191. private function addTemplatingSection(ArrayNodeDefinition $rootNode)
  192. {
  193. $organizeUrls = function($urls) {
  194. $urls += array(
  195. 'http' => array(),
  196. 'ssl' => array(),
  197. );
  198. foreach ($urls as $i => $url) {
  199. if (is_integer($i)) {
  200. if (0 === strpos($url, 'https://') || 0 === strpos($url, '//')) {
  201. $urls['http'][] = $urls['ssl'][] = $url;
  202. } else {
  203. $urls['http'][] = $url;
  204. }
  205. unset($urls[$i]);
  206. }
  207. }
  208. return $urls;
  209. };
  210. $rootNode
  211. ->children()
  212. ->arrayNode('templating')
  213. ->info('templating configuration')
  214. ->canBeUnset()
  215. ->children()
  216. ->scalarNode('assets_version')->defaultValue(null)->end()
  217. ->scalarNode('assets_version_format')->defaultValue('%%s?%%s')->end()
  218. ->scalarNode('hinclude_default_template')->defaultNull()->end()
  219. ->arrayNode('form')
  220. ->addDefaultsIfNotSet()
  221. ->fixXmlConfig('resource')
  222. ->children()
  223. ->arrayNode('resources')
  224. ->addDefaultChildrenIfNoneSet()
  225. ->prototype('scalar')->defaultValue('FrameworkBundle:Form')->end()
  226. ->validate()
  227. ->ifTrue(function($v) {return !in_array('FrameworkBundle:Form', $v); })
  228. ->then(function($v){
  229. return array_merge(array('FrameworkBundle:Form'), $v);
  230. })
  231. ->end()
  232. ->end()
  233. ->end()
  234. ->end()
  235. ->end()
  236. ->fixXmlConfig('assets_base_url')
  237. ->children()
  238. ->arrayNode('assets_base_urls')
  239. ->performNoDeepMerging()
  240. ->addDefaultsIfNotSet()
  241. ->beforeNormalization()
  242. ->ifTrue(function($v) { return !is_array($v); })
  243. ->then(function($v) { return array($v); })
  244. ->end()
  245. ->beforeNormalization()
  246. ->always()
  247. ->then($organizeUrls)
  248. ->end()
  249. ->children()
  250. ->arrayNode('http')
  251. ->prototype('scalar')->end()
  252. ->end()
  253. ->arrayNode('ssl')
  254. ->prototype('scalar')->end()
  255. ->end()
  256. ->end()
  257. ->end()
  258. ->scalarNode('cache')->end()
  259. ->end()
  260. ->fixXmlConfig('engine')
  261. ->children()
  262. ->arrayNode('engines')
  263. ->example(array('twig'))
  264. ->isRequired()
  265. ->requiresAtLeastOneElement()
  266. ->beforeNormalization()
  267. ->ifTrue(function($v){ return !is_array($v); })
  268. ->then(function($v){ return array($v); })
  269. ->end()
  270. ->prototype('scalar')->end()
  271. ->end()
  272. ->end()
  273. ->fixXmlConfig('loader')
  274. ->children()
  275. ->arrayNode('loaders')
  276. ->beforeNormalization()
  277. ->ifTrue(function($v){ return !is_array($v); })
  278. ->then(function($v){ return array($v); })
  279. ->end()
  280. ->prototype('scalar')->end()
  281. ->end()
  282. ->end()
  283. ->fixXmlConfig('package')
  284. ->children()
  285. ->arrayNode('packages')
  286. ->useAttributeAsKey('name')
  287. ->prototype('array')
  288. ->fixXmlConfig('base_url')
  289. ->children()
  290. ->scalarNode('version')->defaultNull()->end()
  291. ->scalarNode('version_format')->defaultValue('%%s?%%s')->end()
  292. ->arrayNode('base_urls')
  293. ->performNoDeepMerging()
  294. ->addDefaultsIfNotSet()
  295. ->beforeNormalization()
  296. ->ifTrue(function($v) { return !is_array($v); })
  297. ->then(function($v) { return array($v); })
  298. ->end()
  299. ->beforeNormalization()
  300. ->always()
  301. ->then($organizeUrls)
  302. ->end()
  303. ->children()
  304. ->arrayNode('http')
  305. ->prototype('scalar')->end()
  306. ->end()
  307. ->arrayNode('ssl')
  308. ->prototype('scalar')->end()
  309. ->end()
  310. ->end()
  311. ->end()
  312. ->end()
  313. ->end()
  314. ->end()
  315. ->end()
  316. ->end()
  317. ->end()
  318. ;
  319. }
  320. private function addTranslatorSection(ArrayNodeDefinition $rootNode)
  321. {
  322. $rootNode
  323. ->children()
  324. ->arrayNode('translator')
  325. ->info('translator configuration')
  326. ->canBeEnabled()
  327. ->children()
  328. ->scalarNode('fallback')->defaultValue('en')->end()
  329. ->end()
  330. ->end()
  331. ->end()
  332. ;
  333. }
  334. private function addValidationSection(ArrayNodeDefinition $rootNode)
  335. {
  336. $rootNode
  337. ->children()
  338. ->arrayNode('validation')
  339. ->info('validation configuration')
  340. ->canBeEnabled()
  341. ->children()
  342. ->scalarNode('cache')->end()
  343. ->booleanNode('enable_annotations')->defaultFalse()->end()
  344. ->scalarNode('translation_domain')->defaultValue('validators')->end()
  345. ->end()
  346. ->end()
  347. ->end()
  348. ;
  349. }
  350. private function addAnnotationsSection(ArrayNodeDefinition $rootNode)
  351. {
  352. $rootNode
  353. ->children()
  354. ->arrayNode('annotations')
  355. ->info('annotation configuration')
  356. ->addDefaultsIfNotSet()
  357. ->children()
  358. ->scalarNode('cache')->defaultValue('file')->end()
  359. ->scalarNode('file_cache_dir')->defaultValue('%kernel.cache_dir%/annotations')->end()
  360. ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
  361. ->end()
  362. ->end()
  363. ->end()
  364. ;
  365. }
  366. private function addSerializerSection(ArrayNodeDefinition $rootNode)
  367. {
  368. $rootNode
  369. ->children()
  370. ->arrayNode('serializer')
  371. ->info('serializer configuration')
  372. ->canBeEnabled()
  373. ->end()
  374. ->end()
  375. ;
  376. }
  377. }