vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php line 36

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\Bundle\TwigBundle\Loader\NativeFilesystemLoader;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\Config\Resource\FileExistenceResource;
  14. use Symfony\Component\Console\Application;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  19. use Symfony\Component\Mailer\Mailer;
  20. use Symfony\Component\Translation\Translator;
  21. use Twig\Extension\ExtensionInterface;
  22. use Twig\Extension\RuntimeExtensionInterface;
  23. use Twig\Loader\LoaderInterface;
  24. /**
  25.  * TwigExtension.
  26.  *
  27.  * @author Fabien Potencier <fabien@symfony.com>
  28.  * @author Jeremy Mikola <jmikola@gmail.com>
  29.  */
  30. class TwigExtension extends Extension
  31. {
  32.     public function load(array $configsContainerBuilder $container)
  33.     {
  34.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  35.         $loader->load('twig.xml');
  36.         if (class_exists('Symfony\Component\Form\Form')) {
  37.             $loader->load('form.xml');
  38.         }
  39.         if (interface_exists('Symfony\Component\Templating\EngineInterface')) {
  40.             $loader->load('templating.xml');
  41.         }
  42.         if (class_exists(Application::class)) {
  43.             $loader->load('console.xml');
  44.         }
  45.         if (class_exists(Mailer::class)) {
  46.             $loader->load('mailer.xml');
  47.         }
  48.         if (!class_exists(Translator::class)) {
  49.             $container->removeDefinition('twig.translation.extractor');
  50.         }
  51.         foreach ($configs as $key => $config) {
  52.             if (isset($config['globals'])) {
  53.                 foreach ($config['globals'] as $name => $value) {
  54.                     if (\is_array($value) && isset($value['key'])) {
  55.                         $configs[$key]['globals'][$name] = [
  56.                             'key' => $name,
  57.                             'value' => $value,
  58.                         ];
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.         $configuration $this->getConfiguration($configs$container);
  64.         $config $this->processConfiguration($configuration$configs);
  65.         $container->setParameter('twig.exception_listener.controller'$config['exception_controller']);
  66.         $container->setParameter('twig.form.resources'$config['form_themes']);
  67.         $container->setParameter('twig.default_path'$config['default_path']);
  68.         $defaultTwigPath $container->getParameterBag()->resolveValue($config['default_path']);
  69.         $envConfiguratorDefinition $container->getDefinition('twig.configurator.environment');
  70.         $envConfiguratorDefinition->replaceArgument(0$config['date']['format']);
  71.         $envConfiguratorDefinition->replaceArgument(1$config['date']['interval_format']);
  72.         $envConfiguratorDefinition->replaceArgument(2$config['date']['timezone']);
  73.         $envConfiguratorDefinition->replaceArgument(3$config['number_format']['decimals']);
  74.         $envConfiguratorDefinition->replaceArgument(4$config['number_format']['decimal_point']);
  75.         $envConfiguratorDefinition->replaceArgument(5$config['number_format']['thousands_separator']);
  76.         $twigFilesystemLoaderDefinition $container->getDefinition('twig.loader.native_filesystem');
  77.         if ($container->getParameter('kernel.debug')) {
  78.             $twigFilesystemLoaderDefinition->setClass(NativeFilesystemLoader::class);
  79.         }
  80.         // register user-configured paths
  81.         foreach ($config['paths'] as $path => $namespace) {
  82.             if (!$namespace) {
  83.                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path]);
  84.             } else {
  85.                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path$namespace]);
  86.             }
  87.         }
  88.         // paths are modified in ExtensionPass if forms are enabled
  89.         $container->getDefinition('twig.cache_warmer')->replaceArgument(2$config['paths']);
  90.         $container->getDefinition('twig.template_iterator')->replaceArgument(2$config['paths']);
  91.         foreach ($this->getBundleTemplatePaths($container$config) as $name => $paths) {
  92.             $namespace $this->normalizeBundleName($name);
  93.             foreach ($paths as $path) {
  94.                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path$namespace]);
  95.             }
  96.             if ($paths) {
  97.                 // the last path must be the bundle views directory
  98.                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path'!'.$namespace]);
  99.             }
  100.         }
  101.         if (file_exists($dir $container->getParameter('kernel.root_dir').'/Resources/views')) {
  102.             if ($dir !== $defaultTwigPath) {
  103.                 @trigger_error(sprintf('Loading Twig templates from the "%s" directory is deprecated since Symfony 4.2, use "%s" instead.'$dir$defaultTwigPath), E_USER_DEPRECATED);
  104.             }
  105.             $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$dir]);
  106.         }
  107.         $container->addResource(new FileExistenceResource($dir));
  108.         if (file_exists($defaultTwigPath)) {
  109.             $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$defaultTwigPath]);
  110.         }
  111.         $container->addResource(new FileExistenceResource($defaultTwigPath));
  112.         if (!empty($config['globals'])) {
  113.             $def $container->getDefinition('twig');
  114.             foreach ($config['globals'] as $key => $global) {
  115.                 if (isset($global['type']) && 'service' === $global['type']) {
  116.                     $def->addMethodCall('addGlobal', [$key, new Reference($global['id'])]);
  117.                 } else {
  118.                     $def->addMethodCall('addGlobal', [$key$global['value']]);
  119.                 }
  120.             }
  121.         }
  122.         if (isset($config['autoescape_service']) && isset($config['autoescape_service_method'])) {
  123.             $config['autoescape'] = [new Reference($config['autoescape_service']), $config['autoescape_service_method']];
  124.         }
  125.         $container->getDefinition('twig')->replaceArgument(1array_intersect_key($config, [
  126.             'debug' => true,
  127.             'charset' => true,
  128.             'base_template_class' => true,
  129.             'strict_variables' => true,
  130.             'autoescape' => true,
  131.             'cache' => true,
  132.             'auto_reload' => true,
  133.             'optimizations' => true,
  134.         ]));
  135.         $container->registerForAutoconfiguration(\Twig_ExtensionInterface::class)->addTag('twig.extension');
  136.         $container->registerForAutoconfiguration(\Twig_LoaderInterface::class)->addTag('twig.loader');
  137.         $container->registerForAutoconfiguration(ExtensionInterface::class)->addTag('twig.extension');
  138.         $container->registerForAutoconfiguration(LoaderInterface::class)->addTag('twig.loader');
  139.         $container->registerForAutoconfiguration(RuntimeExtensionInterface::class)->addTag('twig.runtime');
  140.         if (false === $config['cache']) {
  141.             $container->removeDefinition('twig.cache_warmer');
  142.             $container->removeDefinition('twig.template_cache_warmer');
  143.         }
  144.     }
  145.     private function getBundleTemplatePaths(ContainerBuilder $container, array $config): array
  146.     {
  147.         $bundleHierarchy = [];
  148.         foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
  149.             $defaultOverrideBundlePath $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name;
  150.             if (file_exists($dir $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
  151.                 @trigger_error(sprintf('Loading Twig templates for "%s" from the "%s" directory is deprecated since Symfony 4.2, use "%s" instead.'$name$dir$defaultOverrideBundlePath), E_USER_DEPRECATED);
  152.                 $bundleHierarchy[$name][] = $dir;
  153.             }
  154.             $container->addResource(new FileExistenceResource($dir));
  155.             if (file_exists($defaultOverrideBundlePath)) {
  156.                 $bundleHierarchy[$name][] = $defaultOverrideBundlePath;
  157.             }
  158.             $container->addResource(new FileExistenceResource($defaultOverrideBundlePath));
  159.             if (file_exists($dir $bundle['path'].'/Resources/views') || file_exists($dir $bundle['path'].'/templates')) {
  160.                 $bundleHierarchy[$name][] = $dir;
  161.             }
  162.             $container->addResource(new FileExistenceResource($dir));
  163.         }
  164.         return $bundleHierarchy;
  165.     }
  166.     private function normalizeBundleName(string $name): string
  167.     {
  168.         if ('Bundle' === substr($name, -6)) {
  169.             $name substr($name0, -6);
  170.         }
  171.         return $name;
  172.     }
  173.     /**
  174.      * {@inheritdoc}
  175.      */
  176.     public function getXsdValidationBasePath()
  177.     {
  178.         return __DIR__.'/../Resources/config/schema';
  179.     }
  180.     public function getNamespace()
  181.     {
  182.         return 'http://symfony.com/schema/dic/twig';
  183.     }
  184. }