FragmentHandler.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Fragment;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\StreamedResponse;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  15. /**
  16. * Renders a URI that represents a resource fragment.
  17. *
  18. * This class handles the rendering of resource fragments that are included into
  19. * a main resource. The handling of the rendering is managed by specialized renderers.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. *
  23. * @see FragmentRendererInterface
  24. */
  25. class FragmentHandler
  26. {
  27. private $debug;
  28. private $renderers = array();
  29. private $requestStack;
  30. /**
  31. * Constructor.
  32. *
  33. * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
  34. * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
  35. * @param bool $debug Whether the debug mode is enabled or not
  36. */
  37. public function __construct(RequestStack $requestStack, array $renderers = array(), $debug = false)
  38. {
  39. $this->requestStack = $requestStack;
  40. foreach ($renderers as $renderer) {
  41. $this->addRenderer($renderer);
  42. }
  43. $this->debug = $debug;
  44. }
  45. /**
  46. * Adds a renderer.
  47. *
  48. * @param FragmentRendererInterface $renderer A FragmentRendererInterface instance
  49. */
  50. public function addRenderer(FragmentRendererInterface $renderer)
  51. {
  52. $this->renderers[$renderer->getName()] = $renderer;
  53. }
  54. /**
  55. * Renders a URI and returns the Response content.
  56. *
  57. * Available options:
  58. *
  59. * * ignore_errors: true to return an empty string in case of an error
  60. *
  61. * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
  62. * @param string $renderer The renderer name
  63. * @param array $options An array of options
  64. *
  65. * @return string|null The Response content or null when the Response is streamed
  66. *
  67. * @throws \InvalidArgumentException when the renderer does not exist
  68. * @throws \LogicException when no master request is being handled
  69. */
  70. public function render($uri, $renderer = 'inline', array $options = array())
  71. {
  72. if (!isset($options['ignore_errors'])) {
  73. $options['ignore_errors'] = !$this->debug;
  74. }
  75. if (!isset($this->renderers[$renderer])) {
  76. throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
  77. }
  78. if (!$request = $this->requestStack->getCurrentRequest()) {
  79. throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
  80. }
  81. return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
  82. }
  83. /**
  84. * Delivers the Response as a string.
  85. *
  86. * When the Response is a StreamedResponse, the content is streamed immediately
  87. * instead of being returned.
  88. *
  89. * @param Response $response A Response instance
  90. *
  91. * @return string|null The Response content or null when the Response is streamed
  92. *
  93. * @throws \RuntimeException when the Response is not successful
  94. */
  95. protected function deliver(Response $response)
  96. {
  97. if (!$response->isSuccessful()) {
  98. throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->requestStack->getCurrentRequest()->getUri(), $response->getStatusCode()));
  99. }
  100. if (!$response instanceof StreamedResponse) {
  101. return $response->getContent();
  102. }
  103. $response->sendContent();
  104. }
  105. }