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\Controller\ControllerReference;
|
|
|
|
use Symfony\Component\HttpKernel\HttpCache\Esi;
|
|
|
|
|
|
|
|
/**
|
2013-01-04 17:38:08 +00:00
|
|
|
* Implements the ESI rendering strategy.
|
2012-12-19 07:54:53 +00:00
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
2013-01-31 09:37:27 +00:00
|
|
|
class EsiFragmentRenderer extends RoutableFragmentRenderer
|
2012-12-19 07:54:53 +00:00
|
|
|
{
|
|
|
|
private $esi;
|
2013-01-31 09:37:27 +00:00
|
|
|
private $inlineStrategy;
|
2012-12-19 07:54:53 +00:00
|
|
|
|
2013-01-04 17:38:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* The "fallback" strategy when ESI is not available should always be an
|
2013-01-31 09:37:27 +00:00
|
|
|
* instance of InlineFragmentRenderer.
|
2013-01-04 17:38:08 +00:00
|
|
|
*
|
2014-04-08 23:45:11 +01:00
|
|
|
* @param Esi $esi An Esi instance
|
|
|
|
* @param FragmentRendererInterface $inlineStrategy The inline strategy to use when ESI is not supported
|
2013-01-04 17:38:08 +00:00
|
|
|
*/
|
2013-08-13 10:17:13 +01:00
|
|
|
public function __construct(Esi $esi = null, InlineFragmentRenderer $inlineStrategy)
|
2012-12-19 07:54:53 +00:00
|
|
|
{
|
|
|
|
$this->esi = $esi;
|
2013-01-31 09:37:27 +00:00
|
|
|
$this->inlineStrategy = $inlineStrategy;
|
2012-12-19 07:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-04 17:38:08 +00:00
|
|
|
* {@inheritdoc}
|
2012-12-19 07:54:53 +00:00
|
|
|
*
|
2013-01-04 17:38:08 +00:00
|
|
|
* Note that if the current Request has no ESI capability, this method
|
2013-01-31 09:37:27 +00:00
|
|
|
* falls back to use the inline rendering strategy.
|
2012-12-19 07:54:53 +00:00
|
|
|
*
|
2013-01-04 17:38:08 +00:00
|
|
|
* Additional available options:
|
2012-12-19 07:54:53 +00:00
|
|
|
*
|
2013-01-04 17:38:08 +00:00
|
|
|
* * alt: an alternative URI to render in case of an error
|
2012-12-19 07:54:53 +00:00
|
|
|
* * comment: a comment to add when returning an esi:include tag
|
2013-01-04 17:38:08 +00:00
|
|
|
*
|
|
|
|
* @see Symfony\Component\HttpKernel\HttpCache\ESI
|
2012-12-19 07:54:53 +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-08-13 10:17:13 +01:00
|
|
|
if (!$this->esi || !$this->esi->hasSurrogateEsiCapability($request)) {
|
2013-01-31 09:37:27 +00:00
|
|
|
return $this->inlineStrategy->render($uri, $request, $options);
|
2012-12-19 07:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($uri instanceof ControllerReference) {
|
2013-01-31 09:37:27 +00:00
|
|
|
$uri = $this->generateFragmentUri($uri, $request);
|
2012-12-19 07:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$alt = isset($options['alt']) ? $options['alt'] : null;
|
|
|
|
if ($alt instanceof ControllerReference) {
|
2013-01-31 09:37:27 +00:00
|
|
|
$alt = $this->generateFragmentUri($alt, $request);
|
2012-12-19 07:54:53 +00:00
|
|
|
}
|
|
|
|
|
2013-01-20 09:04:47 +00:00
|
|
|
$tag = $this->esi->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : '');
|
|
|
|
|
|
|
|
return new Response($tag);
|
2012-12-19 07:54:53 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 17:38:08 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-12-19 07:54:53 +00:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'esi';
|
|
|
|
}
|
|
|
|
}
|