vendor/symfony/twig-bundle/DependencyInjection/Compiler/ExtensionPass.php line 46

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\Compiler;
  11. use Symfony\Component\Asset\Packages;
  12. use Symfony\Component\DependencyInjection\Alias;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\ExpressionLanguage\Expression;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Workflow\Workflow;
  18. use Symfony\Component\Yaml\Yaml;
  19. /**
  20.  * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  21.  */
  22. class ExtensionPass implements CompilerPassInterface
  23. {
  24.     public function process(ContainerBuilder $container)
  25.     {
  26.         if (!class_exists(Packages::class)) {
  27.             $container->removeDefinition('twig.extension.assets');
  28.         }
  29.         if (!class_exists(Expression::class)) {
  30.             $container->removeDefinition('twig.extension.expression');
  31.         }
  32.         if (!interface_exists(UrlGeneratorInterface::class)) {
  33.             $container->removeDefinition('twig.extension.routing');
  34.         }
  35.         if (!class_exists(Yaml::class)) {
  36.             $container->removeDefinition('twig.extension.yaml');
  37.         }
  38.         $viewDir \dirname((new \ReflectionClass(\Symfony\Bridge\Twig\Extension\FormExtension::class))->getFileName(), 2).'/Resources/views';
  39.         $templateIterator $container->getDefinition('twig.template_iterator');
  40.         $templatePaths $templateIterator->getArgument(1);
  41.         $loader $container->getDefinition('twig.loader.native_filesystem');
  42.         if ($container->has('mailer')) {
  43.             $emailPath $viewDir.'/Email';
  44.             $loader->addMethodCall('addPath', [$emailPath'email']);
  45.             $loader->addMethodCall('addPath', [$emailPath'!email']);
  46.             $templatePaths[$emailPath] = 'email';
  47.         }
  48.         if ($container->has('form.extension')) {
  49.             $container->getDefinition('twig.extension.form')->addTag('twig.extension');
  50.             $coreThemePath $viewDir.'/Form';
  51.             $loader->addMethodCall('addPath', [$coreThemePath]);
  52.             $templatePaths[$coreThemePath] = null;
  53.         }
  54.         $templateIterator->replaceArgument(1$templatePaths);
  55.         if ($container->has('router')) {
  56.             $container->getDefinition('twig.extension.routing')->addTag('twig.extension');
  57.         }
  58.         if ($container->has('fragment.handler')) {
  59.             $container->getDefinition('twig.extension.httpkernel')->addTag('twig.extension');
  60.             $container->getDefinition('twig.runtime.httpkernel')->addTag('twig.runtime');
  61.             if ($container->hasDefinition('fragment.renderer.hinclude')) {
  62.                 $container->getDefinition('fragment.renderer.hinclude')
  63.                     ->addTag('kernel.fragment_renderer', ['alias' => 'hinclude'])
  64.                 ;
  65.             }
  66.         }
  67.         if ($container->has('request_stack')) {
  68.             $container->getDefinition('twig.extension.httpfoundation')->addTag('twig.extension');
  69.         }
  70.         if ($container->getParameter('kernel.debug')) {
  71.             $container->getDefinition('twig.extension.profiler')->addTag('twig.extension');
  72.             // only register if the improved version from DebugBundle is *not* present
  73.             if (!$container->has('twig.extension.dump')) {
  74.                 $container->getDefinition('twig.extension.debug')->addTag('twig.extension');
  75.             }
  76.         }
  77.         if ($container->has('web_link.add_link_header_listener')) {
  78.             $container->getDefinition('twig.extension.weblink')->addTag('twig.extension');
  79.         }
  80.         $container->setAlias('twig.loader.filesystem', new Alias('twig.loader.native_filesystem'false));
  81.         if ($container->has('assets.packages')) {
  82.             $container->getDefinition('twig.extension.assets')->addTag('twig.extension');
  83.         }
  84.         if ($container->hasDefinition('twig.extension.yaml')) {
  85.             $container->getDefinition('twig.extension.yaml')->addTag('twig.extension');
  86.         }
  87.         if (class_exists(\Symfony\Component\Stopwatch\Stopwatch::class)) {
  88.             $container->getDefinition('twig.extension.debug.stopwatch')->addTag('twig.extension');
  89.         }
  90.         if ($container->hasDefinition('twig.extension.expression')) {
  91.             $container->getDefinition('twig.extension.expression')->addTag('twig.extension');
  92.         }
  93.         if (!class_exists(Workflow::class) || !$container->has('workflow.registry')) {
  94.             $container->removeDefinition('workflow.twig_extension');
  95.         } else {
  96.             $container->getDefinition('workflow.twig_extension')->addTag('twig.extension');
  97.         }
  98.         if ($container->has('serializer')) {
  99.             $container->getDefinition('twig.runtime.serializer')->addTag('twig.runtime');
  100.             $container->getDefinition('twig.extension.serializer')->addTag('twig.extension');
  101.         }
  102.     }
  103. }