TwigEnvironmentPass.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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\Reference;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. /**
  15. * Adds tagged twig.extension services to twig service.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class TwigEnvironmentPass implements CompilerPassInterface
  20. {
  21. public function process(ContainerBuilder $container)
  22. {
  23. if (false === $container->hasDefinition('twig')) {
  24. return;
  25. }
  26. $definition = $container->getDefinition('twig');
  27. // Extensions must always be registered before everything else.
  28. // For instance, global variable definitions must be registered
  29. // afterward. If not, the globals from the extensions will never
  30. // be registered.
  31. $calls = $definition->getMethodCalls();
  32. $definition->setMethodCalls(array());
  33. foreach ($container->findTaggedServiceIds('twig.extension') as $id => $attributes) {
  34. $definition->addMethodCall('addExtension', array(new Reference($id)));
  35. }
  36. $definition->setMethodCalls(array_merge($definition->getMethodCalls(), $calls));
  37. }
  38. }