FragmentHandler.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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\RequestStack;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\StreamedResponse;
  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. * RequestStack will become required in 3.0.
  39. *
  40. * @param RequestStack $requestStack The Request stack that controls the lifecycle of requests
  41. * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
  42. * @param bool $debug Whether the debug mode is enabled or not
  43. */
  44. public function __construct($requestStack = null, $renderers = array(), $debug = false)
  45. {
  46. if (\is_array($requestStack)) {
  47. $tmp = $debug;
  48. $debug = \func_num_args() < 2 ? false : $renderers;
  49. $renderers = $requestStack;
  50. $requestStack = \func_num_args() < 3 ? null : $tmp;
  51. @trigger_error('The '.__METHOD__.' method now requires a RequestStack to be given as first argument as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
  52. } elseif (!$requestStack instanceof RequestStack) {
  53. @trigger_error('The '.__METHOD__.' method now requires a RequestStack instance as '.__CLASS__.'::setRequest method will not be supported anymore in 3.0.', E_USER_DEPRECATED);
  54. }
  55. if (null !== $requestStack && !$requestStack instanceof RequestStack) {
  56. throw new \InvalidArgumentException('RequestStack instance expected.');
  57. }
  58. if (!\is_array($renderers)) {
  59. throw new \InvalidArgumentException('Renderers must be an array.');
  60. }
  61. $this->requestStack = $requestStack;
  62. foreach ($renderers as $renderer) {
  63. $this->addRenderer($renderer);
  64. }
  65. $this->debug = $debug;
  66. }
  67. /**
  68. * Adds a renderer.
  69. */
  70. public function addRenderer(FragmentRendererInterface $renderer)
  71. {
  72. $this->renderers[$renderer->getName()] = $renderer;
  73. }
  74. /**
  75. * Sets the current Request.
  76. *
  77. * This method was used to synchronize the Request, but as the HttpKernel
  78. * is doing that automatically now, you should never call it directly.
  79. * It is kept public for BC with the 2.3 version.
  80. *
  81. * @param Request|null $request A Request instance
  82. *
  83. * @deprecated since version 2.4, to be removed in 3.0.
  84. */
  85. public function setRequest(Request $request = null)
  86. {
  87. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.4 and will be removed in 3.0.', E_USER_DEPRECATED);
  88. $this->request = $request;
  89. }
  90. /**
  91. * Renders a URI and returns the Response content.
  92. *
  93. * Available options:
  94. *
  95. * * ignore_errors: true to return an empty string in case of an error
  96. *
  97. * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance
  98. * @param string $renderer The renderer name
  99. * @param array $options An array of options
  100. *
  101. * @return string|null The Response content or null when the Response is streamed
  102. *
  103. * @throws \InvalidArgumentException when the renderer does not exist
  104. * @throws \LogicException when no master request is being handled
  105. */
  106. public function render($uri, $renderer = 'inline', array $options = array())
  107. {
  108. if (!isset($options['ignore_errors'])) {
  109. $options['ignore_errors'] = !$this->debug;
  110. }
  111. if (!isset($this->renderers[$renderer])) {
  112. throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
  113. }
  114. if (!$request = $this->getRequest()) {
  115. throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
  116. }
  117. return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
  118. }
  119. /**
  120. * Delivers the Response as a string.
  121. *
  122. * When the Response is a StreamedResponse, the content is streamed immediately
  123. * instead of being returned.
  124. *
  125. * @return string|null The Response content or null when the Response is streamed
  126. *
  127. * @throws \RuntimeException when the Response is not successful
  128. */
  129. protected function deliver(Response $response)
  130. {
  131. if (!$response->isSuccessful()) {
  132. throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $this->getRequest()->getUri(), $response->getStatusCode()));
  133. }
  134. if (!$response instanceof StreamedResponse) {
  135. return $response->getContent();
  136. }
  137. $response->sendContent();
  138. }
  139. private function getRequest()
  140. {
  141. return $this->requestStack ? $this->requestStack->getCurrentRequest() : $this->request;
  142. }
  143. }