vendor/bundles/Symfony/Bundle/AsseticBundle/DependencyInjection/MainConfiguration.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\AsseticBundle\DependencyInjection;
       
    13 
       
    14 use Symfony\Component\Process\ExecutableFinder;
       
    15 use Symfony\Component\Config\Definition\Builder\TreeBuilder;
       
    16 use Symfony\Component\Config\Definition\ConfigurationInterface;
       
    17 
       
    18 /**
       
    19  * This class contains the configuration information for the bundle
       
    20  *
       
    21  * This information is solely responsible for how the different configuration
       
    22  * sections are normalized, and merged.
       
    23  *
       
    24  * @author Christophe Coevoet <stof@notk.org>
       
    25  * @author Kris Wallsmith <kris@symfony.com>
       
    26  */
       
    27 class MainConfiguration implements ConfigurationInterface
       
    28 {
       
    29     private $bundles;
       
    30 
       
    31     /**
       
    32      * Constructor
       
    33      *
       
    34      * @param array $bundles An array of bundle names
       
    35      */
       
    36     public function __construct(array $bundles)
       
    37     {
       
    38         $this->bundles = $bundles;
       
    39     }
       
    40 
       
    41     /**
       
    42      * Generates the configuration tree builder.
       
    43      *
       
    44      * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
       
    45      */
       
    46     public function getConfigTreeBuilder()
       
    47     {
       
    48         $builder = new TreeBuilder();
       
    49         $finder = new ExecutableFinder();
       
    50 
       
    51         $builder->root('assetic')
       
    52             ->children()
       
    53                 ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
       
    54                 ->booleanNode('use_controller')->defaultValue('%kernel.debug%')->end()
       
    55                 ->scalarNode('read_from')->defaultValue('%kernel.root_dir%/../web')->end()
       
    56                 ->scalarNode('write_to')->defaultValue('%assetic.read_from%')->end()
       
    57                 ->scalarNode('java')->defaultValue(function() use($finder) { return $finder->find('java', '/usr/bin/java'); })->end()
       
    58                 ->scalarNode('node')->defaultValue(function() use($finder) { return $finder->find('node', '/usr/bin/node'); })->end()
       
    59                 ->scalarNode('sass')->defaultValue(function() use($finder) { return $finder->find('sass', '/usr/bin/sass'); })->end()
       
    60             ->end()
       
    61 
       
    62             // bundles
       
    63             ->fixXmlConfig('bundle')
       
    64             ->children()
       
    65                 ->arrayNode('bundles')
       
    66                     ->defaultValue($this->bundles)
       
    67                     ->prototype('scalar')
       
    68                         ->validate()
       
    69                             ->ifNotInArray($this->bundles)
       
    70                             ->thenInvalid('%s is not a valid bundle.')
       
    71                         ->end()
       
    72                     ->end()
       
    73                 ->end()
       
    74             ->end()
       
    75 
       
    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 
       
   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 
       
   108                                 return $v;
       
   109                             })
       
   110                         ->end()
       
   111 
       
   112                         // the formula
       
   113                         ->fixXmlConfig('input')
       
   114                         ->fixXmlConfig('filter')
       
   115                         ->children()
       
   116                             ->arrayNode('inputs')
       
   117                                 ->prototype('scalar')->end()
       
   118                             ->end()
       
   119                             ->arrayNode('filters')
       
   120                                 ->prototype('scalar')->end()
       
   121                             ->end()
       
   122                             ->arrayNode('options')
       
   123                                 ->useAttributeAsKey('name')
       
   124                                 ->prototype('variable')->end()
       
   125                             ->end()
       
   126                         ->end()
       
   127                     ->end()
       
   128                 ->end()
       
   129             ->end()
       
   130 
       
   131             // filters
       
   132             ->fixXmlConfig('filter')
       
   133             ->children()
       
   134                 ->arrayNode('filters')
       
   135                     ->addDefaultsIfNotSet()
       
   136                     ->requiresAtLeastOneElement()
       
   137                     ->useAttributeAsKey('name')
       
   138                     ->prototype('variable')
       
   139                         ->treatNullLike(array())
       
   140                         ->validate()
       
   141                             ->ifTrue(function($v) { return !is_array($v); })
       
   142                             ->thenInvalid('The assetic.filters config %s must be either null or an array.')
       
   143                         ->end()
       
   144                     ->end()
       
   145                 ->end()
       
   146             ->end()
       
   147 
       
   148             // twig
       
   149             ->children()
       
   150                 ->arrayNode('twig')
       
   151                     ->addDefaultsIfNotSet()
       
   152                     ->defaultValue(array())
       
   153                     ->fixXmlConfig('function')
       
   154                     ->children()
       
   155                         ->arrayNode('functions')
       
   156                             ->addDefaultsIfNotSet()
       
   157                             ->defaultValue(array())
       
   158                             ->useAttributeAsKey('name')
       
   159                             ->prototype('variable')
       
   160                                 ->treatNullLike(array())
       
   161                                 ->validate()
       
   162                                     ->ifTrue(function($v) { return !is_array($v); })
       
   163                                     ->thenInvalid('The assetic.twig.functions config %s must be either null or an array.')
       
   164                                 ->end()
       
   165                             ->end()
       
   166                         ->end()
       
   167                     ->end()
       
   168                 ->end()
       
   169             ->end()
       
   170         ;
       
   171 
       
   172         return $builder;
       
   173     }
       
   174 }