vendor/symfony/twig-bundle/DependencyInjection/Configuration.php line 133

Open in your IDE?
  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\TwigBundle\DependencyInjection;
  11. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  12. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  13. use Symfony\Component\Config\Definition\ConfigurationInterface;
  14. /**
  15.  * TwigExtension configuration structure.
  16.  *
  17.  * @author Jeremy Mikola <jmikola@gmail.com>
  18.  */
  19. class Configuration implements ConfigurationInterface
  20. {
  21.     /**
  22.      * Generates the configuration tree builder.
  23.      *
  24.      * @return TreeBuilder The tree builder
  25.      */
  26.     public function getConfigTreeBuilder()
  27.     {
  28.         $treeBuilder = new TreeBuilder();
  29.         $rootNode $treeBuilder->root('twig');
  30.         $rootNode
  31.             ->children()
  32.                 ->scalarNode('exception_controller')->defaultValue('twig.controller.exception::showAction')->end()
  33.             ->end()
  34.         ;
  35.         $this->addFormThemesSection($rootNode);
  36.         $this->addGlobalsSection($rootNode);
  37.         $this->addTwigOptions($rootNode);
  38.         $this->addTwigFormatOptions($rootNode);
  39.         return $treeBuilder;
  40.     }
  41.     private function addFormThemesSection(ArrayNodeDefinition $rootNode)
  42.     {
  43.         $rootNode
  44.             ->fixXmlConfig('form_theme')
  45.             ->children()
  46.                 ->arrayNode('form_themes')
  47.                     ->addDefaultChildrenIfNoneSet()
  48.                     ->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
  49.                     ->example(['MyBundle::form.html.twig'])
  50.                     ->validate()
  51.                         ->ifTrue(function ($v) { return !\in_array('form_div_layout.html.twig'$v); })
  52.                         ->then(function ($v) {
  53.                             return array_merge(['form_div_layout.html.twig'], $v);
  54.                         })
  55.                     ->end()
  56.                 ->end()
  57.             ->end()
  58.         ;
  59.     }
  60.     private function addGlobalsSection(ArrayNodeDefinition $rootNode)
  61.     {
  62.         $rootNode
  63.             ->fixXmlConfig('global')
  64.             ->children()
  65.                 ->arrayNode('globals')
  66.                     ->normalizeKeys(false)
  67.                     ->useAttributeAsKey('key')
  68.                     ->example(['foo' => '"@bar"''pi' => 3.14])
  69.                     ->prototype('array')
  70.                         ->normalizeKeys(false)
  71.                         ->beforeNormalization()
  72.                             ->ifTrue(function ($v) { return \is_string($v) && === strpos($v'@'); })
  73.                             ->then(function ($v) {
  74.                                 if (=== strpos($v'@@')) {
  75.                                     return substr($v1);
  76.                                 }
  77.                                 return ['id' => substr($v1), 'type' => 'service'];
  78.                             })
  79.                         ->end()
  80.                         ->beforeNormalization()
  81.                             ->ifTrue(function ($v) {
  82.                                 if (\is_array($v)) {
  83.                                     $keys array_keys($v);
  84.                                     sort($keys);
  85.                                     return $keys !== ['id''type'] && $keys !== ['value'];
  86.                                 }
  87.                                 return true;
  88.                             })
  89.                             ->then(function ($v) { return ['value' => $v]; })
  90.                         ->end()
  91.                         ->children()
  92.                             ->scalarNode('id')->end()
  93.                             ->scalarNode('type')
  94.                                 ->validate()
  95.                                     ->ifNotInArray(['service'])
  96.                                     ->thenInvalid('The %s type is not supported')
  97.                                 ->end()
  98.                             ->end()
  99.                             ->variableNode('value')->end()
  100.                         ->end()
  101.                     ->end()
  102.                 ->end()
  103.             ->end()
  104.         ;
  105.     }
  106.     private function addTwigOptions(ArrayNodeDefinition $rootNode)
  107.     {
  108.         $rootNode
  109.             ->fixXmlConfig('path')
  110.             ->children()
  111.                 ->variableNode('autoescape')->defaultValue('name')->end()
  112.                 ->scalarNode('autoescape_service')->defaultNull()->end()
  113.                 ->scalarNode('autoescape_service_method')->defaultNull()->end()
  114.                 ->scalarNode('base_template_class')->example('Twig\Template')->cannotBeEmpty()->end()
  115.                 ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
  116.                 ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
  117.                 ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
  118.                 ->booleanNode('strict_variables')
  119.                     ->defaultValue(function () {
  120.                         @trigger_error('Relying on the default value ("false") of the "twig.strict_variables" configuration option is deprecated since Symfony 4.1. You should use "%kernel.debug%" explicitly instead, which will be the new default in 5.0.'E_USER_DEPRECATED);
  121.                         return false;
  122.                     })
  123.                 ->end()
  124.                 ->scalarNode('auto_reload')->end()
  125.                 ->integerNode('optimizations')->min(-1)->end()
  126.                 ->scalarNode('default_path')
  127.                     ->info('The default path used to load templates')
  128.                     ->defaultValue('%kernel.project_dir%/templates')
  129.                 ->end()
  130.                 ->arrayNode('paths')
  131.                     ->normalizeKeys(false)
  132.                     ->useAttributeAsKey('paths')
  133.                     ->beforeNormalization()
  134.                         ->always()
  135.                         ->then(function ($paths) {
  136.                             $normalized = [];
  137.                             foreach ($paths as $path => $namespace) {
  138.                                 if (\is_array($namespace)) {
  139.                                     // xml
  140.                                     $path $namespace['value'];
  141.                                     $namespace $namespace['namespace'];
  142.                                 }
  143.                                 // path within the default namespace
  144.                                 if (ctype_digit((string) $path)) {
  145.                                     $path $namespace;
  146.                                     $namespace null;
  147.                                 }
  148.                                 $normalized[$path] = $namespace;
  149.                             }
  150.                             return $normalized;
  151.                         })
  152.                     ->end()
  153.                     ->prototype('variable')->end()
  154.                 ->end()
  155.             ->end()
  156.         ;
  157.     }
  158.     private function addTwigFormatOptions(ArrayNodeDefinition $rootNode)
  159.     {
  160.         $rootNode
  161.             ->children()
  162.                 ->arrayNode('date')
  163.                     ->info('The default format options used by the date filter')
  164.                     ->addDefaultsIfNotSet()
  165.                     ->children()
  166.                         ->scalarNode('format')->defaultValue('F j, Y H:i')->end()
  167.                         ->scalarNode('interval_format')->defaultValue('%d days')->end()
  168.                         ->scalarNode('timezone')
  169.                             ->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used')
  170.                             ->defaultNull()
  171.                         ->end()
  172.                     ->end()
  173.                 ->end()
  174.                 ->arrayNode('number_format')
  175.                     ->info('The default format options for the number_format filter')
  176.                     ->addDefaultsIfNotSet()
  177.                     ->children()
  178.                         ->integerNode('decimals')->defaultValue(0)->end()
  179.                         ->scalarNode('decimal_point')->defaultValue('.')->end()
  180.                         ->scalarNode('thousands_separator')->defaultValue(',')->end()
  181.                     ->end()
  182.                 ->end()
  183.             ->end()
  184.         ;
  185.     }
  186. }