vendor/symfony/twig-bundle/DependencyInjection/TwigExtension.php line 43

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 Composer\InstalledVersions;
  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\PhpFileLoader;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. use Symfony\Component\Form\AbstractRendererEngine;
  19. use Symfony\Component\Form\Form;
  20. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  21. use Symfony\Component\Mailer\Mailer;
  22. use Symfony\Component\Translation\Translator;
  23. use Symfony\Contracts\Service\ResetInterface;
  24. use Twig\Environment;
  25. use Twig\Extension\ExtensionInterface;
  26. use Twig\Extension\RuntimeExtensionInterface;
  27. use Twig\Loader\LoaderInterface;
  28. /**
  29.  * TwigExtension.
  30.  *
  31.  * @author Fabien Potencier <fabien@symfony.com>
  32.  * @author Jeremy Mikola <jmikola@gmail.com>
  33.  */
  34. class TwigExtension extends Extension
  35. {
  36.     public function load(array $configsContainerBuilder $container)
  37.     {
  38.         if (!class_exists(InstalledVersions::class)) {
  39.             trigger_deprecation('symfony/twig-bundle''5.4''Configuring Symfony without the Composer Runtime API is deprecated. Consider upgrading to Composer 2.1 or later.');
  40.         }
  41.         $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  42.         $loader->load('twig.php');
  43.         if (method_exists(Environment::class, 'resetGlobals')) {
  44.             $container->getDefinition('twig')->addTag('kernel.reset', ['method' => 'resetGlobals']);
  45.         }
  46.         if ($container::willBeAvailable('symfony/form'Form::class, ['symfony/twig-bundle'], true)) {
  47.             $loader->load('form.php');
  48.             if (is_subclass_of(AbstractRendererEngine::class, ResetInterface::class)) {
  49.                 $container->getDefinition('twig.form.engine')->addTag('kernel.reset', [
  50.                     'method' => 'reset',
  51.                 ]);
  52.             }
  53.         }
  54.         if ($container::willBeAvailable('symfony/console'Application::class, ['symfony/twig-bundle'], true)) {
  55.             $loader->load('console.php');
  56.         }
  57.         if ($container::willBeAvailable('symfony/mailer'Mailer::class, ['symfony/twig-bundle'], true)) {
  58.             $loader->load('mailer.php');
  59.         }
  60.         if (!$container::willBeAvailable('symfony/translation'Translator::class, ['symfony/twig-bundle'], true)) {
  61.             $container->removeDefinition('twig.translation.extractor');
  62.         }
  63.         foreach ($configs as $key => $config) {
  64.             if (isset($config['globals'])) {
  65.                 foreach ($config['globals'] as $name => $value) {
  66.                     if (\is_array($value) && isset($value['key'])) {
  67.                         $configs[$key]['globals'][$name] = [
  68.                             'key' => $name,
  69.                             'value' => $value,
  70.                         ];
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.         $configuration $this->getConfiguration($configs$container);
  76.         $config $this->processConfiguration($configuration$configs);
  77.         $container->setParameter('twig.form.resources'$config['form_themes']);
  78.         $container->setParameter('twig.default_path'$config['default_path']);
  79.         $defaultTwigPath $container->getParameterBag()->resolveValue($config['default_path']);
  80.         $envConfiguratorDefinition $container->getDefinition('twig.configurator.environment');
  81.         $envConfiguratorDefinition->replaceArgument(0$config['date']['format']);
  82.         $envConfiguratorDefinition->replaceArgument(1$config['date']['interval_format']);
  83.         $envConfiguratorDefinition->replaceArgument(2$config['date']['timezone']);
  84.         $envConfiguratorDefinition->replaceArgument(3$config['number_format']['decimals']);
  85.         $envConfiguratorDefinition->replaceArgument(4$config['number_format']['decimal_point']);
  86.         $envConfiguratorDefinition->replaceArgument(5$config['number_format']['thousands_separator']);
  87.         $twigFilesystemLoaderDefinition $container->getDefinition('twig.loader.native_filesystem');
  88.         // register user-configured paths
  89.         foreach ($config['paths'] as $path => $namespace) {
  90.             if (!$namespace) {
  91.                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path]);
  92.             } else {
  93.                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path$namespace]);
  94.             }
  95.         }
  96.         // paths are modified in ExtensionPass if forms are enabled
  97.         $container->getDefinition('twig.template_iterator')->replaceArgument(1$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($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.template_cache_warmer');
  142.         }
  143.     }
  144.     private function getBundleTemplatePaths(ContainerBuilder $container, array $config): array
  145.     {
  146.         $bundleHierarchy = [];
  147.         foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
  148.             $defaultOverrideBundlePath $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name;
  149.             if (file_exists($defaultOverrideBundlePath)) {
  150.                 $bundleHierarchy[$name][] = $defaultOverrideBundlePath;
  151.             }
  152.             $container->addResource(new FileExistenceResource($defaultOverrideBundlePath));
  153.             if (file_exists($dir $bundle['path'].'/Resources/views') || file_exists($dir $bundle['path'].'/templates')) {
  154.                 $bundleHierarchy[$name][] = $dir;
  155.             }
  156.             $container->addResource(new FileExistenceResource($dir));
  157.         }
  158.         return $bundleHierarchy;
  159.     }
  160.     private function normalizeBundleName(string $name): string
  161.     {
  162.         if (str_ends_with($name'Bundle')) {
  163.             $name substr($name0, -6);
  164.         }
  165.         return $name;
  166.     }
  167.     /**
  168.      * {@inheritdoc}
  169.      */
  170.     public function getXsdValidationBasePath()
  171.     {
  172.         return __DIR__.'/../Resources/config/schema';
  173.     }
  174.     public function getNamespace()
  175.     {
  176.         return 'http://symfony.com/schema/dic/twig';
  177.     }
  178. }