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

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