Просмотр файла vendor/symfony/http-kernel/Fragment/InlineFragmentRenderer.php

Размер файла: 4.96Kb
  1. <?php
  2.  
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11.  
  12. namespace Symfony\Component\HttpKernel\Fragment;
  13.  
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  17. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  18. use Symfony\Component\HttpKernel\HttpCache\SubRequestHandler;
  19. use Symfony\Component\HttpKernel\HttpKernelInterface;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  22.  
  23. /**
  24. * Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
  25. *
  26. * @author Fabien Potencier <fabien@symfony.com>
  27. */
  28. class InlineFragmentRenderer extends RoutableFragmentRenderer
  29. {
  30. private $kernel;
  31. private $dispatcher;
  32.  
  33. public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
  34. {
  35. $this->kernel = $kernel;
  36. $this->dispatcher = $dispatcher;
  37. }
  38.  
  39. /**
  40. * {@inheritdoc}
  41. *
  42. * Additional available options:
  43. *
  44. * * alt: an alternative URI to render in case of an error
  45. */
  46. public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
  47. {
  48. $reference = null;
  49. if ($uri instanceof ControllerReference) {
  50. $reference = $uri;
  51.  
  52. // Remove attributes from the generated URI because if not, the Symfony
  53. // routing system will use them to populate the Request attributes. We don't
  54. // want that as we want to preserve objects (so we manually set Request attributes
  55. // below instead)
  56. $attributes = $reference->attributes;
  57. $reference->attributes = [];
  58.  
  59. // The request format and locale might have been overridden by the user
  60. foreach (['_format', '_locale'] as $key) {
  61. if (isset($attributes[$key])) {
  62. $reference->attributes[$key] = $attributes[$key];
  63. }
  64. }
  65.  
  66. $uri = $this->generateFragmentUri($uri, $request, false, false);
  67.  
  68. $reference->attributes = array_merge($attributes, $reference->attributes);
  69. }
  70.  
  71. $subRequest = $this->createSubRequest($uri, $request);
  72.  
  73. // override Request attributes as they can be objects (which are not supported by the generated URI)
  74. if (null !== $reference) {
  75. $subRequest->attributes->add($reference->attributes);
  76. }
  77.  
  78. $level = ob_get_level();
  79. try {
  80. return SubRequestHandler::handle($this->kernel, $subRequest, HttpKernelInterface::SUB_REQUEST, false);
  81. } catch (\Exception $e) {
  82. // we dispatch the exception event to trigger the logging
  83. // the response that comes back is ignored
  84. if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
  85. $event = new ExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
  86.  
  87. $this->dispatcher->dispatch($event, KernelEvents::EXCEPTION);
  88. }
  89.  
  90. // let's clean up the output buffers that were created by the sub-request
  91. Response::closeOutputBuffers($level, false);
  92.  
  93. if (isset($options['alt'])) {
  94. $alt = $options['alt'];
  95. unset($options['alt']);
  96.  
  97. return $this->render($alt, $request, $options);
  98. }
  99.  
  100. if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
  101. throw $e;
  102. }
  103.  
  104. return new Response();
  105. }
  106. }
  107.  
  108. protected function createSubRequest(string $uri, Request $request)
  109. {
  110. $cookies = $request->cookies->all();
  111. $server = $request->server->all();
  112.  
  113. unset($server['HTTP_IF_MODIFIED_SINCE']);
  114. unset($server['HTTP_IF_NONE_MATCH']);
  115.  
  116. $subRequest = Request::create($uri, 'get', [], $cookies, [], $server);
  117. if ($request->headers->has('Surrogate-Capability')) {
  118. $subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
  119. }
  120.  
  121. static $setSession;
  122.  
  123. if (null === $setSession) {
  124. $setSession = \Closure::bind(static function ($subRequest, $request) { $subRequest->session = $request->session; }, null, Request::class);
  125. }
  126. $setSession($subRequest, $request);
  127.  
  128. if ($request->get('_format')) {
  129. $subRequest->attributes->set('_format', $request->get('_format'));
  130. }
  131. if ($request->getDefaultLocale() !== $request->getLocale()) {
  132. $subRequest->setLocale($request->getLocale());
  133. }
  134.  
  135. return $subRequest;
  136. }
  137.  
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function getName(): string
  142. {
  143. return 'inline';
  144. }
  145. }