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

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\Form;
  19. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  20. use Symfony\Component\Mailer\Mailer;
  21. use Symfony\Component\Translation\Translator;
  22. use Twig\Extension\ExtensionInterface;
  23. use Twig\Extension\RuntimeExtensionInterface;
  24. use Twig\Loader\LoaderInterface;
  25. /**
  26.  * TwigExtension.
  27.  *
  28.  * @author Fabien Potencier <fabien@symfony.com>
  29.  * @author Jeremy Mikola <jmikola@gmail.com>
  30.  */
  31. class TwigExtension extends Extension
  32. {
  33.     public function load(array $configsContainerBuilder $container)
  34.     {
  35.         if (!class_exists(InstalledVersions::class)) {
  36.             trigger_deprecation('symfony/twig-bundle''5.4''Configuring Symfony without the Composer Runtime API is deprecated. Consider upgrading to Composer 2.1 or later.');
  37.         }
  38.         $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  39.         $loader->load('twig.php');
  40.         if ($container::willBeAvailable('symfony/form'Form::class, ['symfony/twig-bundle'], true)) {
  41.             $loader->load('form.php');
  42.         }
  43.         if ($container::willBeAvailable('symfony/console'Application::class, ['symfony/twig-bundle'], true)) {
  44.             $loader->load('console.php');
  45.         }
  46.         if ($container::willBeAvailable('symfony/mailer'Mailer::class, ['symfony/twig-bundle'], true)) {
  47.             $loader->load('mailer.php');
  48.         }
  49.         if (!$container::willBeAvailable('symfony/translation'Translator::class, ['symfony/twig-bundle'], true)) {
  50.             $container->removeDefinition('twig.translation.extractor');
  51.         }
  52.         foreach ($configs as $key => $config) {
  53.             if (isset($config['globals'])) {
  54.                 foreach ($config['globals'] as $name => $value) {
  55.                     if (\is_array($value) && isset($value['key'])) {
  56.                         $configs[$key]['globals'][$name] = [
  57.                             'key' => $name,
  58.                             'value' => $value,
  59.                         ];
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         $configuration $this->getConfiguration($configs$container);
  65.         $config $this->processConfiguration($configuration$configs);
  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.         // register user-configured paths
  78.         foreach ($config['paths'] as $path => $namespace) {
  79.             if (!$namespace) {
  80.                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path]);
  81.             } else {
  82.                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path$namespace]);
  83.             }
  84.         }
  85.         // paths are modified in ExtensionPass if forms are enabled
  86.         $container->getDefinition('twig.template_iterator')->replaceArgument(1$config['paths']);
  87.         foreach ($this->getBundleTemplatePaths($container$config) as $name => $paths) {
  88.             $namespace $this->normalizeBundleName($name);
  89.             foreach ($paths as $path) {
  90.                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path$namespace]);
  91.             }
  92.             if ($paths) {
  93.                 // the last path must be the bundle views directory
  94.                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path'!'.$namespace]);
  95.             }
  96.         }
  97.         if (file_exists($defaultTwigPath)) {
  98.             $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$defaultTwigPath]);
  99.         }
  100.         $container->addResource(new FileExistenceResource($defaultTwigPath));
  101.         if (!empty($config['globals'])) {
  102.             $def $container->getDefinition('twig');
  103.             foreach ($config['globals'] as $key => $global) {
  104.                 if (isset($global['type']) && 'service' === $global['type']) {
  105.                     $def->addMethodCall('addGlobal', [$key, new Reference($global['id'])]);
  106.                 } else {
  107.                     $def->addMethodCall('addGlobal', [$key$global['value']]);
  108.                 }
  109.             }
  110.         }
  111.         if (isset($config['autoescape_service']) && isset($config['autoescape_service_method'])) {
  112.             $config['autoescape'] = [new Reference($config['autoescape_service']), $config['autoescape_service_method']];
  113.         }
  114.         $container->getDefinition('twig')->replaceArgument(1array_intersect_key($config, [
  115.             'debug' => true,
  116.             'charset' => true,
  117.             'base_template_class' => true,
  118.             'strict_variables' => true,
  119.             'autoescape' => true,
  120.             'cache' => true,
  121.             'auto_reload' => true,
  122.             'optimizations' => true,
  123.         ]));
  124.         $container->registerForAutoconfiguration(\Twig_ExtensionInterface::class)->addTag('twig.extension');
  125.         $container->registerForAutoconfiguration(\Twig_LoaderInterface::class)->addTag('twig.loader');
  126.         $container->registerForAutoconfiguration(ExtensionInterface::class)->addTag('twig.extension');
  127.         $container->registerForAutoconfiguration(LoaderInterface::class)->addTag('twig.loader');
  128.         $container->registerForAutoconfiguration(RuntimeExtensionInterface::class)->addTag('twig.runtime');
  129.         if (false === $config['cache']) {
  130.             $container->removeDefinition('twig.template_cache_warmer');
  131.         }
  132.     }
  133.     private function getBundleTemplatePaths(ContainerBuilder $container, array $config): array
  134.     {
  135.         $bundleHierarchy = [];
  136.         foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
  137.             $defaultOverrideBundlePath $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name;
  138.             if (file_exists($defaultOverrideBundlePath)) {
  139.                 $bundleHierarchy[$name][] = $defaultOverrideBundlePath;
  140.             }
  141.             $container->addResource(new FileExistenceResource($defaultOverrideBundlePath));
  142.             if (file_exists($dir $bundle['path'].'/Resources/views') || file_exists($dir $bundle['path'].'/templates')) {
  143.                 $bundleHierarchy[$name][] = $dir;
  144.             }
  145.             $container->addResource(new FileExistenceResource($dir));
  146.         }
  147.         return $bundleHierarchy;
  148.     }
  149.     private function normalizeBundleName(string $name): string
  150.     {
  151.         if (str_ends_with($name'Bundle')) {
  152.             $name substr($name0, -6);
  153.         }
  154.         return $name;
  155.     }
  156.     /**
  157.      * {@inheritdoc}
  158.      */
  159.     public function getXsdValidationBasePath()
  160.     {
  161.         return __DIR__.'/../Resources/config/schema';
  162.     }
  163.     public function getNamespace()
  164.     {
  165.         return 'http://symfony.com/schema/dic/twig';
  166.     }
  167. }