Configuration.php 7.9KB

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