菜谱项目

FragmentRendererPass.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\ServiceLocatorTagPass;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  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. /**
  27. * @param string $handlerService Service name of the fragment handler in the container
  28. * @param string $rendererTag Tag name used for fragments
  29. */
  30. public function __construct($handlerService = 'fragment.handler', $rendererTag = 'kernel.fragment_renderer')
  31. {
  32. $this->handlerService = $handlerService;
  33. $this->rendererTag = $rendererTag;
  34. }
  35. public function process(ContainerBuilder $container)
  36. {
  37. if (!$container->hasDefinition($this->handlerService)) {
  38. return;
  39. }
  40. $definition = $container->getDefinition($this->handlerService);
  41. $renderers = array();
  42. foreach ($container->findTaggedServiceIds($this->rendererTag, true) as $id => $tags) {
  43. $def = $container->getDefinition($id);
  44. $class = $container->getParameterBag()->resolveValue($def->getClass());
  45. if (!$r = $container->getReflectionClass($class)) {
  46. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  47. }
  48. if (!$r->isSubclassOf(FragmentRendererInterface::class)) {
  49. throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, FragmentRendererInterface::class));
  50. }
  51. foreach ($tags as $tag) {
  52. $renderers[$tag['alias']] = new Reference($id);
  53. }
  54. }
  55. $definition->replaceArgument(0, ServiceLocatorTagPass::register($container, $renderers));
  56. }
  57. }