mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 18:34:27 +02:00
compiler: add support for post compile callbacks
This commit is contained in:
@@ -37,6 +37,27 @@ class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCom
|
||||
*/
|
||||
public $local_var = array();
|
||||
|
||||
/**
|
||||
* array of callbacks called when the normal compile process of template is finished
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $postCompileCallbacks = array();
|
||||
|
||||
/**
|
||||
* prefix code
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $prefixCompiledCode = '';
|
||||
|
||||
/**
|
||||
* postfix code
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $postfixCompiledCode = '';
|
||||
|
||||
/**
|
||||
* Initialize compiler
|
||||
*
|
||||
@@ -68,8 +89,9 @@ class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCom
|
||||
tags in the templates are replaces with PHP code,
|
||||
then written to compiled files. */
|
||||
// init the lexer/parser to compile the template
|
||||
$this->parser = new $this->parser_class(new $this->lexer_class(str_replace(array("\r\n",
|
||||
"\r"), "\n", $_content), $this), $this);
|
||||
$this->parser =
|
||||
new $this->parser_class(new $this->lexer_class(str_replace(array("\r\n", "\r"), "\n", $_content), $this),
|
||||
$this);
|
||||
if ($isTemplateSource && $this->template->caching) {
|
||||
$this->parser->insertPhpCode("<?php\n\$_smarty_tpl->compiled->nocache_hash = '{$this->nocache_hash}';\n?>\n");
|
||||
}
|
||||
@@ -105,7 +127,44 @@ class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCom
|
||||
$this->trigger_template_error("unclosed {$this->smarty->left_delimiter}" . $openTag .
|
||||
"{$this->smarty->right_delimiter} tag");
|
||||
}
|
||||
// call post compile callbacks
|
||||
foreach ($this->postCompileCallbacks as $cb) {
|
||||
$parameter = $cb;
|
||||
$parameter[0] = $this;
|
||||
call_user_func_array($cb[0], $parameter);
|
||||
}
|
||||
// return compiled code
|
||||
return $this->parser->retvalue;
|
||||
return $this->prefixCompiledCode . $this->parser->retvalue . $this->postfixCompiledCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a post compile callback
|
||||
* - when the callback is called after template compiling the compiler object will be inserted as first parameter
|
||||
*
|
||||
* @param callback $callback
|
||||
* @param array $parameter optional parameter array
|
||||
* @param string $key optional key for callback
|
||||
* @param bool $replace if true replace existing keyed callback
|
||||
*/
|
||||
public function registerPostCompileCallback($callback, $parameter = array(), $key = null, $replace = false)
|
||||
{
|
||||
array_unshift($parameter, $callback);
|
||||
if (isset($key)) {
|
||||
if ($replace || !isset($this->postCompileCallbacks[$key])) {
|
||||
$this->postCompileCallbacks[$key] = $parameter;
|
||||
}
|
||||
} else {
|
||||
$this->postCompileCallbacks[] = $parameter;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a post compile callback
|
||||
*
|
||||
* @param string $key callback key
|
||||
*/
|
||||
public function unregisterPostCompileCallback($key)
|
||||
{
|
||||
unset($this->postCompileCallbacks[$key]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user