- minor compiler optimizations

This commit is contained in:
Uwe Tews
2015-06-27 21:23:22 +02:00
parent 291c06dbea
commit 38e47c7ee2
4 changed files with 33 additions and 45 deletions

View File

@@ -5,6 +5,7 @@
- update Smarty security with new realpath handling - update Smarty security with new realpath handling
- update {include_php} with new realpath handling - update {include_php} with new realpath handling
- move $smarty->loadPlugin() into extension - move $smarty->loadPlugin() into extension
- minor compiler optimizations
19.06.2015 19.06.2015
- improvement allow closures as callback at $smarty->registerFilter() https://github.com/smarty-php/smarty/issues/59 - improvement allow closures as callback at $smarty->registerFilter() https://github.com/smarty-php/smarty/issues/59

View File

@@ -106,7 +106,7 @@ class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCom
$this->lex->PrintTrace(); $this->lex->PrintTrace();
} }
// get tokens from lexer and parse them // get tokens from lexer and parse them
while ($this->lex->yylex() && !$this->abort_and_recompile) { while ($this->lex->yylex()) {
if ($this->smarty->_parserdebug) { if ($this->smarty->_parserdebug) {
echo "<pre>Line {$this->lex->line} Parsing {$this->parser->yyTokenName[$this->lex->token]} Token " . echo "<pre>Line {$this->lex->line} Parsing {$this->parser->yyTokenName[$this->lex->token]} Token " .
htmlentities($this->lex->value) . "</pre>"; htmlentities($this->lex->value) . "</pre>";
@@ -114,10 +114,6 @@ class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCom
$this->parser->doParse($this->lex->token, $this->lex->value); $this->parser->doParse($this->lex->token, $this->lex->value);
} }
if ($this->abort_and_recompile) {
// exit here on abort
return false;
}
// finish parsing process // finish parsing process
$this->parser->doParse(0, 0); $this->parser->doParse(0, 0);
if ($mbEncoding) { if ($mbEncoding) {

View File

@@ -825,7 +825,9 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/ */
public function loadCompiler() public function loadCompiler()
{ {
$this->smarty->loadPlugin($this->source->compiler_class); if (!class_exists($this->source->compiler_class)) {
$this->smarty->loadPlugin($this->source->compiler_class);
}
$this->compiler = new $this->source->compiler_class($this->source->template_lexer_class, $this->source->template_parser_class, $this->smarty); $this->compiler = new $this->source->compiler_class($this->source->template_lexer_class, $this->source->template_parser_class, $this->smarty);
} }

View File

@@ -249,13 +249,6 @@ abstract class Smarty_Internal_TemplateCompilerBase
*/ */
public $tag_nocache = false; public $tag_nocache = false;
/**
* Flag to restart parsing
*
* @var bool
*/
public $abort_and_recompile = false;
/** /**
* Compiled tag prefix code * Compiled tag prefix code
* *
@@ -362,35 +355,32 @@ abstract class Smarty_Internal_TemplateCompilerBase
Smarty_Internal_Debug::start_compile($this->template); Smarty_Internal_Debug::start_compile($this->template);
} }
$no_sources = count($this->sources); $no_sources = count($this->sources);
$this->parent_compiler->template->properties['file_dependency'][$this->template->source->uid] = array($this->template->source->filepath, $this->template->source->timestamp, $this->template->source->type); $this->parent_compiler->template->properties['file_dependency'][$this->template->source->uid] = array($this->template->source->filepath,
$this->template->source->timestamp, $this->template->source->type);
$loop ++; $loop ++;
if ($no_sources) { if ($no_sources) {
$this->inheritance_child = true; $this->inheritance_child = true;
} else { } else {
$this->inheritance_child = false; $this->inheritance_child = false;
} }
do { // flag for nochache sections
// flag for nochache sections $this->nocache = $nocache;
$this->nocache = $nocache; $this->tag_nocache = false;
$this->tag_nocache = false; // reset has nocache code flag
// reset has nocache code flag $this->template->has_nocache_code = false;
$this->template->has_nocache_code = false; $this->has_variable_string = false;
$this->has_variable_string = false; $this->prefix_code = array();
$this->prefix_code = array(); $_compiled_code = '';
$_compiled_code = ''; // get template source
// flag for aborting current and start recompile $_content = $this->template->source->content;
$this->abort_and_recompile = false; if ($_content != '') {
// get template source // run prefilter if required
$_content = $this->template->source->content; if ((isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) && !$this->suppressFilter) {
if ($_content != '') { $_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
// run prefilter if required
if ((isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre'])) && !$this->suppressFilter) {
$_content = Smarty_Internal_Filter_Handler::runFilter('pre', $_content, $template);
}
// call compiler
$_compiled_code = $this->doCompile($_content, true);
} }
} while ($this->abort_and_recompile); // call compiler
$_compiled_code = $this->doCompile($_content, true);
}
if ($this->smarty->debugging) { if ($this->smarty->debugging) {
Smarty_Internal_Debug::end_compile($this->template); Smarty_Internal_Debug::end_compile($this->template);
} }
@@ -401,7 +391,6 @@ abstract class Smarty_Internal_TemplateCompilerBase
$this->smarty->_current_file = $this->template->source->filepath; $this->smarty->_current_file = $this->template->source->filepath;
// free memory // free memory
unset($this->parser->root_buffer, $this->parser->current_buffer, $this->parser, $this->lex); unset($this->parser->root_buffer, $this->parser->current_buffer, $this->parser, $this->lex);
self::$_tag_objects = array();
// return compiled code to template object // return compiled code to template object
$merged_code = ''; $merged_code = '';
if (!empty($this->mergedSubTemplatesCode)) { if (!empty($this->mergedSubTemplatesCode)) {
@@ -482,8 +471,7 @@ abstract class Smarty_Internal_TemplateCompilerBase
$this->template->used_tags[] = array($tag, $args); $this->template->used_tags[] = array($tag, $args);
} }
// check nocache option flag // check nocache option flag
if (in_array("'nocache'", $args) || in_array(array('nocache' => 'true'), $args) || in_array(array('nocache' => '"true"'), $args) || in_array(array('nocache' => "'true'"), $args) if (in_array("'nocache'", $args) || in_array(array('nocache' => 'true'), $args) || in_array(array('nocache' => '"true"'), $args) || in_array(array('nocache' => "'true'"), $args)) {
) {
$this->tag_nocache = true; $this->tag_nocache = true;
} }
// compile the smarty tag (required compile classes to compile the tag are autoloaded) // compile the smarty tag (required compile classes to compile the tag are autoloaded)
@@ -522,8 +510,7 @@ abstract class Smarty_Internal_TemplateCompilerBase
// check if tag is a registered object // check if tag is a registered object
if (isset($this->smarty->registered_objects[$tag]) && isset($parameter['object_method'])) { if (isset($this->smarty->registered_objects[$tag]) && isset($parameter['object_method'])) {
$method = $parameter['object_method']; $method = $parameter['object_method'];
if (!in_array($method, $this->smarty->registered_objects[$tag][3]) && (empty($this->smarty->registered_objects[$tag][1]) || in_array($method, $this->smarty->registered_objects[$tag][1])) if (!in_array($method, $this->smarty->registered_objects[$tag][3]) && (empty($this->smarty->registered_objects[$tag][1]) || in_array($method, $this->smarty->registered_objects[$tag][1]))) {
) {
return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $method); return $this->callTagCompiler('private_object_function', $args, $parameter, $tag, $method);
} elseif (in_array($method, $this->smarty->registered_objects[$tag][3])) { } elseif (in_array($method, $this->smarty->registered_objects[$tag][3])) {
return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $method); return $this->callTagCompiler('private_object_block_function', $args, $parameter, $tag, $method);
@@ -533,7 +520,8 @@ abstract class Smarty_Internal_TemplateCompilerBase
} }
} }
// check if tag is registered // check if tag is registered
foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION, Smarty::PLUGIN_BLOCK) as $plugin_type) { foreach (array(Smarty::PLUGIN_COMPILER, Smarty::PLUGIN_FUNCTION,
Smarty::PLUGIN_BLOCK) as $plugin_type) {
if (isset($this->smarty->registered_plugins[$plugin_type][$tag])) { if (isset($this->smarty->registered_plugins[$plugin_type][$tag])) {
// if compiler function plugin call it now // if compiler function plugin call it now
if ($plugin_type == Smarty::PLUGIN_COMPILER) { if ($plugin_type == Smarty::PLUGIN_COMPILER) {
@@ -831,7 +819,8 @@ abstract class Smarty_Internal_TemplateCompilerBase
$callback = null; $callback = null;
$script = null; $script = null;
$cacheable = true; $cacheable = true;
$result = call_user_func_array($this->smarty->default_plugin_handler_func, array($tag, $plugin_type, $this->template, &$callback, &$script, &$cacheable)); $result = call_user_func_array($this->smarty->default_plugin_handler_func, array($tag, $plugin_type,
$this->template, &$callback, &$script, &$cacheable));
if ($result) { if ($result) {
$this->tag_nocache = $this->tag_nocache || !$cacheable; $this->tag_nocache = $this->tag_nocache || !$cacheable;
if ($script !== null) { if ($script !== null) {
@@ -898,8 +887,7 @@ abstract class Smarty_Internal_TemplateCompilerBase
// If the template is not evaluated and we have a nocache section and or a nocache tag // If the template is not evaluated and we have a nocache section and or a nocache tag
if ($is_code && !empty($content)) { if ($is_code && !empty($content)) {
// generate replacement code // generate replacement code
if ((!($this->template->source->recompiled) || $this->forceNocache) && $this->template->caching && !$this->suppressNocacheProcessing && ($this->nocache || $this->tag_nocache) if ((!($this->template->source->recompiled) || $this->forceNocache) && $this->template->caching && !$this->suppressNocacheProcessing && ($this->nocache || $this->tag_nocache)) {
) {
$this->template->has_nocache_code = true; $this->template->has_nocache_code = true;
$_output = addcslashes($content, '\'\\'); $_output = addcslashes($content, '\'\\');
$_output = str_replace("^#^", "'", $_output); $_output = str_replace("^#^", "'", $_output);
@@ -948,7 +936,8 @@ abstract class Smarty_Internal_TemplateCompilerBase
if ($this->smarty->debugging && $debug) { if ($this->smarty->debugging && $debug) {
Smarty_Internal_Debug::end_compile($this->template); Smarty_Internal_Debug::end_compile($this->template);
} }
array_push($this->trace_stack, array($this->smarty->_current_file, $this->trace_filepath, $this->trace_uid, $this->trace_line_offset)); array_push($this->trace_stack, array($this->smarty->_current_file, $this->trace_filepath, $this->trace_uid,
$this->trace_line_offset));
$this->trace_filepath = $this->smarty->_current_file = $file; $this->trace_filepath = $this->smarty->_current_file = $file;
$this->trace_uid = $uid; $this->trace_uid = $uid;
$this->trace_line_offset = $line; $this->trace_line_offset = $line;