No Description

InlineFragmentRenderer.php 5.0KB

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