Guest User

Untitled

a guest
Jul 22nd, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Symfony\Bundle\TwigBundle\DependencyInjection;
  4.  
  5. use Symfony\Component\Config\Definition\Builder\NodeBuilder;
  6. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  7.  
  8. /**
  9. * TwigExtension configuration structure.
  10. *
  11. * @author Jeremy Mikola <jmikola@gmail.com>
  12. */
  13. class Configuration
  14. {
  15. /**
  16. * Generates the configuration tree.
  17. *
  18. * @return \Symfony\Component\Config\Definition\NodeInterface
  19. */
  20. public function getConfigTree()
  21. {
  22. $treeBuilder = new TreeBuilder();
  23. $rootNode = $treeBuilder->root('twig', 'array');
  24.  
  25. $rootNode
  26. ->scalarNode('cache_warmer')->end()
  27. ;
  28.  
  29. $this->addExtensionsSection($rootNode);
  30. $this->addFormSection($rootNode);
  31. $this->addGlobalsSection($rootNode);
  32. $this->addTwigOptions($rootNode);
  33.  
  34. return $treeBuilder->buildTree();
  35. }
  36.  
  37. private function addExtensionsSection(NodeBuilder $rootNode)
  38. {
  39. $rootNode
  40. ->fixXmlConfig('extension')
  41. ->arrayNode('extensions')
  42. ->prototype('scalar')
  43. ->beforeNormalization()
  44. ->ifTrue(function($v) { return is_array($v) && isset($v['id']); })
  45. ->then(function($v){ return $v['id']; })
  46. ->end()
  47. ->end()
  48. ->end()
  49. ;
  50. }
  51.  
  52. private function addFormSection(NodeBuilder $rootNode)
  53. {
  54. $rootNode
  55. ->arrayNode('form')
  56. ->addDefaultsIfNotSet()
  57. ->fixXmlConfig('resource')
  58. ->arrayNode('resources')
  59. ->addDefaultsIfNotSet()
  60. ->defaultValue(array('TwigBundle::form.html.twig'))
  61. ->validate()
  62. ->always()
  63. ->then(function($v){
  64. return array_merge(array('TwigBundle::form.html.twig'), $v);
  65. })
  66. ->end()
  67. ->prototype('scalar')->end()
  68. ->end()
  69. ->end()
  70. ;
  71. }
  72.  
  73. private function addGlobalsSection(NodeBuilder $rootNode)
  74. {
  75. $rootNode
  76. ->fixXmlConfig('global')
  77. ->arrayNode('globals')
  78. ->useAttributeAsKey('key')
  79. ->prototype('array')
  80. ->beforeNormalization()
  81. ->ifTrue(function($v){ return is_scalar($v); })
  82. ->then(function($v){
  83. return ('@' === substr($v, 0, 1))
  84. ? array('id' => substr($v, 1), 'type' => 'service')
  85. : array('value' => $v, 'type' => 'value');
  86. })
  87. ->end()
  88. ->scalarNode('id')->end()
  89. ->scalarNode('type')
  90. ->addDefaultsIfNotSet()
  91. ->defaultValue('value')
  92. ->validate()
  93. ->ifNotInArray(array('service', 'value'))
  94. ->thenInvalid('The %s type is not supported')
  95. ->end()
  96. ->end()
  97. ->scalarNode('value')->end()
  98. ->end()
  99. ->end()
  100. ;
  101. }
  102.  
  103. private function addTwigOptions(NodeBuilder $rootNode)
  104. {
  105. $rootNode
  106. ->scalarNode('autoescape')->end()
  107. ->scalarNode('base_template_class')->end()
  108. ->scalarNode('cache')
  109. ->addDefaultsIfNotSet()
  110. ->defaultValue('%kernel.cache_dir%/twig')
  111. ->end()
  112. ->scalarNode('charset')
  113. ->addDefaultsIfNotSet()
  114. ->defaultValue('%kernel.charset%')
  115. ->end()
  116. ->scalarNode('debug')
  117. ->addDefaultsIfNotSet()
  118. ->defaultValue('%kernel.debug%')
  119. ->end()
  120. ->scalarNode('strict_variables')->end()
  121. ->scalarNode('auto_reload')->end()
  122. ;
  123. }
  124. }
Add Comment
Please, Sign In to add comment