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-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-01-04 17:38:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param HttpKernelInterface $kernel A HttpKernelInterface instance
|
|
|
|
*/
|
2012-12-19 07:54:53 +00:00
|
|
|
public function __construct(HttpKernelInterface $kernel)
|
|
|
|
{
|
|
|
|
$this->kernel = $kernel;
|
|
|
|
}
|
|
|
|
|
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-01-31 09:37:27 +00:00
|
|
|
$uri = $this->generateFragmentUri($uri, $request);
|
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) {
|
|
|
|
// let's clean up the output buffers that were created by the sub-request
|
|
|
|
while (ob_get_level() > $level) {
|
|
|
|
ob_get_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// the sub-request is internal
|
|
|
|
$server['REMOTE_ADDR'] = '127.0.0.1';
|
2012-12-19 07:54:53 +00:00
|
|
|
|
2013-03-15 12:43:45 +00:00
|
|
|
$subRequest = $request::create($uri, 'get', array(), $cookies, array(), $server);
|
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
|
|
|
}
|
|
|
|
}
|