FragmentRendererPass.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Component\HttpKernel\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
  17. /**
  18. * Adds services tagged kernel.fragment_renderer as HTTP content rendering strategies.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class FragmentRendererPass implements CompilerPassInterface
  23. {
  24. /**
  25. * @return void
  26. */
  27. public function process(ContainerBuilder $container)
  28. {
  29. if (!$container->hasDefinition('fragment.handler')) {
  30. return;
  31. }
  32. $definition = $container->getDefinition('fragment.handler');
  33. $renderers = [];
  34. foreach ($container->findTaggedServiceIds('kernel.fragment_renderer', true) as $id => $tags) {
  35. $def = $container->getDefinition($id);
  36. $class = $container->getParameterBag()->resolveValue($def->getClass());
  37. if (!$r = $container->getReflectionClass($class)) {
  38. throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  39. }
  40. if (!$r->isSubclassOf(FragmentRendererInterface::class)) {
  41. throw new InvalidArgumentException(\sprintf('Service "%s" must implement interface "%s".', $id, FragmentRendererInterface::class));
  42. }
  43. foreach ($tags as $tag) {
  44. $renderers[$tag['alias']] = new Reference($id);
  45. }
  46. }
  47. $definition->replaceArgument(0, ServiceLocatorTagPass::register($container, $renderers));
  48. }
  49. }