FragmentHandler.php 3.6 KB

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