Drupal investigation

FragmentHandler.php 6.0KB

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