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
35 * @param HttpKernelInterface $kernel A HttpKernelInterface instance
36 * @param EventDispatcherInterface $dispatcher A EventDispatcherInterface instance
38 public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
40 $this->kernel = $kernel;
41 $this->dispatcher = $dispatcher;
47 * Additional available options:
49 * * alt: an alternative URI to render in case of an error
51 public function render($uri, Request $request, array $options = array())
54 if ($uri instanceof ControllerReference) {
57 // Remove attributes from the generated URI because if not, the Symfony
58 // routing system will use them to populate the Request attributes. We don't
59 // want that as we want to preserve objects (so we manually set Request attributes
61 $attributes = $reference->attributes;
62 $reference->attributes = array();
64 // The request format and locale might have been overridden by the user
65 foreach (array('_format', '_locale') as $key) {
66 if (isset($attributes[$key])) {
67 $reference->attributes[$key] = $attributes[$key];
71 $uri = $this->generateFragmentUri($uri, $request, false, false);
73 $reference->attributes = array_merge($attributes, $reference->attributes);
76 $subRequest = $this->createSubRequest($uri, $request);
78 // override Request attributes as they can be objects (which are not supported by the generated URI)
79 if (null !== $reference) {
80 $subRequest->attributes->add($reference->attributes);
83 $level = ob_get_level();
85 return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
86 } catch (\Exception $e) {
87 // we dispatch the exception event to trigger the logging
88 // the response that comes back is simply ignored
89 if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
90 $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
92 $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
95 // let's clean up the output buffers that were created by the sub-request
96 Response::closeOutputBuffers($level, false);
98 if (isset($options['alt'])) {
99 $alt = $options['alt'];
100 unset($options['alt']);
102 return $this->render($alt, $request, $options);
105 if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
109 return new Response();
113 protected function createSubRequest($uri, Request $request)
115 $cookies = $request->cookies->all();
116 $server = $request->server->all();
118 // Override the arguments to emulate a sub-request.
119 // Sub-request object will point to localhost as client ip and real client ip
120 // will be included into trusted header for client ip
122 if ($trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) {
123 $currentXForwardedFor = $request->headers->get($trustedHeaderName, '');
125 $server['HTTP_'.$trustedHeaderName] = ($currentXForwardedFor ? $currentXForwardedFor.', ' : '').$request->getClientIp();
127 } catch (\InvalidArgumentException $e) {
131 $server['REMOTE_ADDR'] = '127.0.0.1';
132 unset($server['HTTP_IF_MODIFIED_SINCE']);
133 unset($server['HTTP_IF_NONE_MATCH']);
135 $subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server);
136 if ($request->headers->has('Surrogate-Capability')) {
137 $subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
140 if ($session = $request->getSession()) {
141 $subRequest->setSession($session);
150 public function getName()