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

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('twig');
  29.         $rootNode $treeBuilder->getRootNode();
  30.         $rootNode
  31.             ->children()
  32.                 ->scalarNode('exception_controller')
  33.                     ->defaultValue(static function () {
  34.                         @trigger_error('The "twig.exception_controller" configuration key has been deprecated in Symfony 4.4, set it to "null" and use "framework.error_controller" configuration key instead.'E_USER_DEPRECATED);
  35.                         return 'twig.controller.exception::showAction';
  36.                     })
  37.                     ->validate()
  38.                         ->ifTrue(static function ($v) { return null !== $v; })
  39.                         ->then(static function ($v) {
  40.                             @trigger_error('The "twig.exception_controller" configuration key has been deprecated in Symfony 4.4, set it to "null" and use "framework.error_controller" configuration key instead.'E_USER_DEPRECATED);
  41.                             return $v;
  42.                         })
  43.                     ->end()
  44.                 ->end()
  45.             ->end()
  46.         ;
  47.         $this->addFormThemesSection($rootNode);
  48.         $this->addGlobalsSection($rootNode);
  49.         $this->addTwigOptions($rootNode);
  50.         $this->addTwigFormatOptions($rootNode);
  51.         return $treeBuilder;
  52.     }
  53.     private function addFormThemesSection(ArrayNodeDefinition $rootNode)
  54.     {
  55.         $rootNode
  56.             ->fixXmlConfig('form_theme')
  57.             ->children()
  58.                 ->arrayNode('form_themes')
  59.                     ->addDefaultChildrenIfNoneSet()
  60.                     ->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
  61.                     ->example(['@My/form.html.twig'])
  62.                     ->validate()
  63.                         ->ifTrue(function ($v) { return !\in_array('form_div_layout.html.twig'$v); })
  64.                         ->then(function ($v) {
  65.                             return array_merge(['form_div_layout.html.twig'], $v);
  66.                         })
  67.                     ->end()
  68.                 ->end()
  69.             ->end()
  70.         ;
  71.     }
  72.     private function addGlobalsSection(ArrayNodeDefinition $rootNode)
  73.     {
  74.         $rootNode
  75.             ->fixXmlConfig('global')
  76.             ->children()
  77.                 ->arrayNode('globals')
  78.                     ->normalizeKeys(false)
  79.                     ->useAttributeAsKey('key')
  80.                     ->example(['foo' => '"@bar"''pi' => 3.14])
  81.                     ->prototype('array')
  82.                         ->normalizeKeys(false)
  83.                         ->beforeNormalization()
  84.                             ->ifTrue(function ($v) { return \is_string($v) && === strpos($v'@'); })
  85.                             ->then(function ($v) {
  86.                                 if (=== strpos($v'@@')) {
  87.                                     return substr($v1);
  88.                                 }
  89.                                 return ['id' => substr($v1), 'type' => 'service'];
  90.                             })
  91.                         ->end()
  92.                         ->beforeNormalization()
  93.                             ->ifTrue(function ($v) {
  94.                                 if (\is_array($v)) {
  95.                                     $keys array_keys($v);
  96.                                     sort($keys);
  97.                                     return $keys !== ['id''type'] && $keys !== ['value'];
  98.                                 }
  99.                                 return true;
  100.                             })
  101.                             ->then(function ($v) { return ['value' => $v]; })
  102.                         ->end()
  103.                         ->children()
  104.                             ->scalarNode('id')->end()
  105.                             ->scalarNode('type')
  106.                                 ->validate()
  107.                                     ->ifNotInArray(['service'])
  108.                                     ->thenInvalid('The %s type is not supported')
  109.                                 ->end()
  110.                             ->end()
  111.                             ->variableNode('value')->end()
  112.                         ->end()
  113.                     ->end()
  114.                 ->end()
  115.             ->end()
  116.         ;
  117.     }
  118.     private function addTwigOptions(ArrayNodeDefinition $rootNode)
  119.     {
  120.         $rootNode
  121.             ->fixXmlConfig('path')
  122.             ->children()
  123.                 ->variableNode('autoescape')->defaultValue('name')->end()
  124.                 ->scalarNode('autoescape_service')->defaultNull()->end()
  125.                 ->scalarNode('autoescape_service_method')->defaultNull()->end()
  126.                 ->scalarNode('base_template_class')->example('Twig\Template')->cannotBeEmpty()->end()
  127.                 ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
  128.                 ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
  129.                 ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
  130.                 ->booleanNode('strict_variables')
  131.                     ->defaultValue(function () {
  132.                         @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);
  133.                         return false;
  134.                     })
  135.                 ->end()
  136.                 ->scalarNode('auto_reload')->end()
  137.                 ->integerNode('optimizations')->min(-1)->end()
  138.                 ->scalarNode('default_path')
  139.                     ->info('The default path used to load templates')
  140.                     ->defaultValue('%kernel.project_dir%/templates')
  141.                 ->end()
  142.                 ->arrayNode('paths')
  143.                     ->normalizeKeys(false)
  144.                     ->useAttributeAsKey('paths')
  145.                     ->beforeNormalization()
  146.                         ->always()
  147.                         ->then(function ($paths) {
  148.                             $normalized = [];
  149.                             foreach ($paths as $path => $namespace) {
  150.                                 if (\is_array($namespace)) {
  151.                                     // xml
  152.                                     $path $namespace['value'];
  153.                                     $namespace $namespace['namespace'];
  154.                                 }
  155.                                 // path within the default namespace
  156.                                 if (ctype_digit((string) $path)) {
  157.                                     $path $namespace;
  158.                                     $namespace null;
  159.                                 }
  160.                                 $normalized[$path] = $namespace;
  161.                             }
  162.                             return $normalized;
  163.                         })
  164.                     ->end()
  165.                     ->prototype('variable')->end()
  166.                 ->end()
  167.             ->end()
  168.         ;
  169.     }
  170.     private function addTwigFormatOptions(ArrayNodeDefinition $rootNode)
  171.     {
  172.         $rootNode
  173.             ->children()
  174.                 ->arrayNode('date')
  175.                     ->info('The default format options used by the date filter')
  176.                     ->addDefaultsIfNotSet()
  177.                     ->children()
  178.                         ->scalarNode('format')->defaultValue('F j, Y H:i')->end()
  179.                         ->scalarNode('interval_format')->defaultValue('%d days')->end()
  180.                         ->scalarNode('timezone')
  181.                             ->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used')
  182.                             ->defaultNull()
  183.                         ->end()
  184.                     ->end()
  185.                 ->end()
  186.                 ->arrayNode('number_format')
  187.                     ->info('The default format options for the number_format filter')
  188.                     ->addDefaultsIfNotSet()
  189.                     ->children()
  190.                         ->integerNode('decimals')->defaultValue(0)->end()
  191.                         ->scalarNode('decimal_point')->defaultValue('.')->end()
  192.                         ->scalarNode('thousands_separator')->defaultValue(',')->end()
  193.                     ->end()
  194.                 ->end()
  195.             ->end()
  196.         ;
  197.     }
  198. }