mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-28 06:08:52 +00:00
Template caching ( file based )
git-svn-id: file:///svn/phpbb/trunk@2642 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
parent
e733a0eca1
commit
5cff35d0f7
1 changed files with 101 additions and 48 deletions
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
/***************************************************************************
|
/***************************************************************************
|
||||||
* template.php
|
* template.inc
|
||||||
* -------------------
|
* -------------------
|
||||||
* begin : Saturday, Feb 13, 2001
|
* begin : Saturday, Feb 13, 2001
|
||||||
* copyright : (C) 2001 The phpBB Group
|
* copyright : (C) 2001 The phpBB Group
|
||||||
|
@ -28,7 +28,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Template {
|
class Template {
|
||||||
var $classname = "Template";
|
var $classname = 'Template';
|
||||||
|
|
||||||
// variable that holds all the data we'll be substituting into
|
// variable that holds all the data we'll be substituting into
|
||||||
// the compiled templates.
|
// the compiled templates.
|
||||||
|
@ -43,7 +43,7 @@ class Template {
|
||||||
var $files = array();
|
var $files = array();
|
||||||
|
|
||||||
// Root template directory.
|
// Root template directory.
|
||||||
var $root = "";
|
var $root = '';
|
||||||
|
|
||||||
// this will hash handle names to the compiled code for that handle.
|
// this will hash handle names to the compiled code for that handle.
|
||||||
var $compiled_code = array();
|
var $compiled_code = array();
|
||||||
|
@ -55,9 +55,12 @@ class Template {
|
||||||
* Constructor. Simply sets the root dir.
|
* Constructor. Simply sets the root dir.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function Template($root = ".")
|
function Template($root = '.')
|
||||||
{
|
{
|
||||||
|
global $board_config, $db;
|
||||||
|
|
||||||
$this->set_rootdir($root);
|
$this->set_rootdir($root);
|
||||||
|
$this->db = $db;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,6 +83,7 @@ class Template {
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->root = $dir;
|
$this->root = $dir;
|
||||||
|
$this->cachedir = $dir . '/cache/';
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,9 +98,11 @@ class Template {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
reset($filename_array);
|
$template_names = '';
|
||||||
while(list($handle, $filename) = each($filename_array))
|
@reset($filename_array);
|
||||||
|
while ( list($handle, $filename) = @each($filename_array) )
|
||||||
{
|
{
|
||||||
|
$this->filename[$handle] = $filename;
|
||||||
$this->files[$handle] = $this->make_filename($filename);
|
$this->files[$handle] = $this->make_filename($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,21 +116,41 @@ class Template {
|
||||||
* the results of executing the template.
|
* the results of executing the template.
|
||||||
*/
|
*/
|
||||||
function pparse($handle)
|
function pparse($handle)
|
||||||
|
{
|
||||||
|
$cache_file = $this->cachedir . $this->filename[$handle] . '.php';
|
||||||
|
|
||||||
|
if( @filemtime($cache_file) == @filemtime($this->files[$handle]) )
|
||||||
|
{
|
||||||
|
$_str = '';
|
||||||
|
include($cache_file);
|
||||||
|
|
||||||
|
if ( $_str != '' )
|
||||||
|
{
|
||||||
|
echo $_str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
if ( !$this->loadfile($handle) )
|
if ( !$this->loadfile($handle) )
|
||||||
{
|
{
|
||||||
die("Template->pparse(): Couldn't load template file for handle $handle");
|
die("Template->pparse(): Couldn't load template file for handle $handle");
|
||||||
}
|
}
|
||||||
|
|
||||||
// actually compile the template now.
|
//
|
||||||
if (!isset($this->compiled_code[$handle]) || empty($this->compiled_code[$handle]))
|
|
||||||
{
|
|
||||||
// Actually compile the code now.
|
// Actually compile the code now.
|
||||||
|
//
|
||||||
$this->compiled_code[$handle] = $this->compile($this->uncompiled_code[$handle]);
|
$this->compiled_code[$handle] = $this->compile($this->uncompiled_code[$handle]);
|
||||||
|
|
||||||
|
$fp = fopen($cache_file, 'w+');
|
||||||
|
fwrite ($fp, '<?php' . "\n" . $this->compiled_code[$handle] . "\n?" . '>');
|
||||||
|
fclose($fp);
|
||||||
|
|
||||||
|
touch($cache_file, filemtime($this->files[$handle]));
|
||||||
|
@chmod($cache_file, 0777);
|
||||||
|
|
||||||
|
eval($this->compiled_code[$handle]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the compiled code.
|
|
||||||
eval($this->compiled_code[$handle]);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,18 +163,37 @@ class Template {
|
||||||
* BEFORE calling this function.
|
* BEFORE calling this function.
|
||||||
*/
|
*/
|
||||||
function assign_var_from_handle($varname, $handle)
|
function assign_var_from_handle($varname, $handle)
|
||||||
|
{
|
||||||
|
$cache_file = $this->cachedir . $this->filename[$handle] . '.php';
|
||||||
|
|
||||||
|
if( @filemtime($cache_file) == @filemtime($this->files[$handle]) )
|
||||||
|
{
|
||||||
|
$_str = '';
|
||||||
|
include($cache_file);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
if ( !$this->loadfile($handle) )
|
if ( !$this->loadfile($handle) )
|
||||||
{
|
{
|
||||||
die("Template->assign_var_from_handle(): Couldn't load template file for handle $handle");
|
die("Template->pparse(): Couldn't load template file for handle $handle");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compile it, with the "no echo statements" option on.
|
|
||||||
$_str = "";
|
|
||||||
$code = $this->compile($this->uncompiled_code[$handle], true, '_str');
|
$code = $this->compile($this->uncompiled_code[$handle], true, '_str');
|
||||||
|
|
||||||
|
$fp = fopen($cache_file, 'w+');
|
||||||
|
fwrite ($fp, '<?php' . "\n" . $code . "\n?" . '>');
|
||||||
|
fclose($fp);
|
||||||
|
|
||||||
|
touch($cache_file, filemtime($this->files[$handle]));
|
||||||
|
@chmod($cache_file, 0777);
|
||||||
|
|
||||||
|
// Compile It, With The "no Echo Statements" Option On.
|
||||||
|
$_str = '';
|
||||||
// evaluate the variable assignment.
|
// evaluate the variable assignment.
|
||||||
eval($code);
|
eval($code);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// assign the value of the generated variable to the given varname.
|
// assign the value of the generated variable to the given varname.
|
||||||
$this->assign_var($varname, $_str);
|
$this->assign_var($varname, $_str);
|
||||||
|
|
||||||
|
@ -248,8 +293,15 @@ class Template {
|
||||||
*/
|
*/
|
||||||
function loadfile($handle)
|
function loadfile($handle)
|
||||||
{
|
{
|
||||||
|
switch ( $board_config['template_cache'] )
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
// If the file for this handle is already loaded and compiled, do nothing.
|
// If the file for this handle is already loaded and compiled, do nothing.
|
||||||
if (isset($this->uncompiled_code[$handle]) && !empty($this->uncompiled_code[$handle]))
|
if ( !empty($this->uncompiled_code[$handle]) )
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -262,13 +314,15 @@ class Template {
|
||||||
|
|
||||||
$filename = $this->files[$handle];
|
$filename = $this->files[$handle];
|
||||||
|
|
||||||
$str = implode("", @file($filename));
|
$str = implode('', @file($filename));
|
||||||
if (empty($str))
|
if (empty($str))
|
||||||
{
|
{
|
||||||
die("Template->loadfile(): File $filename for handle $handle is empty");
|
die("Template->loadfile(): File $filename for handle $handle is empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->uncompiled_code[$handle] = $str;
|
$this->uncompiled_code[$handle] = $str;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -311,7 +365,7 @@ class Template {
|
||||||
|
|
||||||
$block_nesting_level = 0;
|
$block_nesting_level = 0;
|
||||||
$block_names = array();
|
$block_names = array();
|
||||||
$block_names[0] = ".";
|
$block_names[0] = '.';
|
||||||
|
|
||||||
// Second: prepend echo ', append ' . "\n"; to each line.
|
// Second: prepend echo ', append ' . "\n"; to each line.
|
||||||
$line_count = sizeof($code_lines);
|
$line_count = sizeof($code_lines);
|
||||||
|
@ -413,7 +467,6 @@ class Template {
|
||||||
// Bring it back into a single string of lines of code.
|
// Bring it back into a single string of lines of code.
|
||||||
$code = implode("\n", $code_lines);
|
$code = implode("\n", $code_lines);
|
||||||
return $code;
|
return $code;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -454,7 +507,7 @@ class Template {
|
||||||
function generate_block_data_ref($blockname, $include_last_iterator)
|
function generate_block_data_ref($blockname, $include_last_iterator)
|
||||||
{
|
{
|
||||||
// Get an array of the blocks involved.
|
// Get an array of the blocks involved.
|
||||||
$blocks = explode(".", $blockname);
|
$blocks = explode('.', $blockname);
|
||||||
$blockcount = sizeof($blocks) - 1;
|
$blockcount = sizeof($blocks) - 1;
|
||||||
$varref = '$this->_tpldata';
|
$varref = '$this->_tpldata';
|
||||||
// Build up the string with everything but the last child.
|
// Build up the string with everything but the last child.
|
||||||
|
|
Loading…
Add table
Reference in a new issue