PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

https://gitlab.com/freebird/WebApp
PHP | 202 lines | 164 code | 18 blank | 20 comment | 6 complexity | aea255defc1a3510f849b1e0c5bc3065 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\TwigBundle\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. * TwigExtension 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('twig');
  30. $rootNode
  31. ->children()
  32. ->scalarNode('exception_controller')->defaultValue('twig.controller.exception:showAction')->end()
  33. ->end()
  34. ;
  35. $this->addFormSection($rootNode);
  36. $this->addFormThemesSection($rootNode);
  37. $this->addGlobalsSection($rootNode);
  38. $this->addTwigOptions($rootNode);
  39. return $treeBuilder;
  40. }
  41. private function addFormSection(ArrayNodeDefinition $rootNode)
  42. {
  43. $rootNode
  44. ->validate()
  45. ->ifTrue(function ($v) {
  46. return count($v['form']['resources']) > 0;
  47. })
  48. ->then(function ($v) {
  49. $v['form_themes'] = array_values(array_unique(array_merge($v['form']['resources'], $v['form_themes'])));
  50. return $v;
  51. })
  52. ->end()
  53. ->children()
  54. ->arrayNode('form')
  55. ->info('Deprecated since 2.6, to be removed in 3.0. Use twig.form_themes instead')
  56. ->addDefaultsIfNotSet()
  57. ->fixXmlConfig('resource')
  58. ->children()
  59. ->arrayNode('resources')
  60. ->addDefaultChildrenIfNoneSet()
  61. ->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
  62. ->example(array('MyBundle::form.html.twig'))
  63. ->validate()
  64. ->ifNotInArray(array('form_div_layout.html.twig'))
  65. ->then(function ($v) {
  66. return array_merge(array('form_div_layout.html.twig'), $v);
  67. })
  68. ->end()
  69. ->end()
  70. ->end()
  71. ->end()
  72. ->end()
  73. ;
  74. }
  75. private function addFormThemesSection(ArrayNodeDefinition $rootNode)
  76. {
  77. $rootNode
  78. ->fixXmlConfig('form_theme')
  79. ->children()
  80. ->arrayNode('form_themes')
  81. ->addDefaultChildrenIfNoneSet()
  82. ->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
  83. ->example(array('MyBundle::form.html.twig'))
  84. ->validate()
  85. ->ifTrue(function ($v) { return !in_array('form_div_layout.html.twig', $v); })
  86. ->then(function ($v) {
  87. return array_merge(array('form_div_layout.html.twig'), $v);
  88. })
  89. ->end()
  90. ->end()
  91. ->end()
  92. ;
  93. }
  94. private function addGlobalsSection(ArrayNodeDefinition $rootNode)
  95. {
  96. $rootNode
  97. ->fixXmlConfig('global')
  98. ->children()
  99. ->arrayNode('globals')
  100. ->normalizeKeys(false)
  101. ->useAttributeAsKey('key')
  102. ->example(array('foo' => '"@bar"', 'pi' => 3.14))
  103. ->prototype('array')
  104. ->beforeNormalization()
  105. ->ifTrue(function ($v) { return is_string($v) && 0 === strpos($v, '@'); })
  106. ->then(function ($v) {
  107. if (0 === strpos($v, '@@')) {
  108. return substr($v, 1);
  109. }
  110. return array('id' => substr($v, 1), 'type' => 'service');
  111. })
  112. ->end()
  113. ->beforeNormalization()
  114. ->ifTrue(function ($v) {
  115. if (is_array($v)) {
  116. $keys = array_keys($v);
  117. sort($keys);
  118. return $keys !== array('id', 'type') && $keys !== array('value');
  119. }
  120. return true;
  121. })
  122. ->then(function ($v) { return array('value' => $v); })
  123. ->end()
  124. ->children()
  125. ->scalarNode('id')->end()
  126. ->scalarNode('type')
  127. ->validate()
  128. ->ifNotInArray(array('service'))
  129. ->thenInvalid('The %s type is not supported')
  130. ->end()
  131. ->end()
  132. ->variableNode('value')->end()
  133. ->end()
  134. ->end()
  135. ->end()
  136. ->end()
  137. ;
  138. }
  139. private function addTwigOptions(ArrayNodeDefinition $rootNode)
  140. {
  141. $rootNode
  142. ->fixXmlConfig('path')
  143. ->children()
  144. ->variableNode('autoescape')
  145. ->defaultValue(array('Symfony\Bundle\TwigBundle\TwigDefaultEscapingStrategy', 'guess'))
  146. ->end()
  147. ->scalarNode('autoescape_service')->defaultNull()->end()
  148. ->scalarNode('autoescape_service_method')->defaultNull()->end()
  149. ->scalarNode('base_template_class')->example('Twig_Template')->cannotBeEmpty()->end()
  150. ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
  151. ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
  152. ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
  153. ->booleanNode('strict_variables')->end()
  154. ->scalarNode('auto_reload')->end()
  155. ->integerNode('optimizations')->min(-1)->end()
  156. ->arrayNode('paths')
  157. ->normalizeKeys(false)
  158. ->useAttributeAsKey('paths')
  159. ->beforeNormalization()
  160. ->always()
  161. ->then(function ($paths) {
  162. $normalized = array();
  163. foreach ($paths as $path => $namespace) {
  164. if (is_array($namespace)) {
  165. // xml
  166. $path = $namespace['value'];
  167. $namespace = $namespace['namespace'];
  168. }
  169. // path within the default namespace
  170. if (ctype_digit((string) $path)) {
  171. $path = $namespace;
  172. $namespace = null;
  173. }
  174. $normalized[$path] = $namespace;
  175. }
  176. return $normalized;
  177. })
  178. ->end()
  179. ->prototype('variable')->end()
  180. ->end()
  181. ->end()
  182. ;
  183. }
  184. }