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 .
*/
namespace Symfony\Component\HttpKernel\RenderingStrategy ;
use Symfony\Component\HttpFoundation\Request ;
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
/**
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
2013-01-03 18:22:22 +00:00
class HIncludeRenderingStrategy extends GeneratorAwareRenderingStrategy
2012-12-19 07:54:53 +00:00
{
private $templating ;
private $globalDefaultTemplate ;
2013-01-03 18:22:22 +00:00
private $signer ;
2012-12-19 07:54:53 +00:00
2013-01-03 18:22:22 +00:00
public function __construct ( $templating , UriSigner $signer = null , $globalDefaultTemplate = null )
2012-12-19 07:54:53 +00:00
{
if ( ! $templating instanceof EngineInterface && ! $templating instanceof \Twig_Environment ) {
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
}
public function render ( $uri , Request $request = null , array $options = array ())
{
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.' );
}
$uri = $this -> signer -> sign ( $this -> generateProxyUri ( $uri , $request ));
2012-12-19 07:54:53 +00:00
}
2013-01-03 18:22:22 +00:00
$defaultTemplate = isset ( $options [ 'default' ]) ? $options [ 'default' ] : null ;
2012-12-19 07:54:53 +00:00
$defaultContent = null ;
if ( null !== $defaultTemplate ) {
if ( $this -> templateExists ( $defaultTemplate )) {
$defaultContent = $this -> templating -> render ( $defaultContent );
} else {
$defaultContent = $defaultTemplate ;
}
} elseif ( $this -> globalDefaultTemplate ) {
$defaultContent = $this -> templating -> render ( $this -> globalDefaultTemplate );
}
return $this -> renderHIncludeTag ( $uri , $defaultContent );
}
/**
* Renders an HInclude tag .
*
* @ param string $uri A URI
* @ param string $defaultContent Default content
*/
protected function renderHIncludeTag ( $uri , $defaultContent = null )
{
return sprintf ( '<hx:include src="%s">%s</hx:include>' , $uri , $defaultContent );
}
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 ;
}
public function getName ()
{
return 'hinclude' ;
}
}