You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.5 KiB

3 years ago
  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. private $handlerService;
  25. private $rendererTag;
  26. public function __construct(string $handlerService = 'fragment.handler', string $rendererTag = 'kernel.fragment_renderer')
  27. {
  28. if (0 < \func_num_args()) {
  29. trigger_deprecation('symfony/http-kernel', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  30. }
  31. $this->handlerService = $handlerService;
  32. $this->rendererTag = $rendererTag;
  33. }
  34. public function process(ContainerBuilder $container)
  35. {
  36. if (!$container->hasDefinition($this->handlerService)) {
  37. return;
  38. }
  39. $definition = $container->getDefinition($this->handlerService);
  40. $renderers = [];
  41. foreach ($container->findTaggedServiceIds($this->rendererTag, true) as $id => $tags) {
  42. $def = $container->getDefinition($id);
  43. $class = $container->getParameterBag()->resolveValue($def->getClass());
  44. if (!$r = $container->getReflectionClass($class)) {
  45. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  46. }
  47. if (!$r->isSubclassOf(FragmentRendererInterface::class)) {
  48. throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, FragmentRendererInterface::class));
  49. }
  50. foreach ($tags as $tag) {
  51. $renderers[$tag['alias']] = new Reference($id);
  52. }
  53. }
  54. $definition->replaceArgument(0, ServiceLocatorTagPass::register($container, $renderers));
  55. }
  56. }