RuntimeLoaderPass.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. /**
  14. * Registers Twig runtime services.
  15. */
  16. class RuntimeLoaderPass implements CompilerPassInterface
  17. {
  18. public function process(ContainerBuilder $container)
  19. {
  20. if (!$container->hasDefinition('twig.runtime_loader')) {
  21. return;
  22. }
  23. $definition = $container->getDefinition('twig.runtime_loader');
  24. $mapping = array();
  25. foreach ($container->findTaggedServiceIds('twig.runtime') as $id => $attributes) {
  26. $def = $container->getDefinition($id);
  27. if (!$def->isPublic()) {
  28. throw new InvalidArgumentException(sprintf('The service "%s" must be public as it can be lazy-loaded.', $id));
  29. }
  30. if ($def->isAbstract()) {
  31. throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as it can be lazy-loaded.', $id));
  32. }
  33. $mapping[$def->getClass()] = $id;
  34. }
  35. $definition->replaceArgument(1, $mapping);
  36. }
  37. }