FragmentRendererPass.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. public function process(ContainerBuilder $container): void
  25. {
  26. if (!$container->hasDefinition('fragment.handler')) {
  27. return;
  28. }
  29. $definition = $container->getDefinition('fragment.handler');
  30. $renderers = [];
  31. foreach ($container->findTaggedServiceIds('kernel.fragment_renderer', true) as $id => $tags) {
  32. $def = $container->getDefinition($id);
  33. $class = $container->getParameterBag()->resolveValue($def->getClass());
  34. if (!$r = $container->getReflectionClass($class)) {
  35. throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  36. }
  37. if (!$r->isSubclassOf(FragmentRendererInterface::class)) {
  38. throw new InvalidArgumentException(\sprintf('Service "%s" must implement interface "%s".', $id, FragmentRendererInterface::class));
  39. }
  40. foreach ($tags as $tag) {
  41. $renderers[$tag['alias']] = new Reference($id);
  42. }
  43. }
  44. $definition->replaceArgument(0, ServiceLocatorTagPass::register($container, $renderers));
  45. }
  46. }