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

https://github.com/nattaphat/hgis · PHP · 159 lines · 124 code · 15 blank · 20 comment · 5 complexity · 5f0657d5d12912edc4a68173cade4f92 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 \Symfony\Component\Config\Definition\Builder\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->addGlobalsSection($rootNode);
  37. $this->addTwigOptions($rootNode);
  38. return $treeBuilder;
  39. }
  40. private function addFormSection(ArrayNodeDefinition $rootNode)
  41. {
  42. $rootNode
  43. ->children()
  44. ->arrayNode('form')
  45. ->addDefaultsIfNotSet()
  46. ->fixXmlConfig('resource')
  47. ->children()
  48. ->arrayNode('resources')
  49. ->addDefaultChildrenIfNoneSet()
  50. ->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
  51. ->example(array('MyBundle::form.html.twig'))
  52. ->validate()
  53. ->ifTrue(function($v) { return !in_array('form_div_layout.html.twig', $v); })
  54. ->then(function($v){
  55. return array_merge(array('form_div_layout.html.twig'), $v);
  56. })
  57. ->end()
  58. ->end()
  59. ->end()
  60. ->end()
  61. ->end()
  62. ;
  63. }
  64. private function addGlobalsSection(ArrayNodeDefinition $rootNode)
  65. {
  66. $rootNode
  67. ->fixXmlConfig('global')
  68. ->children()
  69. ->arrayNode('globals')
  70. ->normalizeKeys(false)
  71. ->useAttributeAsKey('key')
  72. ->example(array('foo' => '"@bar"', 'pi' => 3.14))
  73. ->prototype('array')
  74. ->beforeNormalization()
  75. ->ifTrue(function($v){ return is_string($v) && 0 === strpos($v, '@'); })
  76. ->then(function($v){ return array('id' => substr($v, 1), 'type' => 'service'); })
  77. ->end()
  78. ->beforeNormalization()
  79. ->ifTrue(function($v){
  80. if (is_array($v)) {
  81. $keys = array_keys($v);
  82. sort($keys);
  83. return $keys !== array('id', 'type') && $keys !== array('value');
  84. }
  85. return true;
  86. })
  87. ->then(function($v){ return array('value' => $v); })
  88. ->end()
  89. ->children()
  90. ->scalarNode('id')->end()
  91. ->scalarNode('type')
  92. ->validate()
  93. ->ifNotInArray(array('service'))
  94. ->thenInvalid('The %s type is not supported')
  95. ->end()
  96. ->end()
  97. ->variableNode('value')->end()
  98. ->end()
  99. ->end()
  100. ->end()
  101. ->end()
  102. ;
  103. }
  104. private function addTwigOptions(ArrayNodeDefinition $rootNode)
  105. {
  106. $rootNode
  107. ->fixXmlConfig('path')
  108. ->children()
  109. ->scalarNode('autoescape')->end()
  110. ->scalarNode('base_template_class')->example('Twig_Template')->end()
  111. ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
  112. ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
  113. ->scalarNode('debug')->defaultValue('%kernel.debug%')->end()
  114. ->scalarNode('strict_variables')->end()
  115. ->scalarNode('auto_reload')->end()
  116. ->scalarNode('optimizations')->end()
  117. ->arrayNode('paths')
  118. ->normalizeKeys(false)
  119. ->beforeNormalization()
  120. ->always()
  121. ->then(function ($paths) {
  122. $normalized = array();
  123. foreach ($paths as $path => $namespace) {
  124. if (is_array($namespace)) {
  125. // xml
  126. $path = $namespace['value'];
  127. $namespace = $namespace['namespace'];
  128. }
  129. // path within the default namespace
  130. if (ctype_digit((string) $path)) {
  131. $path = $namespace;
  132. $namespace = null;
  133. }
  134. $normalized[$path] = $namespace;
  135. }
  136. return $normalized;
  137. })
  138. ->end()
  139. ->prototype('variable')->end()
  140. ->end()
  141. ->end()
  142. ;
  143. }
  144. }