vendor/symfony/http-kernel/Fragment/InlineFragmentRenderer.php line 46

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