PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

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