MainConfiguration.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. ->arrayNode('use_controller')
  48. ->addDefaultsIfNotSet()
  49. ->treatTrueLike(array('enabled' => true))
  50. ->treatFalseLike(array('enabled' => false))
  51. ->children()
  52. ->booleanNode('enabled')->defaultValue('%kernel.debug%')->end()
  53. ->booleanNode('profiler')->defaultFalse()->end()
  54. ->end()
  55. ->end()
  56. ->scalarNode('read_from')->defaultValue('%kernel.root_dir%/../web')->end()
  57. ->scalarNode('write_to')->defaultValue('%assetic.read_from%')->end()
  58. ->booleanNode('dump_on_warmup')->end()
  59. ->scalarNode('java')->defaultValue(function() use($finder) { return $finder->find('java', '/usr/bin/java'); })->end()
  60. ->scalarNode('node')->defaultValue(function() use($finder) { return $finder->find('node', '/usr/bin/node'); })->end()
  61. ->scalarNode('ruby')->defaultValue(function() use($finder) { return $finder->find('ruby', '/usr/bin/ruby'); })->end()
  62. ->scalarNode('sass')->defaultValue(function() use($finder) { return $finder->find('sass', '/usr/bin/sass'); })->end()
  63. ->end()
  64. // bundles
  65. ->fixXmlConfig('bundle')
  66. ->children()
  67. ->arrayNode('bundles')
  68. ->defaultValue($this->bundles)
  69. ->prototype('scalar')
  70. ->validate()
  71. ->ifNotInArray($this->bundles)
  72. ->thenInvalid('%s is not a valid bundle.')
  73. ->end()
  74. ->end()
  75. ->end()
  76. ->end()
  77. // assets
  78. ->fixXmlConfig('asset')
  79. ->children()
  80. ->arrayNode('assets')
  81. ->addDefaultsIfNotSet()
  82. ->requiresAtLeastOneElement()
  83. ->useAttributeAsKey('name')
  84. ->prototype('array')
  85. ->beforeNormalization()
  86. // a scalar is a simple formula of one input file
  87. ->ifTrue(function($v) { return !is_array($v); })
  88. ->then(function($v) { return array('inputs' => array($v)); })
  89. ->end()
  90. ->beforeNormalization()
  91. ->always()
  92. ->then(function($v)
  93. {
  94. // cast scalars as array
  95. foreach (array('input', 'inputs', 'filter', 'filters') as $key) {
  96. if (isset($v[$key]) && !is_array($v[$key])) {
  97. $v[$key] = array($v[$key]);
  98. }
  99. }
  100. // organize arbitrary options
  101. foreach ($v as $key => $value) {
  102. if (!in_array($key, array('input', 'inputs', 'filter', 'filters', 'option', 'options'))) {
  103. $v['options'][$key] = $value;
  104. unset($v[$key]);
  105. }
  106. }
  107. return $v;
  108. })
  109. ->end()
  110. // the formula
  111. ->fixXmlConfig('input')
  112. ->fixXmlConfig('filter')
  113. ->children()
  114. ->arrayNode('inputs')
  115. ->prototype('scalar')->end()
  116. ->end()
  117. ->arrayNode('filters')
  118. ->prototype('scalar')->end()
  119. ->end()
  120. ->arrayNode('options')
  121. ->useAttributeAsKey('name')
  122. ->prototype('variable')->end()
  123. ->end()
  124. ->end()
  125. ->end()
  126. ->end()
  127. ->end()
  128. // filters
  129. ->fixXmlConfig('filter')
  130. ->children()
  131. ->arrayNode('filters')
  132. ->addDefaultsIfNotSet()
  133. ->requiresAtLeastOneElement()
  134. ->useAttributeAsKey('name')
  135. ->prototype('variable')
  136. ->treatNullLike(array())
  137. ->validate()
  138. ->ifTrue(function($v) { return !is_array($v); })
  139. ->thenInvalid('The assetic.filters config %s must be either null or an array.')
  140. ->end()
  141. ->end()
  142. ->validate()
  143. ->always(function($v) use ($finder) {
  144. if (isset($v['compass']) && !isset($v['compass']['bin'])) {
  145. $v['compass']['bin'] = $finder->find('compass', '/usr/bin/compass');
  146. }
  147. return $v;
  148. })
  149. ->end()
  150. ->end()
  151. ->end()
  152. // twig
  153. ->children()
  154. ->arrayNode('twig')
  155. ->addDefaultsIfNotSet()
  156. ->defaultValue(array())
  157. ->fixXmlConfig('function')
  158. ->children()
  159. ->arrayNode('functions')
  160. ->addDefaultsIfNotSet()
  161. ->defaultValue(array())
  162. ->useAttributeAsKey('name')
  163. ->prototype('variable')
  164. ->treatNullLike(array())
  165. ->validate()
  166. ->ifTrue(function($v) { return !is_array($v); })
  167. ->thenInvalid('The assetic.twig.functions config %s must be either null or an array.')
  168. ->end()
  169. ->end()
  170. ->end()
  171. ->end()
  172. ->end()
  173. ->end()
  174. ;
  175. return $builder;
  176. }
  177. }