InlineFragmentRenderer.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\HttpKernel\Controller\ControllerReference;
  14. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  15. use Symfony\Component\HttpKernel\HttpCache\SubRequestHandler;
  16. use Symfony\Component\HttpKernel\HttpKernelInterface;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. /**
  20. * Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class InlineFragmentRenderer extends RoutableFragmentRenderer
  25. {
  26. public function __construct(
  27. private HttpKernelInterface $kernel,
  28. private ?EventDispatcherInterface $dispatcher = null,
  29. ) {
  30. }
  31. /**
  32. * Additional available options:
  33. *
  34. * * alt: an alternative URI to render in case of an error
  35. */
  36. public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
  37. {
  38. $reference = null;
  39. if ($uri instanceof ControllerReference) {
  40. $reference = $uri;
  41. // Remove attributes from the generated URI because if not, the Symfony
  42. // routing system will use them to populate the Request attributes. We don't
  43. // want that as we want to preserve objects (so we manually set Request attributes
  44. // below instead)
  45. $attributes = $reference->attributes;
  46. $reference->attributes = [];
  47. // The request format and locale might have been overridden by the user
  48. foreach (['_format', '_locale'] as $key) {
  49. if (isset($attributes[$key])) {
  50. $reference->attributes[$key] = $attributes[$key];
  51. }
  52. }
  53. $uri = $this->generateFragmentUri($uri, $request, false, false);
  54. $reference->attributes = array_merge($attributes, $reference->attributes);
  55. }
  56. $subRequest = $this->createSubRequest($uri, $request);
  57. // override Request attributes as they can be objects (which are not supported by the generated URI)
  58. if (null !== $reference) {
  59. $subRequest->attributes->add($reference->attributes);
  60. }
  61. $level = ob_get_level();
  62. try {
  63. return SubRequestHandler::handle($this->kernel, $subRequest, HttpKernelInterface::SUB_REQUEST, false);
  64. } catch (\Exception $e) {
  65. // we dispatch the exception event to trigger the logging
  66. // the response that comes back is ignored
  67. if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
  68. $event = new ExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
  69. $this->dispatcher->dispatch($event, KernelEvents::EXCEPTION);
  70. }
  71. // let's clean up the output buffers that were created by the sub-request
  72. Response::closeOutputBuffers($level, false);
  73. if (isset($options['alt'])) {
  74. $alt = $options['alt'];
  75. unset($options['alt']);
  76. return $this->render($alt, $request, $options);
  77. }
  78. if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
  79. throw $e;
  80. }
  81. return new Response();
  82. }
  83. }
  84. protected function createSubRequest(string $uri, Request $request): Request
  85. {
  86. $cookies = $request->cookies->all();
  87. $server = $request->server->all();
  88. unset($server['HTTP_IF_MODIFIED_SINCE']);
  89. unset($server['HTTP_IF_NONE_MATCH']);
  90. $subRequest = Request::create($uri, 'get', [], $cookies, [], $server);
  91. if ($request->headers->has('Surrogate-Capability')) {
  92. $subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
  93. }
  94. static $setSession;
  95. $setSession ??= \Closure::bind(static function ($subRequest, $request) { $subRequest->session = $request->session; }, null, Request::class);
  96. $setSession($subRequest, $request);
  97. if ($request->get('_format')) {
  98. $subRequest->attributes->set('_format', $request->get('_format'));
  99. }
  100. if ($request->getDefaultLocale() !== $request->getLocale()) {
  101. $subRequest->setLocale($request->getLocale());
  102. }
  103. if ($request->attributes->has('_stateless')) {
  104. $subRequest->attributes->set('_stateless', $request->attributes->get('_stateless'));
  105. }
  106. if ($request->attributes->has('_check_controller_is_allowed')) {
  107. $subRequest->attributes->set('_check_controller_is_allowed', $request->attributes->get('_check_controller_is_allowed'));
  108. }
  109. return $subRequest;
  110. }
  111. public function getName(): string
  112. {
  113. return 'inline';
  114. }
  115. }