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