InlineFragmentRenderer.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\HttpKernelInterface;
  14. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. /**
  19. * Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class InlineFragmentRenderer extends RoutableFragmentRenderer
  24. {
  25. private $kernel;
  26. private $dispatcher;
  27. /**
  28. * Constructor.
  29. *
  30. * @param HttpKernelInterface $kernel A HttpKernelInterface instance
  31. */
  32. public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
  33. {
  34. $this->kernel = $kernel;
  35. $this->dispatcher = $dispatcher;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. *
  40. * Additional available options:
  41. *
  42. * * alt: an alternative URI to render in case of an error
  43. */
  44. public function render($uri, Request $request, array $options = array())
  45. {
  46. $reference = null;
  47. if ($uri instanceof ControllerReference) {
  48. $reference = $uri;
  49. // Remove attributes from the generated URI because if not, the Symfony
  50. // routing system will use them to populate the Request attributes. We don't
  51. // want that as we want to preserve objects (so we manually set Request attributes
  52. // below instead)
  53. $attributes = $reference->attributes;
  54. $reference->attributes = array();
  55. // The request format and locale might have been overriden by the user
  56. foreach (array('_format', '_locale') as $key) {
  57. if (isset($attributes[$key])) {
  58. $reference->attributes[$key] = $attributes[$key];
  59. }
  60. }
  61. $uri = $this->generateFragmentUri($uri, $request);
  62. $reference->attributes = array_merge($attributes, $reference->attributes);
  63. }
  64. $subRequest = $this->createSubRequest($uri, $request);
  65. // override Request attributes as they can be objects (which are not supported by the generated URI)
  66. if (null !== $reference) {
  67. $subRequest->attributes->add($reference->attributes);
  68. }
  69. $level = ob_get_level();
  70. try {
  71. return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
  72. } catch (\Exception $e) {
  73. // we dispatch the exception event to trigger the logging
  74. // the response that comes back is simply ignored
  75. if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
  76. $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
  77. $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
  78. }
  79. // let's clean up the output buffers that were created by the sub-request
  80. while (ob_get_level() > $level) {
  81. ob_get_clean();
  82. }
  83. if (isset($options['alt'])) {
  84. $alt = $options['alt'];
  85. unset($options['alt']);
  86. return $this->render($alt, $request, $options);
  87. }
  88. if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
  89. throw $e;
  90. }
  91. return new Response();
  92. }
  93. }
  94. protected function createSubRequest($uri, Request $request)
  95. {
  96. $cookies = $request->cookies->all();
  97. $server = $request->server->all();
  98. // Override the arguments to emulate a sub-request.
  99. // Sub-request object will point to localhost as client ip and real client ip
  100. // will be included into trusted header for client ip
  101. try {
  102. if ($trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) {
  103. $currentXForwardedFor = $request->headers->get($trustedHeaderName, '');
  104. $server['HTTP_'.$trustedHeaderName] = ($currentXForwardedFor ? $currentXForwardedFor.', ' : '').$request->getClientIp();
  105. }
  106. } catch (\InvalidArgumentException $e) {
  107. // Do nothing
  108. }
  109. $server['REMOTE_ADDR'] = '127.0.0.1';
  110. $subRequest = $request::create($uri, 'get', array(), $cookies, array(), $server);
  111. if ($request->headers->has('Surrogate-Capability')) {
  112. $subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
  113. }
  114. if ($session = $request->getSession()) {
  115. $subRequest->setSession($session);
  116. }
  117. return $subRequest;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function getName()
  123. {
  124. return 'inline';
  125. }
  126. }