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\Templating\EngineInterface ;
use Symfony\Component\HttpKernel\Controller\ControllerReference ;
2013-01-03 18:22:22 +00:00
use Symfony\Component\HttpKernel\UriSigner ;
2012-12-19 07:54:53 +00:00
/**
2013-01-04 17:38:08 +00:00
* Implements the Hinclude 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 HIncludeFragmentRenderer extends RoutableFragmentRenderer
2012-12-19 07:54:53 +00:00
{
2013-01-23 13:48:08 +00:00
protected $templating ;
2012-12-19 07:54:53 +00:00
private $globalDefaultTemplate ;
2013-01-03 18:22:22 +00:00
private $signer ;
2012-12-19 07:54:53 +00:00
2013-01-04 17:38:08 +00:00
/**
* Constructor .
*
* @ param EngineInterface | \Twig_Environment $templating An EngineInterface or a \Twig_Environment instance
* @ param UriSigner $signer A UriSigner instance
2013-01-04 17:47:22 +00:00
* @ param string $globalDefaultTemplate The global default content ( it can be a template name or the content )
2013-01-04 17:38:08 +00:00
*/
2013-01-06 09:59:36 +00:00
public function __construct ( $templating = null , UriSigner $signer = null , $globalDefaultTemplate = null )
2012-12-19 07:54:53 +00:00
{
2013-01-06 09:59:36 +00:00
if ( null !== $templating && ! $templating instanceof EngineInterface && ! $templating instanceof \Twig_Environment ) {
2012-12-19 07:54:53 +00:00
throw new \InvalidArgumentException ( 'The hinclude rendering strategy needs an instance of \Twig_Environment or Symfony\Component\Templating\EngineInterface' );
}
$this -> templating = $templating ;
$this -> globalDefaultTemplate = $globalDefaultTemplate ;
2013-01-03 18:22:22 +00:00
$this -> signer = $signer ;
2012-12-19 07:54:53 +00:00
}
2013-01-04 17:38:08 +00:00
/**
* { @ inheritdoc }
2013-01-04 17:47:22 +00:00
*
* Additional available options :
*
* * default : The default content ( it can be a template name or the content )
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
{
if ( $uri instanceof ControllerReference ) {
2013-01-03 18:22:22 +00:00
if ( null === $this -> signer ) {
throw new \LogicException ( 'You must use a proper URI when using the Hinclude rendering strategy or set a URL signer.' );
}
2013-01-31 09:37:27 +00:00
$uri = $this -> signer -> sign ( $this -> generateFragmentUri ( $uri , $request ));
2012-12-19 07:54:53 +00:00
}
2013-01-04 17:47:22 +00:00
$template = isset ( $options [ 'default' ]) ? $options [ 'default' ] : $this -> globalDefaultTemplate ;
2013-02-06 15:51:32 +00:00
if ( null !== $this -> templating && $template && $this -> templateExists ( $template )) {
2013-01-04 17:47:22 +00:00
$content = $this -> templating -> render ( $template );
} else {
$content = $template ;
2012-12-19 07:54:53 +00:00
}
2013-01-20 09:04:47 +00:00
return new Response ( sprintf ( '<hx:include src="%s">%s</hx:include>' , $uri , $content ));
2012-12-19 07:54:53 +00:00
}
private function templateExists ( $template )
{
if ( $this -> templating instanceof EngineInterface ) {
return $this -> templating -> exists ( $template );
}
$loader = $this -> templating -> getLoader ();
if ( $loader instanceof \Twig_ExistsLoaderInterface ) {
return $loader -> exists ( $template );
}
try {
$loader -> getSource ( $template );
return true ;
} catch ( \Twig_Error_Loader $e ) {
}
return false ;
}
2013-01-04 17:38:08 +00:00
/**
* { @ inheritdoc }
*/
2012-12-19 07:54:53 +00:00
public function getName ()
{
return 'hinclude' ;
}
}