InlineFragmentRenderer.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\EventDispatcherInterface;
  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\GetResponseForExceptionEvent;
  16. use Symfony\Component\HttpKernel\HttpCache\SubRequestHandler;
  17. use Symfony\Component\HttpKernel\HttpKernelInterface;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  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 = array())
  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 = array();
  51. // The request format and locale might have been overridden by the user
  52. foreach (array('_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 simply ignored
  71. if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
  72. $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
  73. $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
  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($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', array(), $cookies, array(), $server);
  95. if ($request->headers->has('Surrogate-Capability')) {
  96. $subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
  97. }
  98. if ($session = $request->getSession()) {
  99. $subRequest->setSession($session);
  100. }
  101. if ($request->get('_format')) {
  102. $subRequest->attributes->set('_format', $request->get('_format'));
  103. }
  104. if ($request->getDefaultLocale() !== $request->getLocale()) {
  105. $subRequest->setLocale($request->getLocale());
  106. }
  107. return $subRequest;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getName()
  113. {
  114. return 'inline';
  115. }
  116. }