MainConfiguration.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\AsseticBundle\DependencyInjection;
  11. use Symfony\Component\Process\ExecutableFinder;
  12. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  13. use Symfony\Component\Config\Definition\ConfigurationInterface;
  14. /**
  15. * This class contains the configuration information for the bundle
  16. *
  17. * This information is solely responsible for how the different configuration
  18. * sections are normalized, and merged.
  19. *
  20. * @author Christophe Coevoet <stof@notk.org>
  21. * @author Kris Wallsmith <kris@symfony.com>
  22. */
  23. class MainConfiguration implements ConfigurationInterface
  24. {
  25. private $bundles;
  26. /**
  27. * Constructor
  28. *
  29. * @param array $bundles An array of bundle names
  30. */
  31. public function __construct(array $bundles)
  32. {
  33. $this->bundles = $bundles;
  34. }
  35. /**
  36. * Generates the configuration tree builder.
  37. *
  38. * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
  39. */
  40. public function getConfigTreeBuilder()
  41. {
  42. $builder = new TreeBuilder();
  43. $finder = new ExecutableFinder();
  44. $builder->root('assetic')
  45. ->children()
  46. ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
  47. ->booleanNode('use_controller')->defaultValue('%kernel.debug%')->end()
  48. ->scalarNode('read_from')->defaultValue('%kernel.root_dir%/../web')->end()
  49. ->scalarNode('write_to')->defaultValue('%assetic.read_from%')->end()
  50. ->scalarNode('java')->defaultValue(function() use($finder) { return $finder->find('java', '/usr/bin/java'); })->end()
  51. ->scalarNode('node')->defaultValue(function() use($finder) { return $finder->find('node', '/usr/bin/node'); })->end()
  52. ->scalarNode('sass')->defaultValue(function() use($finder) { return $finder->find('sass', '/usr/bin/sass'); })->end()
  53. ->end()
  54. // bundles
  55. ->fixXmlConfig('bundle')
  56. ->children()
  57. ->arrayNode('bundles')
  58. ->defaultValue($this->bundles)
  59. ->prototype('scalar')
  60. ->validate()
  61. ->ifNotInArray($this->bundles)
  62. ->thenInvalid('%s is not a valid bundle.')
  63. ->end()
  64. ->end()
  65. ->end()
  66. ->end()
  67. // assets
  68. ->fixXmlConfig('asset')
  69. ->children()
  70. ->arrayNode('assets')
  71. ->addDefaultsIfNotSet()
  72. ->requiresAtLeastOneElement()
  73. ->useAttributeAsKey('name')
  74. ->prototype('array')
  75. ->beforeNormalization()
  76. // a scalar is a simple formula of one input file
  77. ->ifTrue(function($v) { return !is_array($v); })
  78. ->then(function($v) { return array('inputs' => array($v)); })
  79. ->end()
  80. ->beforeNormalization()
  81. ->always()
  82. ->then(function($v)
  83. {
  84. // cast scalars as array
  85. foreach (array('input', 'inputs', 'filter', 'filters') as $key) {
  86. if (isset($v[$key]) && !is_array($v[$key])) {
  87. $v[$key] = array($v[$key]);
  88. }
  89. }
  90. // organize arbitrary options
  91. foreach ($v as $key => $value) {
  92. if (!in_array($key, array('input', 'inputs', 'filter', 'filters', 'option', 'options'))) {
  93. $v['options'][$key] = $value;
  94. unset($v[$key]);
  95. }
  96. }
  97. return $v;
  98. })
  99. ->end()
  100. // the formula
  101. ->fixXmlConfig('input')
  102. ->fixXmlConfig('filter')
  103. ->children()
  104. ->arrayNode('inputs')
  105. ->prototype('scalar')->end()
  106. ->end()
  107. ->arrayNode('filters')
  108. ->prototype('scalar')->end()
  109. ->end()
  110. ->arrayNode('options')
  111. ->useAttributeAsKey('name')
  112. ->prototype('variable')->end()
  113. ->end()
  114. ->end()
  115. ->end()
  116. ->end()
  117. ->end()
  118. // filters
  119. ->fixXmlConfig('filter')
  120. ->children()
  121. ->arrayNode('filters')
  122. ->addDefaultsIfNotSet()
  123. ->requiresAtLeastOneElement()
  124. ->useAttributeAsKey('name')
  125. ->prototype('variable')
  126. ->treatNullLike(array())
  127. ->validate()
  128. ->ifTrue(function($v) { return !is_array($v); })
  129. ->thenInvalid('The assetic.filters config %s must be either null or an array.')
  130. ->end()
  131. ->end()
  132. ->end()
  133. ->end()
  134. // twig
  135. ->children()
  136. ->arrayNode('twig')
  137. ->addDefaultsIfNotSet()
  138. ->defaultValue(array())
  139. ->fixXmlConfig('function')
  140. ->children()
  141. ->arrayNode('functions')
  142. ->addDefaultsIfNotSet()
  143. ->defaultValue(array())
  144. ->useAttributeAsKey('name')
  145. ->prototype('variable')
  146. ->treatNullLike(array())
  147. ->validate()
  148. ->ifTrue(function($v) { return !is_array($v); })
  149. ->thenInvalid('The assetic.twig.functions config %s must be either null or an array.')
  150. ->end()
  151. ->end()
  152. ->end()
  153. ->end()
  154. ->end()
  155. ->end()
  156. ;
  157. return $builder;
  158. }
  159. }