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

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.         unset(
  123.             $config['form'],
  124.             $config['globals'],
  125.             $config['extensions']
  126.         );
  127.         if (isset($config['autoescape_service']) && isset($config['autoescape_service_method'])) {
  128.             $config['autoescape'] = [new Reference($config['autoescape_service']), $config['autoescape_service_method']];
  129.         }
  130.         unset($config['autoescape_service'], $config['autoescape_service_method']);
  131.         $container->getDefinition('twig')->replaceArgument(1$config);
  132.         $container->registerForAutoconfiguration(\Twig_ExtensionInterface::class)->addTag('twig.extension');
  133.         $container->registerForAutoconfiguration(\Twig_LoaderInterface::class)->addTag('twig.loader');
  134.         $container->registerForAutoconfiguration(ExtensionInterface::class)->addTag('twig.extension');
  135.         $container->registerForAutoconfiguration(LoaderInterface::class)->addTag('twig.loader');
  136.         $container->registerForAutoconfiguration(RuntimeExtensionInterface::class)->addTag('twig.runtime');
  137.         if (false === $config['cache']) {
  138.             $container->removeDefinition('twig.cache_warmer');
  139.             $container->removeDefinition('twig.template_cache_warmer');
  140.         }
  141.     }
  142.     private function getBundleTemplatePaths(ContainerBuilder $container, array $config)
  143.     {
  144.         $bundleHierarchy = [];
  145.         foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
  146.             $defaultOverrideBundlePath $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name;
  147.             if (file_exists($dir $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
  148.                 @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);
  149.                 $bundleHierarchy[$name][] = $dir;
  150.             }
  151.             $container->addResource(new FileExistenceResource($dir));
  152.             if (file_exists($defaultOverrideBundlePath)) {
  153.                 $bundleHierarchy[$name][] = $defaultOverrideBundlePath;
  154.             }
  155.             $container->addResource(new FileExistenceResource($defaultOverrideBundlePath));
  156.             if (file_exists($dir $bundle['path'].'/Resources/views')) {
  157.                 $bundleHierarchy[$name][] = $dir;
  158.             }
  159.             $container->addResource(new FileExistenceResource($dir));
  160.         }
  161.         return $bundleHierarchy;
  162.     }
  163.     private function normalizeBundleName($name)
  164.     {
  165.         if ('Bundle' === substr($name, -6)) {
  166.             $name substr($name0, -6);
  167.         }
  168.         return $name;
  169.     }
  170.     /**
  171.      * {@inheritdoc}
  172.      */
  173.     public function getXsdValidationBasePath()
  174.     {
  175.         return __DIR__.'/../Resources/config/schema';
  176.     }
  177.     public function getNamespace()
  178.     {
  179.         return 'http://symfony.com/schema/dic/twig';
  180.     }
  181. }