PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/assetic-bundle/DependencyInjection/Configuration.php

https://gitlab.com/freebird/WebApp
PHP | 254 lines | 204 code | 19 blank | 31 comment | 5 complexity | d81bed98d155a42bb4a6cce2383a362a MD5 | raw file
  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\ArrayNodeDefinition;
  13. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  14. use Symfony\Component\Config\Definition\ConfigurationInterface;
  15. /**
  16. * This class contains the configuration information for the bundle
  17. *
  18. * This information is solely responsible for how the different configuration
  19. * sections are normalized, and merged.
  20. *
  21. * @author Christophe Coevoet <stof@notk.org>
  22. * @author Kris Wallsmith <kris@symfony.com>
  23. */
  24. class Configuration implements ConfigurationInterface
  25. {
  26. private $bundles;
  27. /**
  28. * Constructor
  29. *
  30. * @param array $bundles An array of bundle names
  31. */
  32. public function __construct(array $bundles)
  33. {
  34. $this->bundles = $bundles;
  35. }
  36. /**
  37. * Generates the configuration tree builder.
  38. *
  39. * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
  40. */
  41. public function getConfigTreeBuilder()
  42. {
  43. $builder = new TreeBuilder();
  44. $finder = new ExecutableFinder();
  45. $rootNode = $builder->root('assetic');
  46. $rootNode
  47. ->children()
  48. ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
  49. ->arrayNode('use_controller')
  50. ->addDefaultsIfNotSet()
  51. ->treatTrueLike(array('enabled' => true))
  52. ->treatFalseLike(array('enabled' => false))
  53. ->children()
  54. ->booleanNode('enabled')->defaultValue('%kernel.debug%')->end()
  55. ->booleanNode('profiler')->defaultFalse()->end()
  56. ->end()
  57. ->end()
  58. ->scalarNode('read_from')->defaultValue('%kernel.root_dir%/../web')->end()
  59. ->scalarNode('write_to')->defaultValue('%assetic.read_from%')->end()
  60. ->scalarNode('java')->defaultValue(function () use ($finder) { return $finder->find('java', '/usr/bin/java'); })->end()
  61. ->scalarNode('node')->defaultValue(function () use ($finder) { return $finder->find('node', '/usr/bin/node'); })->end()
  62. ->arrayNode('node_paths')
  63. ->prototype('scalar')->end()
  64. ->end()
  65. ->scalarNode('ruby')->defaultValue(function () use ($finder) { return $finder->find('ruby', '/usr/bin/ruby'); })->end()
  66. ->scalarNode('sass')->defaultValue(function () use ($finder) { return $finder->find('sass', '/usr/bin/sass'); })->end()
  67. ->scalarNode('reactjsx')->defaultValue(function () use ($finder) { return $finder->find('reactjsx', '/usr/bin/jsx'); })->end()
  68. ->end()
  69. ;
  70. $this->addVariablesSection($rootNode);
  71. $this->addBundlesSection($rootNode);
  72. $this->addAssetsSection($rootNode);
  73. $this->addFiltersSection($rootNode, $finder);
  74. $this->addWorkersSection($rootNode);
  75. $this->addTwigSection($rootNode);
  76. return $builder;
  77. }
  78. private function addVariablesSection(ArrayNodeDefinition $rootNode)
  79. {
  80. $rootNode
  81. ->fixXmlConfig('variable')
  82. ->children()
  83. ->arrayNode('variables')
  84. ->useAttributeAsKey('name')
  85. ->prototype('array')
  86. ->prototype('scalar')->end()
  87. ->end()
  88. ->end()
  89. ->end()
  90. ;
  91. }
  92. private function addBundlesSection(ArrayNodeDefinition $rootNode)
  93. {
  94. $rootNode
  95. ->fixXmlConfig('bundle')
  96. ->children()
  97. ->arrayNode('bundles')
  98. ->defaultValue($this->bundles)
  99. ->treatNullLike($this->bundles)
  100. ->prototype('scalar')
  101. ->validate()
  102. ->ifNotInArray($this->bundles)
  103. ->thenInvalid('%s is not a valid bundle.')
  104. ->end()
  105. ->end()
  106. ->end()
  107. ->end()
  108. ;
  109. }
  110. private function addAssetsSection(ArrayNodeDefinition $rootNode)
  111. {
  112. $rootNode
  113. ->fixXmlConfig('asset')
  114. ->children()
  115. ->arrayNode('assets')
  116. ->requiresAtLeastOneElement()
  117. ->useAttributeAsKey('name')
  118. ->prototype('array')
  119. ->beforeNormalization()
  120. // a scalar is a simple formula of one input file
  121. ->ifTrue(function ($v) { return !is_array($v); })
  122. ->then(function ($v) { return array('inputs' => array($v)); })
  123. ->end()
  124. ->beforeNormalization()
  125. ->always()
  126. ->then(function ($v) {
  127. // cast scalars as array
  128. foreach (array('input', 'inputs', 'filter', 'filters') as $key) {
  129. if (isset($v[$key]) && !is_array($v[$key])) {
  130. $v[$key] = array($v[$key]);
  131. }
  132. }
  133. // organize arbitrary options
  134. foreach ($v as $key => $value) {
  135. if (!in_array($key, array('input', 'inputs', 'filter', 'filters', 'option', 'options'))) {
  136. $v['options'][$key] = $value;
  137. unset($v[$key]);
  138. }
  139. }
  140. return $v;
  141. })
  142. ->end()
  143. // the formula
  144. ->fixXmlConfig('input')
  145. ->fixXmlConfig('filter')
  146. ->children()
  147. ->arrayNode('inputs')
  148. ->prototype('scalar')->end()
  149. ->end()
  150. ->arrayNode('filters')
  151. ->prototype('scalar')->end()
  152. ->end()
  153. ->arrayNode('options')
  154. ->useAttributeAsKey('name')
  155. ->prototype('variable')->end()
  156. ->end()
  157. ->end()
  158. ->end()
  159. ->end()
  160. ->end()
  161. ;
  162. }
  163. private function addFiltersSection(ArrayNodeDefinition $rootNode, ExecutableFinder $finder)
  164. {
  165. $rootNode
  166. ->fixXmlConfig('filter')
  167. ->children()
  168. ->arrayNode('filters')
  169. ->requiresAtLeastOneElement()
  170. ->useAttributeAsKey('name')
  171. ->prototype('variable')
  172. ->treatNullLike(array())
  173. ->validate()
  174. ->ifTrue(function ($v) { return !is_array($v); })
  175. ->thenInvalid('The assetic.filters config %s must be either null or an array.')
  176. ->end()
  177. ->end()
  178. ->validate()
  179. ->always(function ($v) use ($finder) {
  180. if (isset($v['compass']) && !isset($v['compass']['bin'])) {
  181. $v['compass']['bin'] = $finder->find('compass', '/usr/bin/compass');
  182. }
  183. return $v;
  184. })
  185. ->end()
  186. ->end()
  187. ->end()
  188. ;
  189. }
  190. private function addWorkersSection(ArrayNodeDefinition $rootNode)
  191. {
  192. $rootNode
  193. ->children()
  194. ->arrayNode('workers')
  195. ->addDefaultsIfNotSet()
  196. ->children()
  197. ->arrayNode('cache_busting')
  198. ->treatTrueLike(array('enabled' => true))
  199. ->treatFalseLike(array('enabled' => false))
  200. ->treatNullLike(array('enabled' => true))
  201. ->addDefaultsIfNotSet()
  202. ->children()
  203. ->booleanNode('enabled')->defaultFalse()->end()
  204. ->end()
  205. ->end()
  206. ->end()
  207. ->end()
  208. ->end()
  209. ;
  210. }
  211. private function addTwigSection(ArrayNodeDefinition $rootNode)
  212. {
  213. $rootNode
  214. ->children()
  215. ->arrayNode('twig')
  216. ->addDefaultsIfNotSet()
  217. ->fixXmlConfig('function')
  218. ->children()
  219. ->arrayNode('functions')
  220. ->defaultValue(array())
  221. ->useAttributeAsKey('name')
  222. ->prototype('variable')
  223. ->treatNullLike(array())
  224. ->validate()
  225. ->ifTrue(function ($v) { return !is_array($v); })
  226. ->thenInvalid('The assetic.twig.functions config %s must be either null or an array.')
  227. ->end()
  228. ->end()
  229. ->end()
  230. ->end()
  231. ->end()
  232. ->end()
  233. ;
  234. }
  235. }