- compile locking by touching old compiled files to avoid concurrent compilations

This commit is contained in:
Uwe.Tews
2010-03-31 16:23:01 +00:00
parent 24d5ad78f3
commit b52e9f1ac8
7 changed files with 278 additions and 280 deletions

View File

@@ -1,3 +1,6 @@
31/03/2010
- compile locking by touching old compiled files to avoid concurrent compilations
29/03/2010 29/03/2010
- bugfix allow array definitions as modifier parameter - bugfix allow array definitions as modifier parameter
- bugfix observe compile_check property when loading config files - bugfix observe compile_check property when loading config files

View File

@@ -129,6 +129,8 @@ class Smarty extends Smarty_Internal_Data {
public $force_compile = false; public $force_compile = false;
// check template for modifications? // check template for modifications?
public $compile_check = true; public $compile_check = true;
// locking concurrent compiles
public $compile_locking = true;
// use sub dirs for compiled/cached files? // use sub dirs for compiled/cached files?
public $use_sub_dirs = false; public $use_sub_dirs = false;
// compile_error? // compile_error?

View File

@@ -205,18 +205,26 @@ class Smarty_Internal_Config {
// load compiler // load compiler
$this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty); $this->compiler_object = new Smarty_Internal_Config_File_Compiler($this->smarty);
} }
// compile locking
if ($this->smarty->compile_locking) {
if ($saved_timestamp = $this->getCompiledTimestamp()) {
touch($this->getCompiledFilepath());
}
}
// call compiler // call compiler
if ($this->compiler_object->compileSource($this)) { try {
$this->compiler_object->compileSource($this);
}
catch (Exception $e) {
// restore old timestamp in case of error
if ($this->smarty->compile_locking && $saved_timestamp) {
touch($this->getCompiledFilepath(), $saved_timestamp);
}
throw $e;
}
// compiling succeded // compiling succeded
// write compiled template // write compiled template
Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty); Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->getCompiledConfig(), $this->smarty);
// make template and compiled file timestamp match
touch($this->getCompiledFilepath(), $this->getTimestamp());
} else {
// error compiling template
throw new Exception("Error compiling template {$this->getConfigFilepath ()}");
return false;
}
} }
/* /*

View File

@@ -14,7 +14,6 @@
* Main config file compiler class * Main config file compiler class
*/ */
class Smarty_Internal_Config_File_Compiler { class Smarty_Internal_Config_File_Compiler {
public $compile_error = false;
/** /**
* Initialize compiler * Initialize compiler
*/ */
@@ -59,12 +58,6 @@ class Smarty_Internal_Config_File_Compiler {
$parser->doParse(0, 0); $parser->doParse(0, 0);
$config->compiled_config = serialize($this->config_data); $config->compiled_config = serialize($this->config_data);
if (!$this->compile_error) {
return true;
} else {
// compilation error
return false;
}
} }
/** /**
* display compiler error messages without dying * display compiler error messages without dying
@@ -107,8 +100,6 @@ class Smarty_Internal_Config_File_Compiler {
$error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect); $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
} }
throw new Exception($error_text); throw new Exception($error_text);
// set error flag
$this->compile_error = true;
} }
} }

View File

@@ -59,15 +59,9 @@ class Smarty_Internal_SmartyTemplateCompiler extends Smarty_Internal_TemplateCom
list($_open_tag, $_data) = array_pop($this->_tag_stack); list($_open_tag, $_data) = array_pop($this->_tag_stack);
$this->trigger_template_error("unclosed {" . $_open_tag . "} tag"); $this->trigger_template_error("unclosed {" . $_open_tag . "} tag");
} }
if (!$this->compile_error) {
// return compiled code // return compiled code
// return str_replace(array("? >\n<?php","? ><?php"), array('',''), $this->parser->retvalue); // return str_replace(array("? >\n<?php","? ><?php"), array('',''), $this->parser->retvalue);
return $this->parser->retvalue; return $this->parser->retvalue;
} else {
// compilation error
return false;
}
} }
} }

View File

@@ -255,18 +255,28 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
$this->smarty->loadPlugin($this->resource_object->compiler_class); $this->smarty->loadPlugin($this->resource_object->compiler_class);
$this->compiler_object = new $this->resource_object->compiler_class($this->resource_object->template_lexer_class, $this->resource_object->template_parser_class, $this->smarty); $this->compiler_object = new $this->resource_object->compiler_class($this->resource_object->template_lexer_class, $this->resource_object->template_parser_class, $this->smarty);
} }
// compile locking
if ($this->smarty->compile_locking && !$this->resource_object->isEvaluated) {
if ($saved_timestamp = $this->getCompiledTimestamp()) {
touch($this->getCompiledFilepath());
}
}
// call compiler // call compiler
if ($this->compiler_object->compileTemplate($this)) { try {
$this->compiler_object->compileTemplate($this);
}
catch (Exception $e) {
// restore old timestamp in case of error
if ($this->smarty->compile_locking && !$this->resource_object->isEvaluated && $saved_timestamp) {
touch($this->getCompiledFilepath(), $saved_timestamp);
}
throw $e;
}
// compiling succeded // compiling succeded
if (!$this->resource_object->isEvaluated) { if (!$this->resource_object->isEvaluated) {
// write compiled template // write compiled template
Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->compiled_template, $this->smarty); Smarty_Internal_Write_File::writeFile($this->getCompiledFilepath(), $this->compiled_template, $this->smarty);
} }
} else {
// error compiling template
throw new Exception("Error compiling template {$this->getTemplateFilepath ()}");
return false;
}
if ($this->smarty->debugging) { if ($this->smarty->debugging) {
Smarty_Internal_Debug::end_compile($this); Smarty_Internal_Debug::end_compile($this);
} }

View File

@@ -50,8 +50,6 @@ class Smarty_Internal_TemplateCompilerBase {
// flag for nochache sections // flag for nochache sections
$this->nocache = false; $this->nocache = false;
$this->tag_nocache = false; $this->tag_nocache = false;
// assume successfull compiling
$this->compile_error = false;
// save template object in compiler class // save template object in compiler class
$this->template = $template; $this->template = $template;
$this->smarty->_current_file = $this->template->getTemplateFilepath(); $this->smarty->_current_file = $this->template->getTemplateFilepath();
@@ -83,7 +81,6 @@ class Smarty_Internal_TemplateCompilerBase {
// call compiler // call compiler
$_compiled_code = $this->doCompile($_content); $_compiled_code = $this->doCompile($_content);
} while ($this->abort_and_recompile); } while ($this->abort_and_recompile);
if (!$this->compile_error) {
// return compiled code to template object // return compiled code to template object
if ($template->suppressFileDependency) { if ($template->suppressFileDependency) {
$template->compiled_template = $_compiled_code; $template->compiled_template = $_compiled_code;
@@ -94,11 +91,6 @@ class Smarty_Internal_TemplateCompilerBase {
if (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) { if (isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post'])) {
$template->compiled_template = Smarty_Internal_Filter_Handler::runFilter('post', $template->compiled_template, $this->smarty, $template); $template->compiled_template = Smarty_Internal_Filter_Handler::runFilter('post', $template->compiled_template, $this->smarty, $template);
} }
return true;
} else {
// compilation error
return false;
}
} }
/** /**
@@ -430,8 +422,6 @@ class Smarty_Internal_TemplateCompilerBase {
$error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect); $error_text .= ' - Unexpected "' . $this->lex->value . '", expected one of: ' . implode(' , ', $expect);
} }
throw new Exception($error_text); throw new Exception($error_text);
// set error flag
$this->compile_error = true;
} }
} }