No Description

FragmentHandler.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\StreamedResponse;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  16. /**
  17. * Renders a URI that represents a resource fragment.
  18. *
  19. * This class handles the rendering of resource fragments that are included into
  20. * a main resource. The handling of the rendering is managed by specialized renderers.
  21. *
  22. * This listener works in 2 modes:
  23. *
  24. * * 2.3 compatibility mode where you must call setRequest whenever the Request changes.
  25. * * 2.4+ mode where you must pass a RequestStack instance in the constructor.
  26. *
  27. * @author Fabien Potencier <fabien@symfony.com>
  28. *
  29. * @see FragmentRendererInterface
  30. */
  31. class FragmentHandler
  32. {
  33. private $debug;
  34. private $renderers = array();
  35. private $request;
  36. private $requestStack;
  37. /**
  38. * Constructor.
  39. *
  40. * RequestStack will become required in 3.0.
  41. *
  42. * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
  43. * @param bool $debug Whether the debug mode is enabled or not
  44. * @param RequestStack|null $requestStack The Request stack that controls the lifecycle of requests
  45. */
  46. public function __construct(array $renderers = array(), $debug = false, RequestStack $requestStack = null)
  47. {
  48. $this->requestStack = $requestStack;
  49. foreach ($renderers as $renderer) {
  50. $this->addRenderer($renderer);
  51. }
  52. $this->debug = $debug;
  53. }
  54. /**
  55. * Adds a renderer.
  56. *
  57. * @param FragmentRendererInterface $renderer A FragmentRendererInterface instance
  58. */
  59. public function addRenderer(FragmentRendererInterface $renderer)
  60. {
  61. $this->renderers[$renderer->getName()] = $renderer;
  62. }
  63. /**
  64. * Sets the current Request.
  65. *
  66. * This method was used to synchronize the Request, but as the HttpKernel
  67. * is doing that automatically now, you should never call it directly.
  68. * It is kept public for BC with the 2.3 version.
  69. *
  70. * @param Request|null $request A Request instance
  71. *
  72. * @deprecated Deprecated since version 2.4, to be removed in 3.0.
  73. */
  74. public function setRequest(Request $request = null)
  75. {
  76. $this->request = $request;
  77. }
  78. /**
  79. * Renders a URI and returns the Response content.
  80. *
  81. * Available options:
  82. *
  83. * * ignore_errors: true to return an empty string in case of an error
  84. *
  85. * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
  86. * @param string $renderer The renderer name
  87. * @param array $options An array of options
  88. *
  89. * @return string|null The Response content or null when the Response is streamed
  90. *
  91. * @throws \InvalidArgumentException when the renderer does not exist
  92. * @throws \LogicException when no master request is being handled
  93. */
  94. public function render($uri, $renderer = 'inline', array $options = array())
  95. {
  96. if (!isset($options['ignore_errors'])) {
  97. $options['ignore_errors'] = !$this->debug;
  98. }
  99. if (!isset($this->renderers[$renderer])) {
  100. throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
  101. }
  102. if (!$request = $this->getRequest()) {
  103. throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
  104. }
  105. return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
  106. }
  107. /**
  108. * Delivers the Response as a string.
  109. *
  110. * When the Response is a StreamedResponse, the content is streamed immediately
  111. * instead of being returned.
  112. *
  113. * @param Response $response A Response instance
  114. *
  115. * @return string|null The Response content or null when the Response is streamed
  116. *
  117. * @throws \RuntimeException when the Response is not successful
  118. */
  119. protected function deliver(Response $response)
  120. {
  121. if (!$response->isSuccessful()) {
  122. throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->getRequest()->getUri(), $response->getStatusCode()));
  123. }
  124. if (!$response instanceof StreamedResponse) {
  125. return $response->getContent();
  126. }
  127. $response->sendContent();
  128. }
  129. private function getRequest()
  130. {
  131. return $this->requestStack ? $this->requestStack->getCurrentRequest() : $this->request;
  132. }
  133. }