PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

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

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