菜谱项目

InlineFragmentRenderer.php 5.5KB

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