ExtensionPass.php 4.7 KB

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