Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\Fragment; Chris@0: Chris@0: use Symfony\Component\HttpFoundation\Request; Chris@0: use Symfony\Component\HttpFoundation\Response; Chris@0: use Symfony\Component\HttpKernel\HttpKernelInterface; Chris@0: use Symfony\Component\HttpKernel\Controller\ControllerReference; Chris@0: use Symfony\Component\HttpKernel\KernelEvents; Chris@0: use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; Chris@0: use Symfony\Component\EventDispatcher\EventDispatcherInterface; Chris@0: Chris@0: /** Chris@0: * Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class InlineFragmentRenderer extends RoutableFragmentRenderer Chris@0: { Chris@0: private $kernel; Chris@0: private $dispatcher; Chris@0: Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * @param HttpKernelInterface $kernel A HttpKernelInterface instance Chris@0: * @param EventDispatcherInterface $dispatcher A EventDispatcherInterface instance Chris@0: */ Chris@0: public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null) Chris@0: { Chris@0: $this->kernel = $kernel; Chris@0: $this->dispatcher = $dispatcher; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * Additional available options: Chris@0: * Chris@0: * * alt: an alternative URI to render in case of an error Chris@0: */ Chris@0: public function render($uri, Request $request, array $options = array()) Chris@0: { Chris@0: $reference = null; Chris@0: if ($uri instanceof ControllerReference) { Chris@0: $reference = $uri; Chris@0: Chris@0: // Remove attributes from the generated URI because if not, the Symfony Chris@0: // routing system will use them to populate the Request attributes. We don't Chris@0: // want that as we want to preserve objects (so we manually set Request attributes Chris@0: // below instead) Chris@0: $attributes = $reference->attributes; Chris@0: $reference->attributes = array(); Chris@0: Chris@0: // The request format and locale might have been overridden by the user Chris@0: foreach (array('_format', '_locale') as $key) { Chris@0: if (isset($attributes[$key])) { Chris@0: $reference->attributes[$key] = $attributes[$key]; Chris@0: } Chris@0: } Chris@0: Chris@0: $uri = $this->generateFragmentUri($uri, $request, false, false); Chris@0: Chris@0: $reference->attributes = array_merge($attributes, $reference->attributes); Chris@0: } Chris@0: Chris@0: $subRequest = $this->createSubRequest($uri, $request); Chris@0: Chris@0: // override Request attributes as they can be objects (which are not supported by the generated URI) Chris@0: if (null !== $reference) { Chris@0: $subRequest->attributes->add($reference->attributes); Chris@0: } Chris@0: Chris@0: $level = ob_get_level(); Chris@0: try { Chris@0: return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false); Chris@0: } catch (\Exception $e) { Chris@0: // we dispatch the exception event to trigger the logging Chris@0: // the response that comes back is simply ignored Chris@0: if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) { Chris@0: $event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e); Chris@0: Chris@0: $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event); Chris@0: } Chris@0: Chris@0: // let's clean up the output buffers that were created by the sub-request Chris@0: Response::closeOutputBuffers($level, false); Chris@0: Chris@0: if (isset($options['alt'])) { Chris@0: $alt = $options['alt']; Chris@0: unset($options['alt']); Chris@0: Chris@0: return $this->render($alt, $request, $options); Chris@0: } Chris@0: Chris@0: if (!isset($options['ignore_errors']) || !$options['ignore_errors']) { Chris@0: throw $e; Chris@0: } Chris@0: Chris@0: return new Response(); Chris@0: } Chris@0: } Chris@0: Chris@0: protected function createSubRequest($uri, Request $request) Chris@0: { Chris@0: $cookies = $request->cookies->all(); Chris@0: $server = $request->server->all(); Chris@0: Chris@0: // Override the arguments to emulate a sub-request. Chris@0: // Sub-request object will point to localhost as client ip and real client ip Chris@0: // will be included into trusted header for client ip Chris@0: try { Chris@0: if ($trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) { Chris@0: $currentXForwardedFor = $request->headers->get($trustedHeaderName, ''); Chris@0: Chris@0: $server['HTTP_'.$trustedHeaderName] = ($currentXForwardedFor ? $currentXForwardedFor.', ' : '').$request->getClientIp(); Chris@0: } Chris@0: } catch (\InvalidArgumentException $e) { Chris@0: // Do nothing Chris@0: } Chris@0: Chris@0: $server['REMOTE_ADDR'] = '127.0.0.1'; Chris@0: unset($server['HTTP_IF_MODIFIED_SINCE']); Chris@0: unset($server['HTTP_IF_NONE_MATCH']); Chris@0: Chris@0: $subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server); Chris@0: if ($request->headers->has('Surrogate-Capability')) { Chris@0: $subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability')); Chris@0: } Chris@0: Chris@0: if ($session = $request->getSession()) { Chris@0: $subRequest->setSession($session); Chris@0: } Chris@0: Chris@0: return $subRequest; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: return 'inline'; Chris@0: } Chris@0: }