diff options
author | Oleg Pudeyev <oleg@bsdpower.com> | 2011-05-03 23:53:22 -0400 |
---|---|---|
committer | Oleg Pudeyev <oleg@bsdpower.com> | 2011-05-04 01:21:44 -0400 |
commit | d06e59f63bc8213a4d679ff0c20a23dcf8cd524e (patch) | |
tree | 7c80b3768be92135840a8095bca0d0a3f6ed263b /phpBB/includes | |
parent | 237deb12cea909822e0f2fa3c072d31adfec3fb1 (diff) | |
download | forums-d06e59f63bc8213a4d679ff0c20a23dcf8cd524e.tar forums-d06e59f63bc8213a4d679ff0c20a23dcf8cd524e.tar.gz forums-d06e59f63bc8213a4d679ff0c20a23dcf8cd524e.tar.bz2 forums-d06e59f63bc8213a4d679ff0c20a23dcf8cd524e.tar.xz forums-d06e59f63bc8213a4d679ff0c20a23dcf8cd524e.zip |
[feature/template-engine] Split template execution logic into classes.
Template executor interface defines a template executor object.
It is an object which can execute (i.e. display/render) a template.
Currently there are two implementations:
* phpbb_template_executor_include includes php code from a file.
* phpbb_template_executor_eval eval's php code.
PHPBB3-9726
Diffstat (limited to 'phpBB/includes')
-rw-r--r-- | phpBB/includes/template.php | 107 | ||||
-rw-r--r-- | phpBB/includes/template_executor.php | 15 | ||||
-rw-r--r-- | phpBB/includes/template_executor_eval.php | 32 | ||||
-rw-r--r-- | phpBB/includes/template_executor_include.php | 32 |
4 files changed, 123 insertions, 63 deletions
diff --git a/phpBB/includes/template.php b/phpBB/includes/template.php index add52b5c30..8529b90719 100644 --- a/phpBB/includes/template.php +++ b/phpBB/includes/template.php @@ -263,22 +263,17 @@ class phpbb_template $_rootref = &$this->_rootref; $_lang = &$user->lang; - if (($filename = $this->_tpl_load($handle)) !== false) - { - ($include_once) ? include_once($filename) : include($filename); - } - else if (($code = $this->_tpl_eval($handle)) !== false) + $executor = $this->_tpl_load($handle); + + if ($executor) { - $code = ' ?> ' . $code . ' <?php '; - eval($code); + $executor->execute(); + return true; } else { - // if we could not eval AND the file exists, something horrific has occured return false; } - - return true; } /** @@ -311,11 +306,27 @@ class phpbb_template } /** - * Load a compiled template if possible, if not, recompile it + * Obtains a template executor for a template identified by specified + * handle. THe template executor can execute the template later. + * + * Template source will first be compiled into php code. + * If template cache is writable the compiled php code will be stored + * on filesystem and template will not be subsequently recompiled. + * If template cache is not writable template source will be recompiled + * every time it is needed. DEBUG_EXTRA define and load_tplcompile + * configuration setting may be used to force templates to be always + * recompiled. + * + * Returns an object implementing phpbb_template_executor, or null + * if template loading or compilation failed. Call execute() on the + * executor to execute the template. This will result in template + * contents sent to the output stream (unless, of course, output + * buffering is in effect). + * * @access private * @param string $handle Handle of the template to load - * @return string|bool Return filename on success otherwise false - * @uses template_compile is used to compile uncached templates + * @return phpbb_template_executor Template executor object, or null on failure + * @uses template_compile is used to compile template source */ private function _tpl_load($handle) { @@ -358,7 +369,7 @@ class phpbb_template // Recompile page if the original template is newer, otherwise load the compiled version if (!$recompile) { - return $filename; + return new phpbb_template_executor_include($filename); } // Inheritance - we point to another template file for this one. @@ -372,12 +383,21 @@ class phpbb_template $compile = new phpbb_template_compile(); - if ($compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)) === false) + $output_file = $this->_compiled_file_for_handle($handle); + if ($compile->compile_file_to_file($source_file, $output_file) !== false) { - return false; + $executor = new phpbb_template_executor_include($output_file); + } + else if (($code = $compile->compile_file($source_file)) !== false) + { + $executor = new phpbb_template_executor_eval($code); + } + else + { + $executor = null; } - return $filename; + return $executor; } /** @@ -419,28 +439,6 @@ class phpbb_template } /** - * This code should only run when some high level error prevents us from writing to the cache. - * @access private - * @param string $handle Template handle to compile - * @return string|bool Return compiled code on success otherwise false - * @uses template_compile is used to compile template - */ - private function _tpl_eval($handle) - { - $compile = new phpbb_template_compile(); - - $source_file = $this->_source_file_for_handle($handle); - - if (($code = $compile->compile_file($source_file)) === false) - { - return false; - } - - $compile->compile_file_to_file($source_file, $this->_compiled_file_for_handle($handle)); - return false; - } - - /** * Assign key variable pairs from an array * @access public * @param array $vararray A hash of variable name => value pairs @@ -696,32 +694,15 @@ class phpbb_template $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; } - $filename = $this->_tpl_load($handle); + $executor = $this->_tpl_load($handle); - if ($include) + if ($executor) { - global $user; - - $_tpldata = &$this->_tpldata; - $_rootref = &$this->_rootref; - $_lang = &$user->lang; - - if ($filename) - { - include($filename); - return; - } - else - { - $compile = new phpbb_template_compile(); - - $source_file = $this->_source_file_for_handle($handle); - if (($code = $compile->compile_file($source_file)) !== false) - { - $code = ' ?> ' . $code . ' <?php '; - eval($code); - } - } + $executor->execute(); + } + else + { + // What should we do here? } } diff --git a/phpBB/includes/template_executor.php b/phpBB/includes/template_executor.php new file mode 100644 index 0000000000..9bbb3dca0d --- /dev/null +++ b/phpBB/includes/template_executor.php @@ -0,0 +1,15 @@ +<?php + +/** +* Template executor interface. +* +* Objects implementing this interface encapsulate a means of executing +* (i.e. rendering) a template. +*/ +interface phpbb_template_executor +{ + /** + * Executes the template managed by this executor. + */ + public function execute(); +} diff --git a/phpBB/includes/template_executor_eval.php b/phpBB/includes/template_executor_eval.php new file mode 100644 index 0000000000..27bdf95b52 --- /dev/null +++ b/phpBB/includes/template_executor_eval.php @@ -0,0 +1,32 @@ +<?php + +/** +* Template executor that stores compiled template's php code and +* evaluates it via eval. +*/ +class phpbb_template_executor_eval implements phpbb_template_executor +{ + /** + * Template code to be eval'ed. + */ + private $code; + + /** + * Constructor. Stores provided code for future evaluation. + * + * @param string $code php code of the template + */ + public function __construct($code) + { + $this->code = $code; + } + + /** + * Executes the template managed by this executor by eval'ing php code + * of the template. + */ + public function execute() + { + eval($this->code); + } +} diff --git a/phpBB/includes/template_executor_include.php b/phpBB/includes/template_executor_include.php new file mode 100644 index 0000000000..74f0593b13 --- /dev/null +++ b/phpBB/includes/template_executor_include.php @@ -0,0 +1,32 @@ +<?php + +/** +* Template executor that stores path to php file with template code +* and evaluates it by including the file. +*/ +class phpbb_template_executor_include implements phpbb_template_executor +{ + /** + * Template path to be included. + */ + private $path; + + /** + * Constructor. Stores path to the template for future inclusion. + * + * @param string $path path to the template + */ + public function __construct($path) + { + $this->path = $path; + } + + /** + * Executes the template managed by this executor by including + * the php file containing the template. + */ + public function execute() + { + include($this->path); + } +} |