FragmentRendererPass.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15. * Adds services tagged kernel.fragment_renderer as HTTP content rendering strategies.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class FragmentRendererPass implements CompilerPassInterface
  20. {
  21. private $handlerService;
  22. private $rendererTag;
  23. /**
  24. * @param string $handlerService Service name of the fragment handler in the container
  25. * @param string $rendererTag Tag name used for fragments
  26. */
  27. public function __construct($handlerService = 'fragment.handler', $rendererTag = 'kernel.fragment_renderer')
  28. {
  29. $this->handlerService = $handlerService;
  30. $this->rendererTag = $rendererTag;
  31. }
  32. public function process(ContainerBuilder $container)
  33. {
  34. if (!$container->hasDefinition($this->handlerService)) {
  35. return;
  36. }
  37. $definition = $container->getDefinition($this->handlerService);
  38. foreach ($container->findTaggedServiceIds($this->rendererTag) as $id => $tags) {
  39. $def = $container->getDefinition($id);
  40. if (!$def->isPublic()) {
  41. throw new \InvalidArgumentException(sprintf('The service "%s" must be public as fragment renderer are lazy-loaded.', $id));
  42. }
  43. if ($def->isAbstract()) {
  44. throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id));
  45. }
  46. $class = $container->getParameterBag()->resolveValue($def->getClass());
  47. $interface = 'Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface';
  48. if (!is_subclass_of($class, $interface)) {
  49. if (!class_exists($class, false)) {
  50. throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  51. }
  52. throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
  53. }
  54. foreach ($tags as $tag) {
  55. if (!isset($tag['alias'])) {
  56. @trigger_error(sprintf('Service "%s" will have to define the "alias" attribute on the "%s" tag as of Symfony 3.0.', $id, $this->rendererTag), E_USER_DEPRECATED);
  57. // register the handler as a non-lazy-loaded one
  58. $definition->addMethodCall('addRenderer', array(new Reference($id)));
  59. } else {
  60. $definition->addMethodCall('addRendererService', array($tag['alias'], $id));
  61. }
  62. }
  63. }
  64. }
  65. }