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
{
private $globalDefaultTemplate ;
2013-01-03 18:22:22 +00:00
private $signer ;
2013-02-10 11:34:10 +00:00
private $templating ;
2013-03-26 09:03:20 +00:00
private $charset ;
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-03-26 09:03:20 +00:00
* @ param string $charset
2013-01-04 17:38:08 +00:00
*/
2013-03-26 09:03:20 +00:00
public function __construct ( $templating = null , UriSigner $signer = null , $globalDefaultTemplate = null , $charset = 'utf-8' )
2013-02-10 11:34:10 +00:00
{
$this -> setTemplating ( $templating );
$this -> globalDefaultTemplate = $globalDefaultTemplate ;
$this -> signer = $signer ;
2013-03-26 09:03:20 +00:00
$this -> charset = $charset ;
2013-02-10 11:34:10 +00:00
}
/**
* Sets the templating engine to use to render the default content .
*
* @ param EngineInterface | \Twig_Environment | null $templating An EngineInterface or a \Twig_Environment instance
2013-03-26 09:03:20 +00:00
*
* @ throws \InvalidArgumentException
2013-02-10 11:34:10 +00:00
*/
public function setTemplating ( $templating )
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 ;
}
2013-02-19 07:24:52 +00:00
/**
* Checks if a templating engine has been set .
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the templating engine has been set , false otherwise
2013-02-19 07:24:52 +00:00
*/
public function hasTemplating ()
{
return null !== $this -> templating ;
}
2013-01-04 17:38:08 +00:00
/**
* { @ inheritdoc }
2013-01-04 17:47:22 +00:00
*
* Additional available options :
*
2013-03-26 09:03:20 +00:00
* * default : The default content ( it can be a template name or the content )
* * id : An optional hx : include tag id attribute
* * attributes : An optional array of hx : include tag attributes
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-09-08 08:24:10 +01:00
// we need to sign the absolute URI, but want to return the path only.
2013-09-08 18:54:19 +01:00
$uri = substr ( $this -> signer -> sign ( $this -> generateFragmentUri ( $uri , $request , true )), strlen ( $request -> getSchemeAndHttpHost ()));
2012-12-19 07:54:53 +00:00
}
2013-02-15 21:21:39 +00:00
// We need to replace ampersands in the URI with the encoded form in order to return valid html/xml content.
$uri = str_replace ( '&' , '&' , $uri );
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-03-26 09:03:20 +00:00
$attributes = isset ( $options [ 'attributes' ]) && is_array ( $options [ 'attributes' ]) ? $options [ 'attributes' ] : array ();
if ( isset ( $options [ 'id' ]) && $options [ 'id' ]) {
$attributes [ 'id' ] = $options [ 'id' ];
}
$renderedAttributes = '' ;
if ( count ( $attributes ) > 0 ) {
2014-11-17 14:32:09 +00:00
if ( PHP_VERSION_ID >= 50400 ) {
2014-11-01 12:49:09 +00:00
$flags = ENT_QUOTES | ENT_SUBSTITUTE ;
} else {
$flags = ENT_QUOTES ;
}
2013-07-01 13:24:43 +01:00
foreach ( $attributes as $attribute => $value ) {
2013-03-26 09:03:20 +00:00
$renderedAttributes .= sprintf (
' %s="%s"' ,
2014-11-01 12:49:09 +00:00
htmlspecialchars ( $attribute , $flags , $this -> charset , false ),
htmlspecialchars ( $value , $flags , $this -> charset , false )
2013-03-26 09:03:20 +00:00
);
}
2013-03-20 14:33:28 +00:00
}
2013-03-26 09:03:20 +00:00
return new Response ( sprintf ( '<hx:include src="%s"%s>%s</hx:include>' , $uri , $renderedAttributes , $content ));
2012-12-19 07:54:53 +00:00
}
2013-03-26 09:03:20 +00:00
/**
* @ param string $template
*
2014-04-16 11:30:19 +01:00
* @ return bool
2013-03-26 09:03:20 +00:00
*/
2012-12-19 07:54:53 +00:00
private function templateExists ( $template )
{
if ( $this -> templating instanceof EngineInterface ) {
2013-07-08 13:02:04 +01:00
try {
return $this -> templating -> exists ( $template );
} catch ( \InvalidArgumentException $e ) {
return false ;
}
2012-12-19 07:54:53 +00:00
}
$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' ;
}
}