ExtensionPass.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Config\Resource\ClassExistenceResource;
  12. use Symfony\Component\DependencyInjection\Alias;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  17. use Symfony\Component\Stopwatch\Stopwatch;
  18. use Symfony\Component\Yaml\Parser as YamlParser;
  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 ($container->has('form.extension')) {
  27. $container->getDefinition('twig.extension.form')->addTag('twig.extension');
  28. $reflClass = new \ReflectionClass('Symfony\Bridge\Twig\Extension\FormExtension');
  29. $container->getDefinition('twig.loader.filesystem')->addMethodCall('addPath', array(dirname(dirname($reflClass->getFileName())).'/Resources/views/Form'));
  30. }
  31. if ($container->has('translator')) {
  32. $container->getDefinition('twig.extension.trans')->addTag('twig.extension');
  33. }
  34. if ($container->has('router')) {
  35. $container->getDefinition('twig.extension.routing')->addTag('twig.extension');
  36. }
  37. if ($container->has('fragment.handler')) {
  38. $container->getDefinition('twig.extension.httpkernel')->addTag('twig.extension');
  39. // inject Twig in the hinclude service if Twig is the only registered templating engine
  40. if (
  41. !$container->hasParameter('templating.engines')
  42. || array('twig') == $container->getParameter('templating.engines')
  43. ) {
  44. $container->getDefinition('fragment.renderer.hinclude')
  45. ->addTag('kernel.fragment_renderer', array('alias' => 'hinclude'))
  46. ->replaceArgument(0, new Reference('twig'))
  47. ;
  48. }
  49. }
  50. if ($container->has('request_stack')) {
  51. $container->getDefinition('twig.extension.httpfoundation')->addTag('twig.extension');
  52. }
  53. if ($container->getParameter('kernel.debug')) {
  54. $container->getDefinition('twig.extension.profiler')->addTag('twig.extension');
  55. $container->getDefinition('twig.extension.debug')->addTag('twig.extension');
  56. }
  57. $composerRootDir = $this->getComposerRootDir($container->getParameter('kernel.root_dir'));
  58. $loader = $container->getDefinition('twig.loader.filesystem');
  59. $loader->replaceArgument(2, $composerRootDir);
  60. if (!$container->has('templating')) {
  61. $loader = $container->getDefinition('twig.loader.native_filesystem');
  62. $loader->replaceArgument(1, $composerRootDir);
  63. $loader->addTag('twig.loader');
  64. $loader->setMethodCalls($container->getDefinition('twig.loader.filesystem')->getMethodCalls());
  65. $container->setAlias('twig.loader.filesystem', new Alias('twig.loader.native_filesystem', false));
  66. }
  67. if ($container->has('assets.packages')) {
  68. $container->getDefinition('twig.extension.assets')->addTag('twig.extension');
  69. }
  70. $container->addResource(new ClassExistenceResource(YamlParser::class));
  71. if (class_exists(YamlParser::class)) {
  72. $container->getDefinition('twig.extension.yaml')->addTag('twig.extension');
  73. }
  74. $container->addResource(new ClassExistenceResource(Stopwatch::class));
  75. if (class_exists(Stopwatch::class)) {
  76. $container->getDefinition('twig.extension.debug.stopwatch')->addTag('twig.extension');
  77. }
  78. $container->addResource(new ClassExistenceResource(ExpressionLanguage::class));
  79. if (class_exists(ExpressionLanguage::class)) {
  80. $container->getDefinition('twig.extension.expression')->addTag('twig.extension');
  81. }
  82. }
  83. private function getComposerRootDir($rootDir)
  84. {
  85. $dir = $rootDir;
  86. while (!file_exists($dir.'/composer.json')) {
  87. if ($dir === dirname($dir)) {
  88. return $rootDir;
  89. }
  90. $dir = dirname($dir);
  91. }
  92. return $dir;
  93. }
  94. }