aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOleg Pudeyev <oleg@bsdpower.com>2011-04-24 21:18:18 -0400
committerOleg Pudeyev <oleg@bsdpower.com>2011-04-24 21:18:18 -0400
commita2c75f60537ccf6a424c7f066eafbffc13c0c38d (patch)
treeffb6dbac52bbb4fd3fc8eca94450ac4d9e7387ab
parent81962d1d8ff083dd366c77e7d3858fd2c9ed7d43 (diff)
downloadforums-a2c75f60537ccf6a424c7f066eafbffc13c0c38d.tar
forums-a2c75f60537ccf6a424c7f066eafbffc13c0c38d.tar.gz
forums-a2c75f60537ccf6a424c7f066eafbffc13c0c38d.tar.bz2
forums-a2c75f60537ccf6a424c7f066eafbffc13c0c38d.tar.xz
forums-a2c75f60537ccf6a424c7f066eafbffc13c0c38d.zip
[feature/template-engine] Deleted $template from phpbb_template_compile class.
phpbb_template_compile is now much simpler. It takes complete file paths as inputs, either source template path or source template path and output compiled template path. The number of methods also went down to two - compile template and returned compiled text or compile and write to file. phpbb_compile class is responsible for determining source and compiled paths. It already had all the data necessary for this, now the code is in the same place as the data it uses. PHPBB3-9726
-rw-r--r--phpBB/includes/template.php67
-rw-r--r--phpBB/includes/template_compile.php100
-rw-r--r--tests/template/template_test.php3
3 files changed, 74 insertions, 96 deletions
diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php
index 2b4c7a09f7..e1e96791c9 100644
--- a/phpBB/includes/template.php
+++ b/phpBB/includes/template.php
@@ -366,7 +366,7 @@ class phpbb_template
{
return $filename;
}
-
+
// Inheritance - we point to another template file for this one. Equality is also used for store_db
if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'] && !file_exists($this->files[$handle]))
{
@@ -374,20 +374,54 @@ class phpbb_template
$this->files_template[$handle] = $user->theme['template_inherits_id'];
}
+ $source_file = $this->_source_file_for_handle($handle);
+
+ $compile = new phpbb_template_compile($this);
+
+ if ($compile->compile_write($source_file, $this->_compiled_file_for_handle($handle)) === false)
+ {
+ return false;
+ }
+
+ return $filename;
+ }
+
+ /**
+ * Resolves template handle $handle to source file path.
+ * @access private
+ * @param string $handle Template handle (i.e. "friendly" template name)
+ * @return string Source file path
+ */
+ private function _source_file_for_handle($handle)
+ {
// If we don't have a file assigned to this handle, die.
if (!isset($this->files[$handle]))
{
- trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR);
+ trigger_error("_source_file_for_handle(): No file specified for handle $handle", E_USER_ERROR);
}
- $compile = new phpbb_template_compile($this);
+ $source_file = $this->files[$handle];
- if ($compile->_tpl_load_file($handle) === false)
+ // Try and open template for reading
+ if (!file_exists($source_file))
{
- return false;
+ trigger_error("_source_file_for_handle(): File $source_file does not exist", E_USER_ERROR);
}
-
- return $filename;
+ return $source_file;
+ }
+
+ /**
+ * Determines compiled file path for handle $handle.
+ * @access private
+ * @param string $handle Template handle (i.e. "friendly" template name)
+ * @return string Compiled file path
+ */
+ private function _compiled_file_for_handle($handle)
+ {
+ global $phpEx;
+
+ $compiled_file = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $phpEx;
+ return $compiled_file;
}
/**
@@ -401,13 +435,9 @@ class phpbb_template
{
$compile = new phpbb_template_compile($this);
- // If we don't have a file assigned to this handle, die.
- if (!isset($this->files[$handle]))
- {
- trigger_error("template->_tpl_eval(): No file specified for handle $handle", E_USER_ERROR);
- }
+ $source_file = $this->_source_file_for_handle($handle);
- if (($code = $compile->_tpl_gen_src($handle)) === false)
+ if (($code = $compile->compile_gen($source_file)) === false)
{
return false;
}
@@ -473,13 +503,13 @@ class phpbb_template
{
if ($row['template_filename'] == $this->filename[$handle])
{
- $compile->_tpl_load_file($handle, true);
+ $compile->compile_write($source_file, $this->_compiled_file_for_handle($handle));
}
else
{
$this->files[$row['template_filename']] = $file;
$this->filename[$row['template_filename']] = $row['template_filename'];
- $compile->_tpl_load_file($row['template_filename'], true);
+ $compile->compile_write($this->_source_file_for_handle($row['template_filename']), $this->_compiled_file_for_handle($row['template_filename']));
unset($this->compiled_code[$row['template_filename']]);
unset($this->files[$row['template_filename']]);
unset($this->filename[$row['template_filename']]);
@@ -515,14 +545,14 @@ class phpbb_template
$this->files_template[$row['template_filename']] = $user->theme['template_inherits_id'];
}
// Try to load from filesystem and instruct to insert into the styles table...
- $compile->_tpl_load_file($handle, true);
+ $compile->compile_write($source_file, $this->_compiled_file_for_handle($handle));
return false;
}
return false;
}
- $compile->_tpl_load_file($handle);
+ $compile->compile_write($source_file, $this->_compiled_file_for_handle($handle));
return false;
}
@@ -801,7 +831,8 @@ class phpbb_template
{
$compile = new phpbb_template_compile($this);
- if (($code = $compile->_tpl_gen_src($handle)) !== false)
+ $source_file = $this->_source_file_for_handle($handle);
+ if (($code = $compile->compile_gen($source_file)) !== false)
{
$code = ' ?> ' . $code . ' <?php ';
eval($code);
diff --git a/phpBB/includes/template_compile.php b/phpBB/includes/template_compile.php
index 92695a54fa..07e497eca4 100644
--- a/phpBB/includes/template_compile.php
+++ b/phpBB/includes/template_compile.php
@@ -45,7 +45,7 @@ class phpbb_template_filter extends php_user_filter
* @var string
*/
private $chunk;
-
+
/**
* @var bool
*/
@@ -96,12 +96,12 @@ class phpbb_template_filter extends php_user_filter
private function compile($data)
{
$block_start_in_php = $this->in_php;
-
+
$data = preg_replace('#<(?:[\\?%]|script)#s', '<?php echo\'\\0\';?>', $data);
$data = preg_replace_callback(self::REGEX_TOKENS, array($this, 'replace'), $data);
global $config;
-
+
// Remove php
if (!$config['tpl_allow_php'])
{
@@ -117,33 +117,33 @@ class phpbb_template_filter extends php_user_filter
$data = preg_replace('~<!-- PHP -->.*?<!-- ENDPHP -->~', '', $data);
$data = preg_replace('~<!-- ENDPHP -->.*?$~', '', $data);
}
-
+
"?>/**/";
-
+
/*
Preserve whitespace.
PHP removes a newline after the closing tag (if it's there). This is by design.
-
-
+
+
Consider the following template:
-
+
<!-- IF condition -->
some content
<!-- ENDIF -->
-
+
If we were to simply preserve all whitespace, we could simply replace all "?>" tags
with "?>\n".
Doing that, would add additional newlines to the compiled tempalte in place of the
IF and ENDIF statements. These newlines are unwanted (and one is conditional).
The IF and ENDIF are usually on their own line for ease of reading.
-
+
This replacement preserves newlines only for statements that aren't the only statement on a line.
It will NOT preserve newlines at the end of statements in the above examle.
It will preserve newlines in situations like:
-
+
<!-- IF condition -->inline content<!-- ENDIF -->
-
-
+
+
*/
//*
$data = preg_replace('~(?<!^)(<\?php(?:(?<!\?>).)+(?<!/\*\*/)\?>)$~m', "$1\n", $data);
@@ -158,7 +158,7 @@ class phpbb_template_filter extends php_user_filter
{
return '';
}
-
+
if (isset($matches[3]))
{
return $this->compile_var_tags($matches[0]);
@@ -558,7 +558,7 @@ class phpbb_template_filter extends php_user_filter
$token = ($varrefs[2]) ? '$_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$_rootref[\'' . $varrefs[3] . '\']';
$vars[$token] = true;
}
-
+
}
else if (preg_match('#^\.((?:' . self::REGEX_NS . '\.?)+)$#s', $token, $varrefs))
{
@@ -867,70 +867,17 @@ stream_filter_register('phpbb_template', 'phpbb_template_filter');
class phpbb_template_compile
{
/**
- * @var phpbb_template Reference to the {@link phpbb_template template} object performing compilation
- */
- private $template;
-
- /**
- * Constructor
- * @param phpbb_template $template {@link phpbb_template Template} object performing compilation
- */
- public function __construct(phpbb_template $template)
- {
- $this->template = $template;
- }
-
- /**
- * Load template source from file
+ * Compiles template in $source_file and writes compiled template to
+ * cache directory
* @access public
- * @param string $handle Template handle we wish to load
- * @return bool Return true on success otherwise false
- */
- public function _tpl_load_file($handle)
- {
- // Try and open template for read
- if (!file_exists($this->template->files[$handle]))
- {
- trigger_error("template->_tpl_load_file(): File {$this->template->files[$handle]} does not exist or is empty", E_USER_ERROR);
- }
-
- // Actually compile the code now.
- return $this->compile_write($handle, $this->template->files[$handle]);
- }
-
- /**
- * Load template source from file
- * @access public
- * @param string $handle Template handle we wish to compile
- * @return string|bool Return compiled code on successful compilation otherwise false
- */
- public function _tpl_gen_src($handle)
- {
- // Try and open template for read
- if (!file_exists($this->template->files[$handle]))
- {
- trigger_error("template->_tpl_load_file(): File {$this->template->files[$handle]} does not exist or is empty", E_USER_ERROR);
- }
-
- // Actually compile the code now.
- return $this->compile_gen($this->template->files[$handle]);
- }
-
- /**
- * Write compiled file to cache directory
- * @access private
* @param string $handle Template handle to compile
* @param string $source_file Source template file
* @return bool Return true on success otherwise false
*/
- private function compile_write($handle, $source_file)
+ public function compile_write($source_file, $compiled_file)
{
- global $system, $phpEx;
-
- $filename = $this->template->cachepath . str_replace('/', '.', $this->template->filename[$handle]) . '.' . $phpEx;
-
$source_handle = @fopen($source_file, 'rb');
- $destination_handle = @fopen($filename, 'wb');
+ $destination_handle = @fopen($compiled_file, 'wb');
if (!$source_handle || !$destination_handle)
{
@@ -946,7 +893,7 @@ class phpbb_template_compile
@flock($destination_handle, LOCK_UN);
@fclose($destination_handle);
- phpbb_chmod($filename, CHMOD_READ | CHMOD_WRITE);
+ phpbb_chmod($compiled_file, CHMOD_READ | CHMOD_WRITE);
clearstatcache();
@@ -954,12 +901,13 @@ class phpbb_template_compile
}
/**
- * Generate source for eval()
- * @access private
+ * Compiles a template located at $source_file.
+ * Returns PHP source suitable for eval().
+ * @access public
* @param string $source_file Source template file
* @return string|bool Return compiled code on successful compilation otherwise false
*/
- private function compile_gen($source_file)
+ public function compile_gen($source_file)
{
$source_handle = @fopen($source_file, 'rb');
$destination_handle = @fopen('php://temp' ,'r+b');
diff --git a/tests/template/template_test.php b/tests/template/template_test.php
index a7c49927f1..e549c9a0c8 100644
--- a/tests/template/template_test.php
+++ b/tests/template/template_test.php
@@ -275,7 +275,7 @@ class phpbb_template_template_test extends phpbb_test_case
$this->template->set_filenames(array('test' => $filename));
$this->assertFileNotExists($this->template_path . '/' . $filename, 'Testing missing file, file cannot exist');
- $expecting = sprintf('template->_tpl_load_file(): File %s does not exist or is empty', realpath($this->template_path . '/../') . '/templates/' . $filename);
+ $expecting = sprintf('_source_file_for_handle(): File %s does not exist', realpath($this->template_path . '/../') . '/templates/' . $filename);
$this->setExpectedTriggerError(E_USER_ERROR, $expecting);
$this->display('test');
@@ -530,4 +530,3 @@ EOT
}
}
-