mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-07 03:44:26 +02:00
update cache directory creation logic
This commit is contained in:
@@ -54,26 +54,22 @@ class Smarty
|
|||||||
// This is generally set to false once the
|
// This is generally set to false once the
|
||||||
// application is entered into production and
|
// application is entered into production and
|
||||||
// initially compiled. Leave set to true
|
// initially compiled. Leave set to true
|
||||||
// during development. true/false
|
// during development. true/false default true.
|
||||||
|
|
||||||
var $force_compile = false; // force templates to compile every time.
|
var $force_compile = false; // force templates to compile every time.
|
||||||
// overrides compile_check. true/false
|
// if cache file exists, this will
|
||||||
|
// override compile_check and force_compile.
|
||||||
// NOTE: all cache directives override
|
// true/false. default false.
|
||||||
// compiling directives. If a cached version
|
|
||||||
// is available, that will be used regardless
|
|
||||||
// of compile settings.
|
|
||||||
var $caching = false; // whether to use caching or not. true/false
|
var $caching = false; // whether to use caching or not. true/false
|
||||||
var $cache_dir = "./cache"; // name of directory for template cache
|
var $cache_dir = "./cache"; // name of directory for template cache
|
||||||
var $cache_lifetime = 3600; // number of seconds cached content will persist.
|
var $cache_lifetime = 3600; // number of seconds cached content will persist.
|
||||||
// 0 = never expires. default is one hour (3600)
|
// 0 = never expires. default is one hour (3600)
|
||||||
|
|
||||||
|
var $tpl_file_ext = ".tpl"; // template file extention
|
||||||
var $tpl_file_ext = ".tpl"; // template files extention
|
|
||||||
|
|
||||||
var $allow_php = false; // whether or not to allow embedded php
|
var $allow_php = false; // whether or not to allow embedded php
|
||||||
// in the templates. By default, php tags
|
// in the templates. By default, php tags
|
||||||
// are escaped. true/false
|
// are escaped. true/false. default false.
|
||||||
|
|
||||||
var $left_delimiter = "{"; // template tag delimiters.
|
var $left_delimiter = "{"; // template tag delimiters.
|
||||||
var $right_delimiter = "}";
|
var $right_delimiter = "}";
|
||||||
@@ -240,6 +236,33 @@ class Smarty
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: is_cached()
|
||||||
|
Purpose: test to see if valid cache exists for this template
|
||||||
|
\*======================================================================*/
|
||||||
|
|
||||||
|
function is_cached($tpl_file)
|
||||||
|
{
|
||||||
|
global $PHP_SELF;
|
||||||
|
|
||||||
|
// cache id = template path + the invoked script
|
||||||
|
$cache_tpl_md5 = md5($tpl_file);
|
||||||
|
$cache_path_md5 = md5($PHP_SELF);
|
||||||
|
$cache_path_dir = substr($cache_path_md5,0,2);
|
||||||
|
$cache_file = $this->cache_dir."/".$cache_tpl_md5."/$cache_path_dir/$cache_tpl_md5"."_"."$cache_path_md5.cache";
|
||||||
|
|
||||||
|
if(!$this->cache_force &&
|
||||||
|
(file_exists($cache_file) &&
|
||||||
|
($this->cache_lifetime == 0 ||
|
||||||
|
(mktime() - filemtime($cache_file) <= $this->cache_lifetime)
|
||||||
|
)))
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: clear_all_assign()
|
Function: clear_all_assign()
|
||||||
Purpose: clear all the assigned template variables.
|
Purpose: clear all the assigned template variables.
|
||||||
@@ -282,7 +305,11 @@ class Smarty
|
|||||||
|
|
||||||
if($this->caching) {
|
if($this->caching) {
|
||||||
// cache id = template path + the invoked script
|
// cache id = template path + the invoked script
|
||||||
$cache_file = $this->cache_dir."/".urlencode($tpl_file."@".$PHP_SELF).".cache";
|
$cache_tpl_md5 = md5($tpl_file);
|
||||||
|
$cache_path_md5 = md5($PHP_SELF);
|
||||||
|
$cache_path_dir = substr($cache_path_md5,0,2);
|
||||||
|
$cache_file = $this->cache_dir."/".$cache_tpl_md5."/$cache_path_dir/$cache_tpl_md5"."_"."$cache_path_md5.cache";
|
||||||
|
|
||||||
if(!$this->cache_force &&
|
if(!$this->cache_force &&
|
||||||
(file_exists($cache_file) &&
|
(file_exists($cache_file) &&
|
||||||
($this->cache_lifetime == 0 ||
|
($this->cache_lifetime == 0 ||
|
||||||
@@ -317,7 +344,7 @@ class Smarty
|
|||||||
}
|
}
|
||||||
|
|
||||||
if($this->caching) {
|
if($this->caching) {
|
||||||
$this->_write_file($cache_file, $results);
|
$this->_write_file($cache_file, $results, true);
|
||||||
$results = $this->_process_cached_inserts($results);
|
$results = $this->_process_cached_inserts($results);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1179,8 +1206,39 @@ class Smarty
|
|||||||
Purpose: write out a file
|
Purpose: write out a file
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
|
||||||
function _write_file($filename,$contents)
|
function _write_file($filename,$contents,$create_dirs=false)
|
||||||
{
|
{
|
||||||
|
if($create_dirs) {
|
||||||
|
$filename_split = split("\/+",$filename);
|
||||||
|
foreach($filename_split as $curr_dir) {
|
||||||
|
if(empty($curr_dir)) {
|
||||||
|
if(empty($dir_sum))
|
||||||
|
$dir_sum = "/";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($curr_dir == ".." || $curr_dir == ".") {
|
||||||
|
$dir_sum .= $curr_dir."/";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(substr($curr_dir,-6) == ".cache")
|
||||||
|
break;
|
||||||
|
|
||||||
|
if(empty($dir_sum))
|
||||||
|
$dir_sum .= $curr_dir;
|
||||||
|
else
|
||||||
|
$dir_sum .= "/".$curr_dir;
|
||||||
|
|
||||||
|
if(is_dir($dir_sum))
|
||||||
|
continue;
|
||||||
|
if(file_exists($dir_sum))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
mkdir($dir_sum,0755);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(!($fd = fopen($filename, 'w'))) {
|
if(!($fd = fopen($filename, 'w'))) {
|
||||||
$this->_set_error_msg("problem writing '$filename.'");
|
$this->_set_error_msg("problem writing '$filename.'");
|
||||||
return false;
|
return false;
|
||||||
|
24
docs.sgml
24
docs.sgml
@@ -441,6 +441,21 @@ chmod 700 cache
|
|||||||
1.3.0.
|
1.3.0.
|
||||||
</para>
|
</para>
|
||||||
</sect2>
|
</sect2>
|
||||||
|
<sect2 id="api.is.cached">
|
||||||
|
<title>is_cached</title>
|
||||||
|
<funcsynopsis>
|
||||||
|
<funcprototype>
|
||||||
|
<funcdef>void <function>is_cached</function></funcdef>
|
||||||
|
<paramdef>string<parameter>template</parameter></paramdef>
|
||||||
|
</funcprototype>
|
||||||
|
</funcsynopsis>
|
||||||
|
<para>
|
||||||
|
This returns true if there is a valid cache for this template.
|
||||||
|
Use this to skip process-intensive tasks that aren't necessary
|
||||||
|
when a cached version of the template is available. This was
|
||||||
|
added to Smarty 1.3.0.
|
||||||
|
</para>
|
||||||
|
</sect2>
|
||||||
<sect2>
|
<sect2>
|
||||||
<title>get_template_vars</title>
|
<title>get_template_vars</title>
|
||||||
<funcsynopsis>
|
<funcsynopsis>
|
||||||
@@ -488,6 +503,11 @@ chmod 700 cache
|
|||||||
include("Smarty.class.php");
|
include("Smarty.class.php");
|
||||||
$smarty = new Smarty;
|
$smarty = new Smarty;
|
||||||
|
|
||||||
|
|
||||||
|
// only do db calls if cache doesn't exist
|
||||||
|
if(!$smarty->is_cached("index.tpl"))
|
||||||
|
{
|
||||||
|
|
||||||
// dummy up some data
|
// dummy up some data
|
||||||
$address = "245 N 50th";
|
$address = "245 N 50th";
|
||||||
$db_data = array(
|
$db_data = array(
|
||||||
@@ -500,11 +520,13 @@ $smarty->assign("Name","Fred");
|
|||||||
$smarty->assign("Address",$address);
|
$smarty->assign("Address",$address);
|
||||||
$smarty->assign($db_data);
|
$smarty->assign($db_data);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// display the output
|
// display the output
|
||||||
$smarty->display("index.tpl");
|
$smarty->display("index.tpl");
|
||||||
|
|
||||||
// alternatively capture the output
|
// alternatively capture the output
|
||||||
$output = $smarty->fetch("index.tpl");
|
// $output = $smarty->fetch("index.tpl");
|
||||||
</programlisting>
|
</programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect2>
|
</sect2>
|
||||||
|
@@ -54,26 +54,22 @@ class Smarty
|
|||||||
// This is generally set to false once the
|
// This is generally set to false once the
|
||||||
// application is entered into production and
|
// application is entered into production and
|
||||||
// initially compiled. Leave set to true
|
// initially compiled. Leave set to true
|
||||||
// during development. true/false
|
// during development. true/false default true.
|
||||||
|
|
||||||
var $force_compile = false; // force templates to compile every time.
|
var $force_compile = false; // force templates to compile every time.
|
||||||
// overrides compile_check. true/false
|
// if cache file exists, this will
|
||||||
|
// override compile_check and force_compile.
|
||||||
// NOTE: all cache directives override
|
// true/false. default false.
|
||||||
// compiling directives. If a cached version
|
|
||||||
// is available, that will be used regardless
|
|
||||||
// of compile settings.
|
|
||||||
var $caching = false; // whether to use caching or not. true/false
|
var $caching = false; // whether to use caching or not. true/false
|
||||||
var $cache_dir = "./cache"; // name of directory for template cache
|
var $cache_dir = "./cache"; // name of directory for template cache
|
||||||
var $cache_lifetime = 3600; // number of seconds cached content will persist.
|
var $cache_lifetime = 3600; // number of seconds cached content will persist.
|
||||||
// 0 = never expires. default is one hour (3600)
|
// 0 = never expires. default is one hour (3600)
|
||||||
|
|
||||||
|
var $tpl_file_ext = ".tpl"; // template file extention
|
||||||
var $tpl_file_ext = ".tpl"; // template files extention
|
|
||||||
|
|
||||||
var $allow_php = false; // whether or not to allow embedded php
|
var $allow_php = false; // whether or not to allow embedded php
|
||||||
// in the templates. By default, php tags
|
// in the templates. By default, php tags
|
||||||
// are escaped. true/false
|
// are escaped. true/false. default false.
|
||||||
|
|
||||||
var $left_delimiter = "{"; // template tag delimiters.
|
var $left_delimiter = "{"; // template tag delimiters.
|
||||||
var $right_delimiter = "}";
|
var $right_delimiter = "}";
|
||||||
@@ -240,6 +236,33 @@ class Smarty
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*======================================================================*\
|
||||||
|
Function: is_cached()
|
||||||
|
Purpose: test to see if valid cache exists for this template
|
||||||
|
\*======================================================================*/
|
||||||
|
|
||||||
|
function is_cached($tpl_file)
|
||||||
|
{
|
||||||
|
global $PHP_SELF;
|
||||||
|
|
||||||
|
// cache id = template path + the invoked script
|
||||||
|
$cache_tpl_md5 = md5($tpl_file);
|
||||||
|
$cache_path_md5 = md5($PHP_SELF);
|
||||||
|
$cache_path_dir = substr($cache_path_md5,0,2);
|
||||||
|
$cache_file = $this->cache_dir."/".$cache_tpl_md5."/$cache_path_dir/$cache_tpl_md5"."_"."$cache_path_md5.cache";
|
||||||
|
|
||||||
|
if(!$this->cache_force &&
|
||||||
|
(file_exists($cache_file) &&
|
||||||
|
($this->cache_lifetime == 0 ||
|
||||||
|
(mktime() - filemtime($cache_file) <= $this->cache_lifetime)
|
||||||
|
)))
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
Function: clear_all_assign()
|
Function: clear_all_assign()
|
||||||
Purpose: clear all the assigned template variables.
|
Purpose: clear all the assigned template variables.
|
||||||
@@ -282,7 +305,11 @@ class Smarty
|
|||||||
|
|
||||||
if($this->caching) {
|
if($this->caching) {
|
||||||
// cache id = template path + the invoked script
|
// cache id = template path + the invoked script
|
||||||
$cache_file = $this->cache_dir."/".urlencode($tpl_file."@".$PHP_SELF).".cache";
|
$cache_tpl_md5 = md5($tpl_file);
|
||||||
|
$cache_path_md5 = md5($PHP_SELF);
|
||||||
|
$cache_path_dir = substr($cache_path_md5,0,2);
|
||||||
|
$cache_file = $this->cache_dir."/".$cache_tpl_md5."/$cache_path_dir/$cache_tpl_md5"."_"."$cache_path_md5.cache";
|
||||||
|
|
||||||
if(!$this->cache_force &&
|
if(!$this->cache_force &&
|
||||||
(file_exists($cache_file) &&
|
(file_exists($cache_file) &&
|
||||||
($this->cache_lifetime == 0 ||
|
($this->cache_lifetime == 0 ||
|
||||||
@@ -317,7 +344,7 @@ class Smarty
|
|||||||
}
|
}
|
||||||
|
|
||||||
if($this->caching) {
|
if($this->caching) {
|
||||||
$this->_write_file($cache_file, $results);
|
$this->_write_file($cache_file, $results, true);
|
||||||
$results = $this->_process_cached_inserts($results);
|
$results = $this->_process_cached_inserts($results);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1179,8 +1206,39 @@ class Smarty
|
|||||||
Purpose: write out a file
|
Purpose: write out a file
|
||||||
\*======================================================================*/
|
\*======================================================================*/
|
||||||
|
|
||||||
function _write_file($filename,$contents)
|
function _write_file($filename,$contents,$create_dirs=false)
|
||||||
{
|
{
|
||||||
|
if($create_dirs) {
|
||||||
|
$filename_split = split("\/+",$filename);
|
||||||
|
foreach($filename_split as $curr_dir) {
|
||||||
|
if(empty($curr_dir)) {
|
||||||
|
if(empty($dir_sum))
|
||||||
|
$dir_sum = "/";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($curr_dir == ".." || $curr_dir == ".") {
|
||||||
|
$dir_sum .= $curr_dir."/";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(substr($curr_dir,-6) == ".cache")
|
||||||
|
break;
|
||||||
|
|
||||||
|
if(empty($dir_sum))
|
||||||
|
$dir_sum .= $curr_dir;
|
||||||
|
else
|
||||||
|
$dir_sum .= "/".$curr_dir;
|
||||||
|
|
||||||
|
if(is_dir($dir_sum))
|
||||||
|
continue;
|
||||||
|
if(file_exists($dir_sum))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
mkdir($dir_sum,0755);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(!($fd = fopen($filename, 'w'))) {
|
if(!($fd = fopen($filename, 'w'))) {
|
||||||
$this->_set_error_msg("problem writing '$filename.'");
|
$this->_set_error_msg("problem writing '$filename.'");
|
||||||
return false;
|
return false;
|
||||||
|
Reference in New Issue
Block a user