4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Fragment;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\HttpKernel\HttpKernelInterface;
17 use Symfony\Component\HttpKernel\Controller\ControllerReference;
18 use Symfony\Component\HttpKernel\KernelEvents;
19 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
20 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23 * Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
25 * @author Fabien Potencier <fabien@symfony.com>
27 class InlineFragmentRenderer extends RoutableFragmentRenderer
32 public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
34 $this->kernel = $kernel;
35 $this->dispatcher = $dispatcher;
41 * Additional available options:
43 * * alt: an alternative URI to render in case of an error
45 public function render($uri, Request $request, array $options = array())
48 if ($uri instanceof ControllerReference) {
51 // Remove attributes from the generated URI because if not, the Symfony
52 // routing system will use them to populate the Request attributes. We don't
53 // want that as we want to preserve objects (so we manually set Request attributes
55 $attributes = $reference->attributes;
56 $reference->attributes = array();
58 // The request format and locale might have been overridden by the user
59 foreach (array('_format', '_locale') as $key) {
60 if (isset($attributes[$key])) {
61 $reference->attributes[$key] = $attributes[$key];
65 $uri = $this->generateFragmentUri($uri, $request, false, false);
67 $reference->attributes = array_merge($attributes, $reference->attributes);
70 $subRequest = $this->createSubRequest($uri, $request);
72 // override Request attributes as they can be objects (which are not supported by the generated URI)
73 if (null !== $reference) {
74 $subRequest->attributes->add($reference->attributes);
77 $level = ob_get_level();
79 return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
80 } catch (\Exception $e) {
81 // we dispatch the exception event to trigger the logging
82 // the response that comes back is simply ignored
83 if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
84 $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
86 $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
89 // let's clean up the output buffers that were created by the sub-request
90 Response::closeOutputBuffers($level, false);
92 if (isset($options['alt'])) {
93 $alt = $options['alt'];
94 unset($options['alt']);
96 return $this->render($alt, $request, $options);
99 if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
103 return new Response();
107 protected function createSubRequest($uri, Request $request)
109 $cookies = $request->cookies->all();
110 $server = $request->server->all();
112 // Override the arguments to emulate a sub-request.
113 // Sub-request object will point to localhost as client ip and real client ip
114 // will be included into trusted header for client ip
116 if (Request::HEADER_X_FORWARDED_FOR & Request::getTrustedHeaderSet()) {
117 $currentXForwardedFor = $request->headers->get('X_FORWARDED_FOR', '');
119 $server['HTTP_X_FORWARDED_FOR'] = ($currentXForwardedFor ? $currentXForwardedFor.', ' : '').$request->getClientIp();
120 } elseif (method_exists(Request::class, 'getTrustedHeaderName') && $trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP, false)) {
121 $currentXForwardedFor = $request->headers->get($trustedHeaderName, '');
123 $server['HTTP_'.$trustedHeaderName] = ($currentXForwardedFor ? $currentXForwardedFor.', ' : '').$request->getClientIp();
125 } catch (\InvalidArgumentException $e) {
129 $server['REMOTE_ADDR'] = '127.0.0.1';
130 unset($server['HTTP_IF_MODIFIED_SINCE']);
131 unset($server['HTTP_IF_NONE_MATCH']);
133 $subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server);
134 if ($request->headers->has('Surrogate-Capability')) {
135 $subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
138 if ($session = $request->getSession()) {
139 $subRequest->setSession($session);
148 public function getName()