Configuration.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  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. $this->addFormSection($rootNode);
  31. $this->addGlobalsSection($rootNode);
  32. $this->addTwigOptions($rootNode);
  33. return $treeBuilder;
  34. }
  35. private function addFormSection(ArrayNodeDefinition $rootNode)
  36. {
  37. $rootNode
  38. ->children()
  39. ->arrayNode('form')
  40. ->addDefaultsIfNotSet()
  41. ->fixXmlConfig('resource')
  42. ->children()
  43. ->arrayNode('resources')
  44. ->addDefaultsIfNotSet()
  45. ->defaultValue(array('div_layout.html.twig'))
  46. ->validate()
  47. ->always()
  48. ->then(function($v){
  49. return array_merge(array('div_layout.html.twig'), $v);
  50. })
  51. ->end()
  52. ->prototype('scalar')->end()
  53. ->end()
  54. ->end()
  55. ->end()
  56. ->end()
  57. ;
  58. }
  59. private function addGlobalsSection(ArrayNodeDefinition $rootNode)
  60. {
  61. $rootNode
  62. ->fixXmlConfig('global')
  63. ->children()
  64. ->arrayNode('globals')
  65. ->useAttributeAsKey('key')
  66. ->prototype('array')
  67. ->beforeNormalization()
  68. ->ifTrue(function($v){ return is_string($v) && '@' === substr($v, 0, 1); })
  69. ->then(function($v){ return array('id' => substr($v, 1), 'type' => 'service'); })
  70. ->end()
  71. ->beforeNormalization()
  72. ->ifTrue(function($v){
  73. if (is_array($v)) {
  74. $keys = array_keys($v);
  75. sort($keys);
  76. return $keys !== array('id', 'type') && $keys !== array('value');
  77. }
  78. return true;
  79. })
  80. ->then(function($v){ return array('value' => $v); })
  81. ->end()
  82. ->children()
  83. ->scalarNode('id')->end()
  84. ->scalarNode('type')
  85. ->validate()
  86. ->ifNotInArray(array('service'))
  87. ->thenInvalid('The %s type is not supported')
  88. ->end()
  89. ->end()
  90. ->variableNode('value')->end()
  91. ->end()
  92. ->end()
  93. ->end()
  94. ->end()
  95. ;
  96. }
  97. private function addTwigOptions(ArrayNodeDefinition $rootNode)
  98. {
  99. $rootNode
  100. ->children()
  101. ->scalarNode('autoescape')->end()
  102. ->scalarNode('base_template_class')->end()
  103. ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
  104. ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
  105. ->scalarNode('debug')->defaultValue('%kernel.debug%')->end()
  106. ->scalarNode('strict_variables')->end()
  107. ->scalarNode('auto_reload')->end()
  108. ->end()
  109. ;
  110. }
  111. }