Configuration.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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->addFormThemesSection($rootNode);
  36. $this->addGlobalsSection($rootNode);
  37. $this->addTwigOptions($rootNode);
  38. $this->addTwigFormatOptions($rootNode);
  39. return $treeBuilder;
  40. }
  41. private function addFormThemesSection(ArrayNodeDefinition $rootNode)
  42. {
  43. $rootNode
  44. ->fixXmlConfig('form_theme')
  45. ->children()
  46. ->arrayNode('form_themes')
  47. ->addDefaultChildrenIfNoneSet()
  48. ->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
  49. ->example(array('MyBundle::form.html.twig'))
  50. ->validate()
  51. ->ifTrue(function ($v) { return !in_array('form_div_layout.html.twig', $v); })
  52. ->then(function ($v) {
  53. return array_merge(array('form_div_layout.html.twig'), $v);
  54. })
  55. ->end()
  56. ->end()
  57. ->end()
  58. ;
  59. }
  60. private function addGlobalsSection(ArrayNodeDefinition $rootNode)
  61. {
  62. $rootNode
  63. ->fixXmlConfig('global')
  64. ->children()
  65. ->arrayNode('globals')
  66. ->normalizeKeys(false)
  67. ->useAttributeAsKey('key')
  68. ->example(array('foo' => '"@bar"', 'pi' => 3.14))
  69. ->prototype('array')
  70. ->beforeNormalization()
  71. ->ifTrue(function ($v) { return is_string($v) && 0 === strpos($v, '@'); })
  72. ->then(function ($v) {
  73. if (0 === strpos($v, '@@')) {
  74. return substr($v, 1);
  75. }
  76. return array('id' => substr($v, 1), 'type' => 'service');
  77. })
  78. ->end()
  79. ->beforeNormalization()
  80. ->ifTrue(function ($v) {
  81. if (is_array($v)) {
  82. $keys = array_keys($v);
  83. sort($keys);
  84. return $keys !== array('id', 'type') && $keys !== array('value');
  85. }
  86. return true;
  87. })
  88. ->then(function ($v) { return array('value' => $v); })
  89. ->end()
  90. ->children()
  91. ->scalarNode('id')->end()
  92. ->scalarNode('type')
  93. ->validate()
  94. ->ifNotInArray(array('service'))
  95. ->thenInvalid('The %s type is not supported')
  96. ->end()
  97. ->end()
  98. ->variableNode('value')->end()
  99. ->end()
  100. ->end()
  101. ->end()
  102. ->end()
  103. ;
  104. }
  105. private function addTwigOptions(ArrayNodeDefinition $rootNode)
  106. {
  107. $rootNode
  108. ->fixXmlConfig('path')
  109. ->children()
  110. ->variableNode('autoescape')->defaultValue('name')->end()
  111. ->scalarNode('autoescape_service')->defaultNull()->end()
  112. ->scalarNode('autoescape_service_method')->defaultNull()->end()
  113. ->scalarNode('base_template_class')->example('Twig_Template')->cannotBeEmpty()->end()
  114. ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
  115. ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
  116. ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
  117. ->booleanNode('strict_variables')->end()
  118. ->scalarNode('auto_reload')->end()
  119. ->integerNode('optimizations')->min(-1)->end()
  120. ->arrayNode('paths')
  121. ->normalizeKeys(false)
  122. ->useAttributeAsKey('paths')
  123. ->beforeNormalization()
  124. ->always()
  125. ->then(function ($paths) {
  126. $normalized = array();
  127. foreach ($paths as $path => $namespace) {
  128. if (is_array($namespace)) {
  129. // xml
  130. $path = $namespace['value'];
  131. $namespace = $namespace['namespace'];
  132. }
  133. // path within the default namespace
  134. if (ctype_digit((string) $path)) {
  135. $path = $namespace;
  136. $namespace = null;
  137. }
  138. $normalized[$path] = $namespace;
  139. }
  140. return $normalized;
  141. })
  142. ->end()
  143. ->prototype('variable')->end()
  144. ->end()
  145. ->end()
  146. ;
  147. }
  148. private function addTwigFormatOptions(ArrayNodeDefinition $rootNode)
  149. {
  150. $rootNode
  151. ->children()
  152. ->arrayNode('date')
  153. ->info('The default format options used by the date filter')
  154. ->addDefaultsIfNotSet()
  155. ->children()
  156. ->scalarNode('format')->defaultValue('F j, Y H:i')->end()
  157. ->scalarNode('interval_format')->defaultValue('%d days')->end()
  158. ->scalarNode('timezone')
  159. ->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used')
  160. ->defaultNull()
  161. ->end()
  162. ->end()
  163. ->end()
  164. ->arrayNode('number_format')
  165. ->info('The default format options for the number_format filter')
  166. ->addDefaultsIfNotSet()
  167. ->children()
  168. ->integerNode('decimals')->defaultValue(0)->end()
  169. ->scalarNode('decimal_point')->defaultValue('.')->end()
  170. ->scalarNode('thousands_separator')->defaultValue(',')->end()
  171. ->end()
  172. ->end()
  173. ->end()
  174. ;
  175. }
  176. }