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\EventDispatcher\EventDispatcherInterface;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpKernel\Controller\ControllerReference;
18 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
19 use Symfony\Component\HttpKernel\HttpCache\SubRequestHandler;
20 use Symfony\Component\HttpKernel\HttpKernelInterface;
21 use Symfony\Component\HttpKernel\KernelEvents;
24 * Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
26 * @author Fabien Potencier <fabien@symfony.com>
28 class InlineFragmentRenderer extends RoutableFragmentRenderer
33 public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
35 $this->kernel = $kernel;
36 $this->dispatcher = $dispatcher;
42 * Additional available options:
44 * * alt: an alternative URI to render in case of an error
46 public function render($uri, Request $request, array $options = array())
49 if ($uri instanceof ControllerReference) {
52 // Remove attributes from the generated URI because if not, the Symfony
53 // routing system will use them to populate the Request attributes. We don't
54 // want that as we want to preserve objects (so we manually set Request attributes
56 $attributes = $reference->attributes;
57 $reference->attributes = array();
59 // The request format and locale might have been overridden by the user
60 foreach (array('_format', '_locale') as $key) {
61 if (isset($attributes[$key])) {
62 $reference->attributes[$key] = $attributes[$key];
66 $uri = $this->generateFragmentUri($uri, $request, false, false);
68 $reference->attributes = array_merge($attributes, $reference->attributes);
71 $subRequest = $this->createSubRequest($uri, $request);
73 // override Request attributes as they can be objects (which are not supported by the generated URI)
74 if (null !== $reference) {
75 $subRequest->attributes->add($reference->attributes);
78 $level = ob_get_level();
80 return SubRequestHandler::handle($this->kernel, $subRequest, HttpKernelInterface::SUB_REQUEST, false);
81 } catch (\Exception $e) {
82 // we dispatch the exception event to trigger the logging
83 // the response that comes back is simply ignored
84 if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
85 $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
87 $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
90 // let's clean up the output buffers that were created by the sub-request
91 Response::closeOutputBuffers($level, false);
93 if (isset($options['alt'])) {
94 $alt = $options['alt'];
95 unset($options['alt']);
97 return $this->render($alt, $request, $options);
100 if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
104 return new Response();
108 protected function createSubRequest($uri, Request $request)
110 $cookies = $request->cookies->all();
111 $server = $request->server->all();
113 unset($server['HTTP_IF_MODIFIED_SINCE']);
114 unset($server['HTTP_IF_NONE_MATCH']);
116 $subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server);
117 if ($request->headers->has('Surrogate-Capability')) {
118 $subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
121 if ($session = $request->getSession()) {
122 $subRequest->setSession($session);
125 if ($request->get('_format')) {
126 $subRequest->attributes->set('_format', $request->get('_format'));
128 if ($request->getDefaultLocale() !== $request->getLocale()) {
129 $subRequest->setLocale($request->getLocale());
138 public function getName()