2011-10-05 06:22:53 +02:00
< ? php
/*
* This file is part of Twig .
*
2018-05-10 12:24:53 +02:00
* ( c ) Fabien Potencier
2011-10-05 06:22:53 +02:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
/**
* Represents a macro node .
*
2013-08-01 21:20:12 +02:00
* @ author Fabien Potencier < fabien @ symfony . com >
2011-10-05 06:22:53 +02:00
*/
class Twig_Node_Macro extends Twig_Node
{
2018-05-10 12:24:53 +02:00
const VARARGS_NAME = 'varargs' ;
2011-10-05 06:22:53 +02:00
public function __construct ( $name , Twig_NodeInterface $body , Twig_NodeInterface $arguments , $lineno , $tag = null )
{
2018-05-10 12:24:53 +02:00
foreach ( $arguments as $argumentName => $argument ) {
if ( self :: VARARGS_NAME === $argumentName ) {
throw new Twig_Error_Syntax ( sprintf ( 'The argument "%s" in macro "%s" cannot be defined because the variable "%s" is reserved for arbitrary arguments.' , self :: VARARGS_NAME , $name , self :: VARARGS_NAME ), $argument -> getTemplateLine ());
}
}
parent :: __construct ( array ( 'body' => $body , 'arguments' => $arguments ), array ( 'name' => $name ), $lineno , $tag );
2011-10-05 06:22:53 +02:00
}
public function compile ( Twig_Compiler $compiler )
{
2013-08-01 21:20:12 +02:00
$compiler
-> addDebugInfo ( $this )
2018-05-10 12:24:53 +02:00
-> write ( sprintf ( 'public function get%s(' , $this -> getAttribute ( 'name' )))
2013-08-01 21:20:12 +02:00
;
$count = count ( $this -> getNode ( 'arguments' ));
$pos = 0 ;
foreach ( $this -> getNode ( 'arguments' ) as $name => $default ) {
$compiler
2018-05-10 12:24:53 +02:00
-> raw ( '$__' . $name . '__ = ' )
2013-08-01 21:20:12 +02:00
-> subcompile ( $default )
;
if ( ++ $pos < $count ) {
$compiler -> raw ( ', ' );
}
2011-10-05 06:22:53 +02:00
}
2018-05-10 12:24:53 +02:00
if ( PHP_VERSION_ID >= 50600 ) {
if ( $count ) {
$compiler -> raw ( ', ' );
}
$compiler -> raw ( '...$__varargs__' );
}
2011-10-05 06:22:53 +02:00
$compiler
2013-08-01 21:20:12 +02:00
-> raw ( " ) \n " )
-> write ( " { \n " )
2011-10-05 06:22:53 +02:00
-> indent ()
;
2018-05-10 12:24:53 +02:00
$compiler
-> write ( " \$ context = \$ this->env->mergeGlobals(array( \n " )
-> indent ()
;
foreach ( $this -> getNode ( 'arguments' ) as $name => $default ) {
2011-10-05 06:22:53 +02:00
$compiler
2018-05-10 12:24:53 +02:00
-> write ( '' )
-> string ( $name )
-> raw ( ' => $__' . $name . '__' )
-> raw ( " , \n " )
2013-08-01 21:20:12 +02:00
;
2018-05-10 12:24:53 +02:00
}
2013-08-01 21:20:12 +02:00
2018-05-10 12:24:53 +02:00
$compiler
-> write ( '' )
-> string ( self :: VARARGS_NAME )
-> raw ( ' => ' )
;
2013-08-01 21:20:12 +02:00
2018-05-10 12:24:53 +02:00
if ( PHP_VERSION_ID >= 50600 ) {
$compiler -> raw ( " \$ __varargs__, \n " );
} else {
2013-08-01 21:20:12 +02:00
$compiler
2018-05-10 12:24:53 +02:00
-> raw ( 'func_num_args() > ' )
-> repr ( $count )
-> raw ( ' ? array_slice(func_get_args(), ' )
-> repr ( $count )
-> raw ( " ) : array(), \n " )
2011-10-05 06:22:53 +02:00
;
}
$compiler
2018-05-10 12:24:53 +02:00
-> outdent ()
-> write ( " )); \n \n " )
2013-08-01 21:20:12 +02:00
-> write ( " \$ blocks = array(); \n \n " )
2011-10-05 06:22:53 +02:00
-> write ( " ob_start(); \n " )
-> write ( " try { \n " )
-> indent ()
-> subcompile ( $this -> getNode ( 'body' ))
-> outdent ()
2013-08-01 21:20:12 +02:00
-> write ( " } catch (Exception \$ e) { \n " )
2011-10-05 06:22:53 +02:00
-> indent ()
-> write ( " ob_end_clean(); \n \n " )
-> write ( " throw \$ e; \n " )
-> outdent ()
2018-05-10 12:24:53 +02:00
-> write ( " } catch (Throwable \$ e) { \n " )
-> indent ()
-> write ( " ob_end_clean(); \n \n " )
-> write ( " throw \$ e; \n " )
-> outdent ()
2011-10-05 06:22:53 +02:00
-> write ( " } \n \n " )
2013-08-01 21:20:12 +02:00
-> write ( " return ('' === \$ tmp = ob_get_clean()) ? '' : new Twig_Markup( \$ tmp, \$ this->env->getCharset()); \n " )
2011-10-05 06:22:53 +02:00
-> outdent ()
-> write ( " } \n \n " )
;
}
}
2018-05-10 12:24:53 +02:00
class_alias ( 'Twig_Node_Macro' , 'Twig\Node\MacroNode' , false );