Done #231; Improve compile mechanism

This commit is contained in:
bzick 2016-05-30 14:25:08 +03:00
parent 180c171b1e
commit e557123e3c

View File

@ -1029,7 +1029,7 @@ class Fenom
*/ */
protected function _load($template, $opts) protected function _load($template, $opts)
{ {
$file_name = $this->_getCacheName($template, $opts); $file_name = $this->getCompilePath($template, $opts);
if (is_file($this->_compile_dir . "/" . $file_name)) { if (is_file($this->_compile_dir . "/" . $file_name)) {
$fenom = $this; // used in template $fenom = $this; // used in template
$_tpl = include($this->_compile_dir . "/" . $file_name); $_tpl = include($this->_compile_dir . "/" . $file_name);
@ -1049,7 +1049,7 @@ class Fenom
* @param int $options * @param int $options
* @return string * @return string
*/ */
private function _getCacheName($tpl, $options) public function getCompilePath($tpl, $options = 0)
{ {
if (is_array($tpl)) { if (is_array($tpl)) {
$hash = implode(".", $tpl) . ":" . $options; $hash = implode(".", $tpl) . ":" . $options;
@ -1084,17 +1084,15 @@ class Fenom
} }
} }
if ($store) { if ($store) {
$cache = $this->_getCacheName($tpl, $options); $cache_name = $this->getCompilePath($tpl, $options);
$tpl_tmp = tempnam($this->_compile_dir, $cache); $compile_path = $this->_compile_dir . "/" . $cache_name . "." . mt_rand(0, 100000) . ".tmp";
$tpl_fp = fopen($tpl_tmp, "w"); if(!file_put_contents($compile_path, $template->getTemplateCode())) {
if (!$tpl_fp) { throw new \RuntimeException("Can't to write to the file $compile_path. Directory " . $this->_compile_dir . " is writable?");
throw new \RuntimeException("Can't to open temporary file $tpl_tmp. Directory " . $this->_compile_dir . " is writable?");
} }
fwrite($tpl_fp, $template->getTemplateCode()); $cache_path = $this->_compile_dir . "/" . $cache_name;
fclose($tpl_fp); if (!rename($compile_path, $cache_path)) {
$file_name = $this->_compile_dir . "/" . $cache; unlink($compile_path);
if (!rename($tpl_tmp, $file_name)) { throw new \RuntimeException("Can't to move $compile_path to $cache_path");
throw new \RuntimeException("Can't to move $tpl_tmp to $file_name");
} }
} }
return $template; return $template;