InlineFragmentRenderer.php 5.3 KB

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