vendor/symfony/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony framework.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * This source file is subject to the MIT license that is bundled
       
     9  * with this source code in the file LICENSE.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\TwigBundle\DependencyInjection;
       
    13 
       
    14 use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
       
    15 use Symfony\Component\Config\Definition\Builder\TreeBuilder;
       
    16 use Symfony\Component\Config\Definition\ConfigurationInterface;
       
    17 
       
    18 /**
       
    19  * TwigExtension configuration structure.
       
    20  *
       
    21  * @author Jeremy Mikola <jmikola@gmail.com>
       
    22  */
       
    23 class Configuration implements ConfigurationInterface
       
    24 {
       
    25     /**
       
    26      * Generates the configuration tree builder.
       
    27      *
       
    28      * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
       
    29      */
       
    30     public function getConfigTreeBuilder()
       
    31     {
       
    32         $treeBuilder = new TreeBuilder();
       
    33         $rootNode = $treeBuilder->root('twig');
       
    34 
       
    35         $rootNode
       
    36             ->children()
       
    37                 ->scalarNode('exception_controller')->defaultValue('Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController::showAction')->end()
       
    38             ->end()
       
    39         ;
       
    40 
       
    41         $this->addFormSection($rootNode);
       
    42         $this->addGlobalsSection($rootNode);
       
    43         $this->addTwigOptions($rootNode);
       
    44 
       
    45         return $treeBuilder;
       
    46     }
       
    47 
       
    48     private function addFormSection(ArrayNodeDefinition $rootNode)
       
    49     {
       
    50         $rootNode
       
    51             ->children()
       
    52                 ->arrayNode('form')
       
    53                     ->addDefaultsIfNotSet()
       
    54                     ->fixXmlConfig('resource')
       
    55                     ->children()
       
    56                         ->arrayNode('resources')
       
    57                             ->addDefaultsIfNotSet()
       
    58                             ->defaultValue(array('form_div_layout.html.twig'))
       
    59                             ->validate()
       
    60                                 ->ifTrue(function($v) { return !in_array('form_div_layout.html.twig', $v); })
       
    61                                 ->then(function($v){
       
    62                                     return array_merge(array('form_div_layout.html.twig'), $v);
       
    63                                 })
       
    64                             ->end()
       
    65                             ->prototype('scalar')->end()
       
    66                         ->end()
       
    67                     ->end()
       
    68                 ->end()
       
    69             ->end()
       
    70         ;
       
    71     }
       
    72 
       
    73     private function addGlobalsSection(ArrayNodeDefinition $rootNode)
       
    74     {
       
    75         $rootNode
       
    76             ->fixXmlConfig('global')
       
    77             ->children()
       
    78                 ->arrayNode('globals')
       
    79                     ->useAttributeAsKey('key')
       
    80                     ->prototype('array')
       
    81                         ->beforeNormalization()
       
    82                             ->ifTrue(function($v){ return is_string($v) && '@' === substr($v, 0, 1); })
       
    83                             ->then(function($v){ return array('id' => substr($v, 1), 'type' => 'service'); })
       
    84                         ->end()
       
    85                         ->beforeNormalization()
       
    86                             ->ifTrue(function($v){
       
    87                                 if (is_array($v)) {
       
    88                                     $keys = array_keys($v);
       
    89                                     sort($keys);
       
    90 
       
    91                                     return $keys !== array('id', 'type') && $keys !== array('value');
       
    92                                 }
       
    93 
       
    94                                 return true;
       
    95                             })
       
    96                             ->then(function($v){ return array('value' => $v); })
       
    97                         ->end()
       
    98                         ->children()
       
    99                             ->scalarNode('id')->end()
       
   100                             ->scalarNode('type')
       
   101                                 ->validate()
       
   102                                     ->ifNotInArray(array('service'))
       
   103                                     ->thenInvalid('The %s type is not supported')
       
   104                                 ->end()
       
   105                             ->end()
       
   106                             ->variableNode('value')->end()
       
   107                         ->end()
       
   108                     ->end()
       
   109                 ->end()
       
   110             ->end()
       
   111         ;
       
   112     }
       
   113 
       
   114     private function addTwigOptions(ArrayNodeDefinition $rootNode)
       
   115     {
       
   116         $rootNode
       
   117             ->children()
       
   118                 ->scalarNode('autoescape')->end()
       
   119                 ->scalarNode('base_template_class')->end()
       
   120                 ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
       
   121                 ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
       
   122                 ->scalarNode('debug')->defaultValue('%kernel.debug%')->end()
       
   123                 ->scalarNode('strict_variables')->end()
       
   124                 ->scalarNode('auto_reload')->end()
       
   125             ->end()
       
   126         ;
       
   127     }
       
   128 }