2012-12-19 07:54:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2013-01-31 09:37:27 +00:00
|
|
|
namespace Symfony\Component\HttpKernel\Fragment;
|
2012-12-19 07:54:53 +00:00
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2013-01-20 09:04:47 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2012-12-19 07:54:53 +00:00
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerReference;
|
2013-04-22 14:39:53 +01:00
|
|
|
use Symfony\Component\HttpKernel\KernelEvents;
|
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2012-12-19 07:54:53 +00:00
|
|
|
|
|
|
|
/**
|
2013-01-31 09:37:27 +00:00
|
|
|
* Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.
|
2012-12-19 07:54:53 +00:00
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
2013-01-31 09:37:27 +00:00
|
|
|
class InlineFragmentRenderer extends RoutableFragmentRenderer
|
2012-12-19 07:54:53 +00:00
|
|
|
{
|
|
|
|
private $kernel;
|
2013-04-22 14:39:53 +01:00
|
|
|
private $dispatcher;
|
2012-12-19 07:54:53 +00:00
|
|
|
|
2013-04-22 14:39:53 +01:00
|
|
|
public function __construct(HttpKernelInterface $kernel, EventDispatcherInterface $dispatcher = null)
|
2012-12-19 07:54:53 +00:00
|
|
|
{
|
|
|
|
$this->kernel = $kernel;
|
2013-04-22 14:39:53 +01:00
|
|
|
$this->dispatcher = $dispatcher;
|
2012-12-19 07:54:53 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 17:38:08 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2013-01-10 15:08:44 +00:00
|
|
|
*
|
|
|
|
* Additional available options:
|
|
|
|
*
|
|
|
|
* * alt: an alternative URI to render in case of an error
|
2013-01-04 17:38:08 +00:00
|
|
|
*/
|
2013-01-22 08:06:13 +00:00
|
|
|
public function render($uri, Request $request, array $options = array())
|
2012-12-19 07:54:53 +00:00
|
|
|
{
|
2013-02-01 16:08:58 +00:00
|
|
|
$reference = null;
|
2012-12-19 07:54:53 +00:00
|
|
|
if ($uri instanceof ControllerReference) {
|
2013-02-01 16:08:58 +00:00
|
|
|
$reference = $uri;
|
2013-07-08 09:25:51 +01:00
|
|
|
|
2013-07-21 13:35:20 +01:00
|
|
|
// Remove attributes from the generated URI because if not, the Symfony
|
2013-07-08 09:25:51 +01:00
|
|
|
// routing system will use them to populate the Request attributes. We don't
|
|
|
|
// want that as we want to preserve objects (so we manually set Request attributes
|
|
|
|
// below instead)
|
|
|
|
$attributes = $reference->attributes;
|
|
|
|
$reference->attributes = array();
|
2013-08-19 11:49:47 +01:00
|
|
|
|
2013-08-24 07:54:37 +01:00
|
|
|
// The request format and locale might have been overridden by the user
|
2013-08-22 07:48:41 +01:00
|
|
|
foreach (array('_format', '_locale') as $key) {
|
|
|
|
if (isset($attributes[$key])) {
|
|
|
|
$reference->attributes[$key] = $attributes[$key];
|
|
|
|
}
|
2013-08-19 11:49:47 +01:00
|
|
|
}
|
|
|
|
|
2013-08-30 14:15:50 +01:00
|
|
|
$uri = $this->generateFragmentUri($uri, $request, false, false);
|
2013-08-22 07:44:20 +01:00
|
|
|
|
2013-07-10 08:16:02 +01:00
|
|
|
$reference->attributes = array_merge($attributes, $reference->attributes);
|
2012-12-19 07:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$subRequest = $this->createSubRequest($uri, $request);
|
|
|
|
|
2013-02-01 16:08:58 +00:00
|
|
|
// override Request attributes as they can be objects (which are not supported by the generated URI)
|
|
|
|
if (null !== $reference) {
|
|
|
|
$subRequest->attributes->add($reference->attributes);
|
|
|
|
}
|
|
|
|
|
2012-12-19 07:54:53 +00:00
|
|
|
$level = ob_get_level();
|
|
|
|
try {
|
2013-01-20 09:04:47 +00:00
|
|
|
return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
|
2012-12-19 07:54:53 +00:00
|
|
|
} catch (\Exception $e) {
|
2013-04-22 14:39:53 +01:00
|
|
|
// we dispatch the exception event to trigger the logging
|
|
|
|
// the response that comes back is simply ignored
|
2014-04-27 10:44:26 +01:00
|
|
|
if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
|
|
|
|
$event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
|
2013-04-22 14:39:53 +01:00
|
|
|
|
2014-04-27 10:44:26 +01:00
|
|
|
$this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
|
2013-04-22 14:39:53 +01:00
|
|
|
}
|
|
|
|
|
2012-12-19 07:54:53 +00:00
|
|
|
// let's clean up the output buffers that were created by the sub-request
|
2014-04-17 08:51:05 +01:00
|
|
|
Response::closeOutputBuffers($level, false);
|
2012-12-19 07:54:53 +00:00
|
|
|
|
|
|
|
if (isset($options['alt'])) {
|
|
|
|
$alt = $options['alt'];
|
|
|
|
unset($options['alt']);
|
|
|
|
|
|
|
|
return $this->render($alt, $request, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
2013-01-20 09:04:47 +00:00
|
|
|
return new Response();
|
2012-12-19 07:54:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 08:06:13 +00:00
|
|
|
protected function createSubRequest($uri, Request $request)
|
2012-12-19 07:54:53 +00:00
|
|
|
{
|
2013-01-22 08:06:13 +00:00
|
|
|
$cookies = $request->cookies->all();
|
|
|
|
$server = $request->server->all();
|
|
|
|
|
2017-05-22 23:20:30 +01:00
|
|
|
if (Request::HEADER_X_FORWARDED_FOR & Request::getTrustedHeaderSet()) {
|
|
|
|
$currentXForwardedFor = $request->headers->get('X_FORWARDED_FOR', '');
|
2013-04-04 06:07:34 +01:00
|
|
|
|
2017-05-22 23:20:30 +01:00
|
|
|
$server['HTTP_X_FORWARDED_FOR'] = ($currentXForwardedFor ? $currentXForwardedFor.', ' : '').$request->getClientIp();
|
2013-04-04 06:07:34 +01:00
|
|
|
}
|
|
|
|
|
2013-01-22 08:06:13 +00:00
|
|
|
$server['REMOTE_ADDR'] = '127.0.0.1';
|
2016-07-05 09:15:40 +01:00
|
|
|
unset($server['HTTP_IF_MODIFIED_SINCE']);
|
|
|
|
unset($server['HTTP_IF_NONE_MATCH']);
|
2012-12-19 07:54:53 +00:00
|
|
|
|
2013-09-07 22:03:36 +01:00
|
|
|
$subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server);
|
2013-03-03 21:47:32 +00:00
|
|
|
if ($request->headers->has('Surrogate-Capability')) {
|
|
|
|
$subRequest->headers->set('Surrogate-Capability', $request->headers->get('Surrogate-Capability'));
|
|
|
|
}
|
2013-04-04 06:07:34 +01:00
|
|
|
|
2013-01-22 08:06:13 +00:00
|
|
|
if ($session = $request->getSession()) {
|
2012-12-19 07:54:53 +00:00
|
|
|
$subRequest->setSession($session);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $subRequest;
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:38:08 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-19 07:54:53 +00:00
|
|
|
public function getName()
|
|
|
|
{
|
2013-01-31 09:37:27 +00:00
|
|
|
return 'inline';
|
2012-12-19 07:54:53 +00:00
|
|
|
}
|
|
|
|
}
|