暫無描述

InlineFragmentRenderer.php 5.3KB

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