123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
-
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- namespace Symfony\Bundle\AsseticBundle\DependencyInjection;
-
- use Symfony\Component\Process\ExecutableFinder;
- use Symfony\Component\Config\Definition\Builder\TreeBuilder;
- use Symfony\Component\Config\Definition\ConfigurationInterface;
-
- /**
- * This class contains the configuration information for the bundle
- *
- * This information is solely responsible for how the different configuration
- * sections are normalized, and merged.
- *
- * @author Christophe Coevoet <stof@notk.org>
- * @author Kris Wallsmith <kris@symfony.com>
- */
- class Configuration implements ConfigurationInterface
- {
- private $bundles;
-
- /**
- * Constructor
- *
- * @param array $bundles An array of bundle names
- */
- public function __construct(array $bundles)
- {
- $this->bundles = $bundles;
- }
-
- /**
- * Generates the configuration tree builder.
- *
- * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
- */
- public function getConfigTreeBuilder()
- {
- $builder = new TreeBuilder();
- $finder = new ExecutableFinder();
-
- $builder->root('assetic')
- ->children()
- ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
- ->arrayNode('use_controller')
- ->addDefaultsIfNotSet()
- ->treatTrueLike(array('enabled' => true))
- ->treatFalseLike(array('enabled' => false))
- ->children()
- ->booleanNode('enabled')->defaultValue('%kernel.debug%')->end()
- ->booleanNode('profiler')->defaultFalse()->end()
- ->end()
- ->end()
- ->scalarNode('read_from')->defaultValue('%kernel.root_dir%/../web')->end()
- ->scalarNode('write_to')->defaultValue('%assetic.read_from%')->end()
- ->scalarNode('java')->defaultValue(function() use($finder) { return $finder->find('java', '/usr/bin/java'); })->end()
- ->scalarNode('node')->defaultValue(function() use($finder) { return $finder->find('node', '/usr/bin/node'); })->end()
- ->arrayNode('node_paths')
- ->prototype('scalar')->end()
- ->end()
- ->scalarNode('ruby')->defaultValue(function() use($finder) { return $finder->find('ruby', '/usr/bin/ruby'); })->end()
- ->scalarNode('sass')->defaultValue(function() use($finder) { return $finder->find('sass', '/usr/bin/sass'); })->end()
- ->end()
-
- // variables
- ->fixXmlConfig('variable')
- ->children()
- ->arrayNode('variables')
- ->useAttributeAsKey('name')
- ->prototype('array')
- ->prototype('scalar')->end()
- ->end()
- ->end()
- ->end()
-
- // bundles
- ->fixXmlConfig('bundle')
- ->children()
- ->arrayNode('bundles')
- ->defaultValue($this->bundles)
- ->prototype('scalar')
- ->validate()
- ->ifNotInArray($this->bundles)
- ->thenInvalid('%s is not a valid bundle.')
- ->end()
- ->end()
- ->end()
- ->end()
-
- // assets
- ->fixXmlConfig('asset')
- ->children()
- ->arrayNode('assets')
- ->requiresAtLeastOneElement()
- ->useAttributeAsKey('name')
- ->prototype('array')
- ->beforeNormalization()
- // a scalar is a simple formula of one input file
- ->ifTrue(function($v) { return !is_array($v); })
- ->then(function($v) { return array('inputs' => array($v)); })
- ->end()
- ->beforeNormalization()
- ->always()
- ->then(function($v)
- {
- // cast scalars as array
- foreach (array('input', 'inputs', 'filter', 'filters') as $key) {
- if (isset($v[$key]) && !is_array($v[$key])) {
- $v[$key] = array($v[$key]);
- }
- }
-
- // organize arbitrary options
- foreach ($v as $key => $value) {
- if (!in_array($key, array('input', 'inputs', 'filter', 'filters', 'option', 'options'))) {
- $v['options'][$key] = $value;
- unset($v[$key]);
- }
- }
-
- return $v;
- })
- ->end()
-
- // the formula
- ->fixXmlConfig('input')
- ->fixXmlConfig('filter')
- ->children()
- ->arrayNode('inputs')
- ->prototype('scalar')->end()
- ->end()
- ->arrayNode('filters')
- ->prototype('scalar')->end()
- ->end()
- ->arrayNode('options')
- ->useAttributeAsKey('name')
- ->prototype('variable')->end()
- ->end()
- ->end()
- ->end()
- ->end()
- ->end()
-
- // filters
- ->fixXmlConfig('filter')
- ->children()
- ->arrayNode('filters')
- ->requiresAtLeastOneElement()
- ->useAttributeAsKey('name')
- ->prototype('variable')
- ->treatNullLike(array())
- ->validate()
- ->ifTrue(function($v) { return !is_array($v); })
- ->thenInvalid('The assetic.filters config %s must be either null or an array.')
- ->end()
- ->end()
- ->validate()
- ->always(function($v) use ($finder) {
- if (isset($v['compass']) && !isset($v['compass']['bin'])) {
- $v['compass']['bin'] = $finder->find('compass', '/usr/bin/compass');
- }
-
- return $v;
- })
- ->end()
- ->end()
- ->end()
-
- // twig
- ->children()
- ->arrayNode('twig')
- ->addDefaultsIfNotSet()
- ->fixXmlConfig('function')
- ->children()
- ->arrayNode('functions')
- ->defaultValue(array())
- ->useAttributeAsKey('name')
- ->prototype('variable')
- ->treatNullLike(array())
- ->validate()
- ->ifTrue(function($v) { return !is_array($v); })
- ->thenInvalid('The assetic.twig.functions config %s must be either null or an array.')
- ->end()
- ->end()
- ->end()
- ->end()
- ->end()
- ->end()
- ;
-
- return $builder;
- }
- }
|