MainConfiguration.php 7.5KB

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