From f7b06ca12db9da519d49ce334eaf96573d003c0f Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sat, 9 Jul 2011 23:33:44 +0200 Subject: [feature/template-engine] Move template.php to includes/template This allows making use of autoloading. PHPBB3-9726 --- phpBB/includes/template/template.php | 549 +++++++++++++++++++++++++++++++++++ 1 file changed, 549 insertions(+) create mode 100644 phpBB/includes/template/template.php (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php new file mode 100644 index 0000000000..935605b12a --- /dev/null +++ b/phpBB/includes/template/template.php @@ -0,0 +1,549 @@ + $user->img('icon_contact', 'CONTACT', 'full'); +* +* More in-depth... +* yadayada +*/ + +/** +* Base Template class. +* @package phpBB3 +*/ +class phpbb_template +{ + /** + * @var phpbb_template_context Template context. + * Stores template data used during template rendering. + */ + private $context; + + /** + * @var string Root dir for template. + */ + private $root = ''; + + /** + * @var string Path of the cache directory for the template + */ + public $cachepath = ''; + + /** + * @var array Hash of handle => file path pairs + */ + public $files = array(); + + /** + * @var array Hash of handle => filename pairs + */ + public $filename = array(); + + public $files_inherit = array(); + public $files_template = array(); + public $inherit_root = ''; + + public $orig_tpl_inherits_id; + + /** + * @var string phpBB root path + */ + private $phpbb_root_path; + + /** + * @var phpEx PHP file extension + */ + private $phpEx; + + /** + * @var phpbb_config phpBB config instance + */ + private $config; + + /** + * @var user current user + */ + private $user; + + /** + * Constructor. + * + * @param string $phpbb_root_path phpBB root path + * @param user $user current user + */ + public function __construct($phpbb_root_path, $phpEx, $config, $user) + { + $this->phpbb_root_path = $phpbb_root_path; + $this->phpEx = $phpEx; + $this->config = $config; + $this->user = $user; + } + + /** + * Set template location + */ + public function set_template() + { + $template_path = $this->user->theme['template_path']; + if (file_exists($this->phpbb_root_path . 'styles/' . $template_path . '/template')) + { + $this->root = $this->phpbb_root_path . 'styles/' . $template_path . '/template'; + $this->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $template_path) . '_'; + + if ($this->orig_tpl_inherits_id === null) + { + $this->orig_tpl_inherits_id = $this->user->theme['template_inherits_id']; + } + + $this->user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; + + if ($this->user->theme['template_inherits_id']) + { + $this->inherit_root = $this->phpbb_root_path . 'styles/' . $this->user->theme['template_inherit_path'] . '/template'; + } + } + else + { + trigger_error('Template path could not be found: styles/' . $template_path . '/template', E_USER_ERROR); + } + + $this->context = new phpbb_template_context(); + + return true; + } + + /** + * Set custom template location (able to use directory outside of phpBB). + * + * Note: Templates are still compiled to phpBB's cache directory. + * + * @param string $template_path Path to template directory + * @param string $template_name Name of template + * @param string $fallback_template_path Path to fallback template + */ + public function set_custom_template($template_path, $template_name, $fallback_template_path = false) + { + // Make sure $template_path has no ending slash + if (substr($template_path, -1) == '/') + { + $template_path = substr($template_path, 0, -1); + } + + $this->root = $template_path; + $this->cachepath = $this->phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_'; + + if ($fallback_template_path !== false) + { + if (substr($fallback_template_path, -1) == '/') + { + $fallback_template_path = substr($fallback_template_path, 0, -1); + } + + $this->inherit_root = $fallback_template_path; + $this->orig_tpl_inherits_id = true; + } + else + { + $this->orig_tpl_inherits_id = false; + } + + $this->context = new phpbb_template_context(); + + return true; + } + + /** + * Sets the template filenames for handles. $filename_array + * should be a hash of handle => filename pairs. + * @param array $filname_array Should be a hash of handle => filename pairs. + */ + public function set_filenames(array $filename_array) + { + foreach ($filename_array as $handle => $filename) + { + if (empty($filename)) + { + trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR); + } + + $this->filename[$handle] = $filename; + $this->files[$handle] = $this->root . '/' . $filename; + + if ($this->inherit_root) + { + $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; + } + } + + return true; + } + + /** + * Clears all variables and blocks assigned to this template. + */ + public function destroy() + { + $this->context->clear(); + } + + /** + * Reset/empty complete block + * @param string $blockname Name of block to destroy + */ + public function destroy_block_vars($blockname) + { + $this->context->destroy_block_vars($blockname); + } + + /** + * Display handle + * @param string $handle Handle to display + * @param bool $include_once Allow multiple inclusions + * @return bool True on success, false on failure + */ + public function display($handle, $include_once = true) + { + $result = $this->call_hook($handle, $include_once); + if ($result !== false) + { + return $result[0]; + } + + $renderer = $this->_tpl_load($handle); + + if ($renderer) + { + $renderer->render($this->context, $this->get_lang()); + return true; + } + else + { + return false; + } + } + + /** + * Calls hook if any is defined. + * @param string $handle Template handle being displayed. + * @param bool $include_once Allow multiple inclusions + */ + private function call_hook($handle, $include_once) + { + global $phpbb_hook; + + if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this)) + { + if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__))) + { + $result = $phpbb_hook->hook_return_result(array(__CLASS__, __FUNCTION__)); + return array($result); + } + } + + return false; + } + + /** + * Obtains language array. + * This is either lang property of $user property, or if + * it is not set an empty array. + * @return array language entries + */ + public function get_lang() + { + if (isset($this->user->lang)) + { + $lang = $this->user->lang; + } + else + { + $lang = array(); + } + return $lang; + } + + /** + * Display the handle and assign the output to a template variable or return the compiled result. + * @param string $handle Handle to operate on + * @param string $template_var Template variable to assign compiled handle to + * @param bool $return_content If true return compiled handle, otherwise assign to $template_var + * @param bool $include_once Allow multiple inclusions of the file + * @return bool|string false on failure, otherwise if $return_content is true return string of the compiled handle, otherwise return true + */ + public function assign_display($handle, $template_var = '', $return_content = true, $include_once = false) + { + ob_start(); + $result = $this->display($handle, $include_once); + $contents = ob_get_clean(); + if ($result === false) + { + return false; + } + + if ($return_content) + { + return $contents; + } + + $this->assign_var($template_var, $contents); + + return true; + } + + /** + * Obtains a template renderer for a template identified by specified + * handle. The template renderer can display 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_renderer, or null + * if template loading or compilation failed. Call render() on the + * renderer to display the template. This will result in template + * contents sent to the output stream (unless, of course, output + * buffering is in effect). + * + * @param string $handle Handle of the template to load + * @return phpbb_template_renderer Template renderer object, or null on failure + * @uses template_compile is used to compile template source + */ + private function _tpl_load($handle) + { + if (!isset($this->filename[$handle])) + { + trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR); + } + + // reload this setting to have the values they had when this object was initialised + // using set_template or set_custom_template, they might otherwise have been overwritten + // by other template class instances in between. + $this->user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; + + $compiled_path = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $this->phpEx; + $this->files_template[$handle] = (isset($this->user->theme['template_id'])) ? $this->user->theme['template_id'] : 0; + + $recompile = defined('DEBUG_EXTRA') || + !file_exists($compiled_path) || + @filesize($compiled_path) === 0 || + ($this->config['load_tplcompile'] && @filemtime($compiled_path) < @filemtime($this->files[$handle])); + + if (!$recompile && $this->config['load_tplcompile']) + { + // No way around it: we need to check inheritance here + if ($this->user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) + { + $this->files[$handle] = $this->files_inherit[$handle]; + $this->files_template[$handle] = $this->user->theme['template_inherits_id']; + } + $recompile = (@filemtime($compiled_path) < @filemtime($this->files[$handle])) ? true : false; + } + + // Recompile page if the original template is newer, otherwise load the compiled version + if (!$recompile) + { + return new phpbb_template_renderer_include($compiled_path, $this); + } + + // Inheritance - we point to another template file for this one. + if (isset($this->user->theme['template_inherits_id']) && $this->user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) + { + $this->files[$handle] = $this->files_inherit[$handle]; + $this->files_template[$handle] = $this->user->theme['template_inherits_id']; + } + + $source_file = $this->_source_file_for_handle($handle); + + $compile = new phpbb_template_compile(); + + $output_file = $this->_compiled_file_for_handle($handle); + if ($compile->compile_file_to_file($source_file, $output_file) !== false) + { + $renderer = new phpbb_template_renderer_include($output_file, $this); + } + else if (($code = $compile->compile_file($source_file)) !== false) + { + $renderer = new phpbb_template_renderer_eval($code, $this); + } + else + { + $renderer = null; + } + + return $renderer; + } + + /** + * Resolves template handle $handle to source file path. + * @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("_source_file_for_handle(): No file specified for handle $handle", E_USER_ERROR); + } + + $source_file = $this->files[$handle]; + + // Try and open template for reading + if (!file_exists($source_file)) + { + trigger_error("_source_file_for_handle(): File $source_file does not exist", E_USER_ERROR); + } + return $source_file; + } + + /** + * Determines compiled file path for handle $handle. + * @param string $handle Template handle (i.e. "friendly" template name) + * @return string Compiled file path + */ + private function _compiled_file_for_handle($handle) + { + $compiled_file = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $this->phpEx; + return $compiled_file; + } + + /** + * Assign key variable pairs from an array + * @param array $vararray A hash of variable name => value pairs + */ + public function assign_vars(array $vararray) + { + foreach ($vararray as $key => $val) + { + $this->assign_var($key, $val); + } + } + + /** + * Assign a single variable to a single key + * @param string $varname Variable name + * @param string $varval Value to assign to variable + */ + public function assign_var($varname, $varval) + { + $this->context->assign_var($varname, $varval); + } + + // Docstring is copied from phpbb_template_context method with the same name. + /** + * Assign key variable pairs from an array to a specified block + * @param string $blockname Name of block to assign $vararray to + * @param array $vararray A hash of variable name => value pairs + */ + public function assign_block_vars($blockname, array $vararray) + { + return $this->context->assign_block_vars($blockname, $vararray); + } + + // Docstring is copied from phpbb_template_context method with the same name. + /** + * Change already assigned key variable pair (one-dimensional - single loop entry) + * + * An example of how to use this function: + * {@example alter_block_array.php} + * + * @param string $blockname the blockname, for example 'loop' + * @param array $vararray the var array to insert/add or merge + * @param mixed $key Key to search for + * + * array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position] + * + * int: Position [the position to change or insert at directly given] + * + * If key is false the position is set to 0 + * If key is true the position is set to the last entry + * + * @param string $mode Mode to execute (valid modes are 'insert' and 'change') + * + * If insert, the vararray is inserted at the given position (position counting from zero). + * If change, the current block gets merged with the vararray (resulting in new key/value pairs be added and existing keys be replaced by the new value). + * + * Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array) + * and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars) + * + * @return bool false on error, true on success + */ + public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert') + { + return $this->context->alter_block_array($blockname, $vararray, $key, $mode); + } + + /** + * Include a separate template + * @param string $filename Template filename to include + * @param bool $include True to include the file, false to just load it + * @uses template_compile is used to compile uncached templates + */ + public function _tpl_include($filename, $include = true) + { + $handle = $filename; + $this->filename[$handle] = $filename; + $this->files[$handle] = $this->root . '/' . $filename; + if ($this->inherit_root) + { + $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; + } + + $renderer = $this->_tpl_load($handle); + + if ($renderer) + { + $renderer->render($this->context, $this->get_lang()); + } + else + { + // trigger_error cannot be used here, as the output already started + echo 'template->_tpl_include(): Failed including ' . htmlspecialchars($handle) . "\n"; + } + } + + /** + * Include a php-file + */ + public function _php_include($filename) + { + if (is_absolute($filename)) + { + $file = $filename; + } + else + { + $file = $this->phpbb_root_path . $filename; + } + + if (!file_exists($file)) + { + // trigger_error cannot be used here, as the output already started + echo 'template->_php_include(): File ' . htmlspecialchars($file) . " does not exist\n"; + return; + } + include($file); + } +} -- cgit v1.2.1 From ae53623230a45eeedf50cc3f6220164d8cd256c3 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sun, 10 Jul 2011 00:35:07 +0200 Subject: [feature/template-engine] Refactor $config dependency out of the filter The template stream filter no longer depends on the $config global. Instead it uses a 'allow_php' param that is passed via stream_bucket_append's last argument. Tests also adjusted. PHPBB3-9726 --- phpBB/includes/template/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 935605b12a..2c121d7247 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -376,7 +376,7 @@ class phpbb_template $source_file = $this->_source_file_for_handle($handle); - $compile = new phpbb_template_compile(); + $compile = new phpbb_template_compile($this->config['tpl_allow_php']); $output_file = $this->_compiled_file_for_handle($handle); if ($compile->compile_file_to_file($source_file, $output_file) !== false) -- cgit v1.2.1 From c58b09e65d428b53d2e69abf00c5847829f18fc7 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sun, 10 Jul 2011 01:05:54 +0200 Subject: [feature/template-engine] Remove $include_once argument of display() PHPBB3-9726 --- phpBB/includes/template/template.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 2c121d7247..c16eddc426 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -216,12 +216,11 @@ class phpbb_template /** * Display handle * @param string $handle Handle to display - * @param bool $include_once Allow multiple inclusions * @return bool True on success, false on failure */ - public function display($handle, $include_once = true) + public function display($handle) { - $result = $this->call_hook($handle, $include_once); + $result = $this->call_hook($handle); if ($result !== false) { return $result[0]; @@ -243,13 +242,12 @@ class phpbb_template /** * Calls hook if any is defined. * @param string $handle Template handle being displayed. - * @param bool $include_once Allow multiple inclusions */ - private function call_hook($handle, $include_once) + private function call_hook($handle) { global $phpbb_hook; - if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $include_once, $this)) + if (!empty($phpbb_hook) && $phpbb_hook->call_hook(array(__CLASS__, __FUNCTION__), $handle, $this)) { if ($phpbb_hook->hook_return(array(__CLASS__, __FUNCTION__))) { @@ -285,13 +283,12 @@ class phpbb_template * @param string $handle Handle to operate on * @param string $template_var Template variable to assign compiled handle to * @param bool $return_content If true return compiled handle, otherwise assign to $template_var - * @param bool $include_once Allow multiple inclusions of the file * @return bool|string false on failure, otherwise if $return_content is true return string of the compiled handle, otherwise return true */ - public function assign_display($handle, $template_var = '', $return_content = true, $include_once = false) + public function assign_display($handle, $template_var = '', $return_content = true) { ob_start(); - $result = $this->display($handle, $include_once); + $result = $this->display($handle); $contents = ob_get_clean(); if ($result === false) { -- cgit v1.2.1 From e116561348b7bd3400bfe6acccf5eebaaaa7f562 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 24 Jul 2011 13:37:46 -0400 Subject: [feature/template-engine] Rename is_absolute to phpbb_is_absolute. PHPBB3-9726 --- phpBB/includes/template/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index c16eddc426..010284edf3 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -526,7 +526,7 @@ class phpbb_template */ public function _php_include($filename) { - if (is_absolute($filename)) + if (phpbb_is_absolute($filename)) { $file = $filename; } -- cgit v1.2.1 From 4126a571aceca6266b565a0ef1f85cef5230a521 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 30 Jul 2011 15:20:25 -0400 Subject: [feature/template-engine] Delete $files_template property. This seems to have been used for db storage of templates. We no longer offer db storage of templates, and thus currenty $files_template is only written to but not read anywhere. PHPBB3-9726 --- phpBB/includes/template/template.php | 4 ---- 1 file changed, 4 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 010284edf3..d2d035b3b5 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -58,7 +58,6 @@ class phpbb_template public $filename = array(); public $files_inherit = array(); - public $files_template = array(); public $inherit_root = ''; public $orig_tpl_inherits_id; @@ -340,7 +339,6 @@ class phpbb_template $this->user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; $compiled_path = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $this->phpEx; - $this->files_template[$handle] = (isset($this->user->theme['template_id'])) ? $this->user->theme['template_id'] : 0; $recompile = defined('DEBUG_EXTRA') || !file_exists($compiled_path) || @@ -353,7 +351,6 @@ class phpbb_template if ($this->user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) { $this->files[$handle] = $this->files_inherit[$handle]; - $this->files_template[$handle] = $this->user->theme['template_inherits_id']; } $recompile = (@filemtime($compiled_path) < @filemtime($this->files[$handle])) ? true : false; } @@ -368,7 +365,6 @@ class phpbb_template if (isset($this->user->theme['template_inherits_id']) && $this->user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) { $this->files[$handle] = $this->files_inherit[$handle]; - $this->files_template[$handle] = $this->user->theme['template_inherits_id']; } $source_file = $this->_source_file_for_handle($handle); -- cgit v1.2.1 From 05b71ca04e8584428d335512a199e8c71c53a74f Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 30 Jul 2011 17:06:22 -0400 Subject: [feature/template-engine] Factor template locator out of template class. Template locator is responsible for maintaining mapping from template handles to filenames and paths, and provides resolution services using these mappings. Template locator is aware of template inheritance and is capable of checking template file existence on the filesystem. PHPBB3-9726 --- phpBB/includes/template/template.php | 143 ++++++----------------------------- 1 file changed, 25 insertions(+), 118 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index d2d035b3b5..4ac15ae500 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -37,29 +37,11 @@ class phpbb_template */ private $context; - /** - * @var string Root dir for template. - */ - private $root = ''; - /** * @var string Path of the cache directory for the template */ public $cachepath = ''; - /** - * @var array Hash of handle => file path pairs - */ - public $files = array(); - - /** - * @var array Hash of handle => filename pairs - */ - public $filename = array(); - - public $files_inherit = array(); - public $inherit_root = ''; - public $orig_tpl_inherits_id; /** @@ -82,6 +64,11 @@ class phpbb_template */ private $user; + /** + * @var locator template locator + */ + private $locator; + /** * Constructor. * @@ -94,6 +81,7 @@ class phpbb_template $this->phpEx = $phpEx; $this->config = $config; $this->user = $user; + $this->locator = new phpbb_template_locator(); } /** @@ -101,23 +89,12 @@ class phpbb_template */ public function set_template() { - $template_path = $this->user->theme['template_path']; + $template_path = $style_name = $this->user->theme['template_path']; + $this->locator->set_template_path($style_name); + if (file_exists($this->phpbb_root_path . 'styles/' . $template_path . '/template')) { - $this->root = $this->phpbb_root_path . 'styles/' . $template_path . '/template'; $this->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $template_path) . '_'; - - if ($this->orig_tpl_inherits_id === null) - { - $this->orig_tpl_inherits_id = $this->user->theme['template_inherits_id']; - } - - $this->user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; - - if ($this->user->theme['template_inherits_id']) - { - $this->inherit_root = $this->phpbb_root_path . 'styles/' . $this->user->theme['template_inherit_path'] . '/template'; - } } else { @@ -138,32 +115,13 @@ class phpbb_template * @param string $template_name Name of template * @param string $fallback_template_path Path to fallback template */ - public function set_custom_template($template_path, $template_name, $fallback_template_path = false) + public function set_custom_template($template_path, $style_name, $fallback_template_path = false) { - // Make sure $template_path has no ending slash - if (substr($template_path, -1) == '/') - { - $template_path = substr($template_path, 0, -1); - } + $this->locator->set_custom_template($template_path, $style_name, $fallback_template_path); + $template_name = $style_name; - $this->root = $template_path; $this->cachepath = $this->phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_'; - if ($fallback_template_path !== false) - { - if (substr($fallback_template_path, -1) == '/') - { - $fallback_template_path = substr($fallback_template_path, 0, -1); - } - - $this->inherit_root = $fallback_template_path; - $this->orig_tpl_inherits_id = true; - } - else - { - $this->orig_tpl_inherits_id = false; - } - $this->context = new phpbb_template_context(); return true; @@ -176,21 +134,7 @@ class phpbb_template */ public function set_filenames(array $filename_array) { - foreach ($filename_array as $handle => $filename) - { - if (empty($filename)) - { - trigger_error("template->set_filenames: Empty filename specified for $handle", E_USER_ERROR); - } - - $this->filename[$handle] = $filename; - $this->files[$handle] = $this->root . '/' . $filename; - - if ($this->inherit_root) - { - $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; - } - } + $this->locator->set_filenames($filename_array); return true; } @@ -328,31 +272,25 @@ class phpbb_template */ private function _tpl_load($handle) { - if (!isset($this->filename[$handle])) - { - trigger_error("template->_tpl_load(): No file specified for handle $handle", E_USER_ERROR); - } + $virtual_source_file = $this->locator->get_virtual_source_file_for_handle($handle); + $source_file = null; // reload this setting to have the values they had when this object was initialised // using set_template or set_custom_template, they might otherwise have been overwritten // by other template class instances in between. $this->user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; - $compiled_path = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $this->phpEx; + $compiled_path = $this->cachepath . str_replace('/', '.', $virtual_source_file) . '.' . $this->phpEx; $recompile = defined('DEBUG_EXTRA') || !file_exists($compiled_path) || @filesize($compiled_path) === 0 || - ($this->config['load_tplcompile'] && @filemtime($compiled_path) < @filemtime($this->files[$handle])); + ($this->config['load_tplcompile'] && @filemtime($compiled_path) < @filemtime($source_file)); if (!$recompile && $this->config['load_tplcompile']) { - // No way around it: we need to check inheritance here - if ($this->user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) - { - $this->files[$handle] = $this->files_inherit[$handle]; - } - $recompile = (@filemtime($compiled_path) < @filemtime($this->files[$handle])) ? true : false; + $source_file = $this->locator->get_source_file_for_handle($handle); + $recompile = (@filemtime($compiled_path) < @filemtime($source_file)) ? true : false; } // Recompile page if the original template is newer, otherwise load the compiled version @@ -361,14 +299,11 @@ class phpbb_template return new phpbb_template_renderer_include($compiled_path, $this); } - // Inheritance - we point to another template file for this one. - if (isset($this->user->theme['template_inherits_id']) && $this->user->theme['template_inherits_id'] && !file_exists($this->files[$handle])) + if ($source_file === null) { - $this->files[$handle] = $this->files_inherit[$handle]; + $source_file = $this->locator->get_source_file_for_handle($handle); } - $source_file = $this->_source_file_for_handle($handle); - $compile = new phpbb_template_compile($this->config['tpl_allow_php']); $output_file = $this->_compiled_file_for_handle($handle); @@ -388,29 +323,6 @@ class phpbb_template return $renderer; } - /** - * Resolves template handle $handle to source file path. - * @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("_source_file_for_handle(): No file specified for handle $handle", E_USER_ERROR); - } - - $source_file = $this->files[$handle]; - - // Try and open template for reading - if (!file_exists($source_file)) - { - trigger_error("_source_file_for_handle(): File $source_file does not exist", E_USER_ERROR); - } - return $source_file; - } - /** * Determines compiled file path for handle $handle. * @param string $handle Template handle (i.e. "friendly" template name) @@ -418,7 +330,8 @@ class phpbb_template */ private function _compiled_file_for_handle($handle) { - $compiled_file = $this->cachepath . str_replace('/', '.', $this->filename[$handle]) . '.' . $this->phpEx; + $source_file = $this->locator->get_filename_for_handle($handle); + $compiled_file = $this->cachepath . str_replace('/', '.', $source_file) . '.' . $this->phpEx; return $compiled_file; } @@ -496,15 +409,9 @@ class phpbb_template */ public function _tpl_include($filename, $include = true) { - $handle = $filename; - $this->filename[$handle] = $filename; - $this->files[$handle] = $this->root . '/' . $filename; - if ($this->inherit_root) - { - $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename; - } + $this->locator->set_filenames(array($filename => $filename)); - $renderer = $this->_tpl_load($handle); + $renderer = $this->_tpl_load($filename); if ($renderer) { -- cgit v1.2.1 From 13536f2be5e9c26752a6c9c5db19892b3c8e5490 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Wed, 3 Aug 2011 01:09:37 -0400 Subject: [feature/template-engine] Add constructor to template locator. PHPBB3-9726 --- phpBB/includes/template/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 4ac15ae500..2f360e4df4 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -81,7 +81,7 @@ class phpbb_template $this->phpEx = $phpEx; $this->config = $config; $this->user = $user; - $this->locator = new phpbb_template_locator(); + $this->locator = new phpbb_template_locator($phpbb_root_path, $user); } /** -- cgit v1.2.1 From 1a6250d8b6b61b09c1713e3d8683a0c1dd8e0857 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 4 Aug 2011 21:24:40 -0400 Subject: [feature/template-engine] Delete $style_name param from locator's set_custom_template. This parameter was unused, it was only used by template's set_custom_template to determine cache file prefix. PHPBB3-9726 --- phpBB/includes/template/template.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 2f360e4df4..6a60ff34d6 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -117,10 +117,9 @@ class phpbb_template */ public function set_custom_template($template_path, $style_name, $fallback_template_path = false) { - $this->locator->set_custom_template($template_path, $style_name, $fallback_template_path); - $template_name = $style_name; + $this->locator->set_custom_template($template_path, $fallback_template_path); - $this->cachepath = $this->phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $template_name) . '_'; + $this->cachepath = $this->phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $style_name) . '_'; $this->context = new phpbb_template_context(); -- cgit v1.2.1 From 52f208900fb6baa470d0238829147b60e208060d Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 4 Aug 2011 21:31:58 -0400 Subject: [feature/template-engine] Get rid of orig_tpl_* in template engine. The origins of orig_tpl_* are not pretty. Please see the following commits and associated tickets: r9823, r9839, r9847, r10150, r10460. In short, multiple hacks were required due to template engine reading inheritance/storedb flags from $user (global) even when the template that was being looked up or rendered was not the "active style of the current user". We no longer store templates in the database, removing half of the problem. This commit fixes the second half of the problem by deleting set_template_path function from template locator, and moving that logic back into the template class' set_template. set_template now calls set_custom_template, the latter only taking the template path and the fallback paths as parameters. With this change template locator no longer uses $user and does not use phpbb root path either. All logic involving setting the user's "active" template is now encapsulated in a single template class's function, set_template. Setting other templates is done via set_custom_template and the caller is responsible for determining and passing in fallback/inheritance path, if any. PHPBB3-9726 --- phpBB/includes/template/template.php | 41 ++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 6a60ff34d6..ad5581499d 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -42,8 +42,6 @@ class phpbb_template */ public $cachepath = ''; - public $orig_tpl_inherits_id; - /** * @var string phpBB root path */ @@ -81,7 +79,7 @@ class phpbb_template $this->phpEx = $phpEx; $this->config = $config; $this->user = $user; - $this->locator = new phpbb_template_locator($phpbb_root_path, $user); + $this->locator = new phpbb_template_locator(); } /** @@ -89,18 +87,28 @@ class phpbb_template */ public function set_template() { - $template_path = $style_name = $this->user->theme['template_path']; - $this->locator->set_template_path($style_name); + $style_name = $this->user->theme['template_path']; + + $relative_template_root = $this->relative_template_root_for_style($style_name); + $template_root = $this->phpbb_root_path . $relative_template_root; + if (!file_exists($template_root)) + { + trigger_error('template locator: Template path could not be found: ' . $relative_template_root, E_USER_ERROR); + } - if (file_exists($this->phpbb_root_path . 'styles/' . $template_path . '/template')) + if ($this->user->theme['template_inherits_id']) { - $this->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $template_path) . '_'; + $fallback_template_path = $this->phpbb_root_path . $this->relative_template_root_for_style($this->user->theme['template_inherit_path']); } else { - trigger_error('Template path could not be found: styles/' . $template_path . '/template', E_USER_ERROR); + $fallback_template_path = null; } + $this->locator->set_custom_template($template_root, $fallback_template_path); + + $this->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $style_name) . '_'; + $this->context = new phpbb_template_context(); return true; @@ -126,6 +134,18 @@ class phpbb_template return true; } + /** + * Converts a style name to relative (to board root) path to + * the style's template files. + * + * @param $style_name string Style name + * @return string Path to style template files + */ + private function relative_template_root_for_style($style_name) + { + return 'styles/' . $style_name . '/template'; + } + /** * Sets the template filenames for handles. $filename_array * should be a hash of handle => filename pairs. @@ -274,11 +294,6 @@ class phpbb_template $virtual_source_file = $this->locator->get_virtual_source_file_for_handle($handle); $source_file = null; - // reload this setting to have the values they had when this object was initialised - // using set_template or set_custom_template, they might otherwise have been overwritten - // by other template class instances in between. - $this->user->theme['template_inherits_id'] = $this->orig_tpl_inherits_id; - $compiled_path = $this->cachepath . str_replace('/', '.', $virtual_source_file) . '.' . $this->phpEx; $recompile = defined('DEBUG_EXTRA') || -- cgit v1.2.1 From 0b381516a0e7f3559fb697d98aa28e480c60b823 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 7 Aug 2011 15:33:45 -0400 Subject: [feature/template-engine] Create load_and_render to reduce code duplication. PHPBB3-9726 --- phpBB/includes/template/template.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index ad5581499d..c368fd2621 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -188,6 +188,18 @@ class phpbb_template return $result[0]; } + return $this->load_and_render($handle); + } + + /** + * Loads a template for $handle, compiling it if necessary, and + * renders the template. + * + * @param string $handle Template handle to render + * @return bool True on success, false on failure + */ + private function load_and_render($handle) + { $renderer = $this->_tpl_load($handle); if ($renderer) @@ -425,13 +437,7 @@ class phpbb_template { $this->locator->set_filenames(array($filename => $filename)); - $renderer = $this->_tpl_load($filename); - - if ($renderer) - { - $renderer->render($this->context, $this->get_lang()); - } - else + if (!$this->load_and_render($handle)) { // trigger_error cannot be used here, as the output already started echo 'template->_tpl_include(): Failed including ' . htmlspecialchars($handle) . "\n"; -- cgit v1.2.1 From 02fc5330664120705a7812103046f88eab723d3e Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 7 Aug 2011 15:42:04 -0400 Subject: [feature/template-engine] More documentation for template class. PHPBB3-9726 --- phpBB/includes/template/template.php | 43 +++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index c368fd2621..5ac9a32724 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -83,7 +83,7 @@ class phpbb_template } /** - * Set template location + * Set template location based on (current) user's chosen style. */ public function set_template() { @@ -147,8 +147,8 @@ class phpbb_template } /** - * Sets the template filenames for handles. $filename_array - * should be a hash of handle => filename pairs. + * Sets the template filenames for handles. + * * @param array $filname_array Should be a hash of handle => filename pairs. */ public function set_filenames(array $filename_array) @@ -168,6 +168,7 @@ class phpbb_template /** * Reset/empty complete block + * * @param string $blockname Name of block to destroy */ public function destroy_block_vars($blockname) @@ -176,7 +177,12 @@ class phpbb_template } /** - * Display handle + * Display a template for provided handle. + * + * The template will be loaded and compiled, if necessary, first. + * + * This function calls hooks. + * * @param string $handle Handle to display * @return bool True on success, false on failure */ @@ -215,6 +221,7 @@ class phpbb_template /** * Calls hook if any is defined. + * * @param string $handle Template handle being displayed. */ private function call_hook($handle) @@ -253,7 +260,9 @@ class phpbb_template } /** - * Display the handle and assign the output to a template variable or return the compiled result. + * Display the handle and assign the output to a template variable + * or return the compiled result. + * * @param string $handle Handle to operate on * @param string $template_var Template variable to assign compiled handle to * @param bool $return_content If true return compiled handle, otherwise assign to $template_var @@ -351,6 +360,7 @@ class phpbb_template /** * Determines compiled file path for handle $handle. + * * @param string $handle Template handle (i.e. "friendly" template name) * @return string Compiled file path */ @@ -363,6 +373,7 @@ class phpbb_template /** * Assign key variable pairs from an array + * * @param array $vararray A hash of variable name => value pairs */ public function assign_vars(array $vararray) @@ -375,6 +386,7 @@ class phpbb_template /** * Assign a single variable to a single key + * * @param string $varname Variable name * @param string $varval Value to assign to variable */ @@ -428,7 +440,12 @@ class phpbb_template } /** - * Include a separate template + * Include a separate template. + * + * This function is marked public due to the way the template + * implementation uses it. It is actually an implementation function + * and should not be considered part of template class's public API. + * * @param string $filename Template filename to include * @param bool $include True to include the file, false to just load it * @uses template_compile is used to compile uncached templates @@ -437,7 +454,7 @@ class phpbb_template { $this->locator->set_filenames(array($filename => $filename)); - if (!$this->load_and_render($handle)) + if (!$this->load_and_render($filename)) { // trigger_error cannot be used here, as the output already started echo 'template->_tpl_include(): Failed including ' . htmlspecialchars($handle) . "\n"; @@ -445,7 +462,17 @@ class phpbb_template } /** - * Include a php-file + * Include a PHP file. + * + * If a relative path is passed in $filename, it is considered to be + * relative to board root ($phpbb_root_path). Absolute paths are + * also allowed. + * + * This function is marked public due to the way the template + * implementation uses it. It is actually an implementation function + * and should not be considered part of template class's public API. + * + * @param string $filename Path to PHP file to include */ public function _php_include($filename) { -- cgit v1.2.1 From df46a576e952881df963dcc03daede58dfb98927 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sun, 7 Aug 2011 19:26:28 -0400 Subject: [feature/template-engine] Use template engine class in bbcode class. PHPBB3-9726 --- phpBB/includes/template/template.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 5ac9a32724..71fecc7d26 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -493,4 +493,17 @@ class phpbb_template } include($file); } + + /** + * Retrieves the template locator object. + * + * This function is public for the benefit of bbcode implementation. + * It should not be considered part of template class's public API. + * + * @return phpbb_template_locator Template locator for this template + */ + public function _get_locator() + { + return $this->locator; + } } -- cgit v1.2.1 From 66232035aa1e0d81d5c8f141b45521998cd7207e Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Tue, 9 Aug 2011 23:20:37 -0400 Subject: [feature/template-engine] Delete useless code from set_template. set_custom_template performs these calls, repeating them in set_template is not needed. PHPBB3-9726 --- phpBB/includes/template/template.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 71fecc7d26..80052ba59a 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -105,13 +105,7 @@ class phpbb_template $fallback_template_path = null; } - $this->locator->set_custom_template($template_root, $fallback_template_path); - - $this->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $style_name) . '_'; - - $this->context = new phpbb_template_context(); - - return true; + return $this->locator->set_custom_template($template_root, $fallback_template_path); } /** -- cgit v1.2.1 From acb767f14d6300ebff793f87b1a62ebbb4bde82a Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Tue, 9 Aug 2011 23:28:40 -0400 Subject: [feature/template-engine] Dependency inject locator into template. PHPBB3-9726 --- phpBB/includes/template/template.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 80052ba59a..0409b66da6 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -72,14 +72,15 @@ class phpbb_template * * @param string $phpbb_root_path phpBB root path * @param user $user current user + * @param phpbb_template_locator $locator template locator */ - public function __construct($phpbb_root_path, $phpEx, $config, $user) + public function __construct($phpbb_root_path, $phpEx, $config, $user, phpbb_template_locator $locator) { $this->phpbb_root_path = $phpbb_root_path; $this->phpEx = $phpEx; $this->config = $config; $this->user = $user; - $this->locator = new phpbb_template_locator(); + $this->locator = $locator; } /** -- cgit v1.2.1 From fb8a1d999f95eddbd22c5d1e89acdb923caa4bef Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Tue, 9 Aug 2011 23:33:47 -0400 Subject: [feature/template-engine] Need to call set_template on template. PHPBB3-9726 --- phpBB/includes/template/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 0409b66da6..4bca813971 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -106,7 +106,7 @@ class phpbb_template $fallback_template_path = null; } - return $this->locator->set_custom_template($template_root, $fallback_template_path); + return $this->set_custom_template($template_root, $style_name, $fallback_template_path); } /** -- cgit v1.2.1 From 41de09e408bc314c3020084fb7c5ec4a4c7262b5 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Sat, 13 Aug 2011 23:47:24 -0400 Subject: [feature/template-engine] Delete _get_locator function. It is no longer needed as locator is injected into template. PHPBB3-9726 --- phpBB/includes/template/template.php | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 4bca813971..4007b77db8 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -488,17 +488,4 @@ class phpbb_template } include($file); } - - /** - * Retrieves the template locator object. - * - * This function is public for the benefit of bbcode implementation. - * It should not be considered part of template class's public API. - * - * @return phpbb_template_locator Template locator for this template - */ - public function _get_locator() - { - return $this->locator; - } } -- cgit v1.2.1 From 903bc5b78a6208089349d8db9abb37158e4b8e16 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 20 Sep 2011 23:33:08 +0100 Subject: [ticket/10374] Remove 'custom template' cache prefix. PHPBB3-10374 --- phpBB/includes/template/template.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 4007b77db8..0495ade9c5 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -122,7 +122,7 @@ class phpbb_template { $this->locator->set_custom_template($template_path, $fallback_template_path); - $this->cachepath = $this->phpbb_root_path . 'cache/ctpl_' . str_replace('_', '-', $style_name) . '_'; + $this->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $style_name) . '_'; $this->context = new phpbb_template_context(); -- cgit v1.2.1 From 25aee49ca510a4b209130d8e39d6d3f614a6ad7b Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 21 Sep 2011 13:17:10 +0100 Subject: [ticket/10375] Use existing method to generate cache file name. PHPBB3-10375 --- phpBB/includes/template/template.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index 0495ade9c5..d1aa67038c 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -307,10 +307,9 @@ class phpbb_template */ private function _tpl_load($handle) { - $virtual_source_file = $this->locator->get_virtual_source_file_for_handle($handle); $source_file = null; - $compiled_path = $this->cachepath . str_replace('/', '.', $virtual_source_file) . '.' . $this->phpEx; + $compiled_path = $this->_compiled_file_for_handle($handle); $recompile = defined('DEBUG_EXTRA') || !file_exists($compiled_path) || -- cgit v1.2.1 From 9d5e9af54b2bf45baa3faa6195c2c185c08a0d4e Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 21 Sep 2011 13:17:51 +0100 Subject: [ticket/10375] Make _tpl_load() a little leaner. - Removed duplicate variables - Set $source_file earlier for cache checks - Fixed useless mtime check PHPBB3-10375 --- phpBB/includes/template/template.php | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index d1aa67038c..e8220b25bf 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -307,35 +307,22 @@ class phpbb_template */ private function _tpl_load($handle) { - $source_file = null; - - $compiled_path = $this->_compiled_file_for_handle($handle); + $source_file = $this->locator->get_source_file_for_handle($handle); + $output_file = $this->_compiled_file_for_handle($handle); $recompile = defined('DEBUG_EXTRA') || - !file_exists($compiled_path) || - @filesize($compiled_path) === 0 || - ($this->config['load_tplcompile'] && @filemtime($compiled_path) < @filemtime($source_file)); - - if (!$recompile && $this->config['load_tplcompile']) - { - $source_file = $this->locator->get_source_file_for_handle($handle); - $recompile = (@filemtime($compiled_path) < @filemtime($source_file)) ? true : false; - } + !file_exists($output_file) || + @filesize($output_file) === 0 || + ($this->config['load_tplcompile'] && @filemtime($output_file) < @filemtime($source_file)); // Recompile page if the original template is newer, otherwise load the compiled version if (!$recompile) { - return new phpbb_template_renderer_include($compiled_path, $this); - } - - if ($source_file === null) - { - $source_file = $this->locator->get_source_file_for_handle($handle); + return new phpbb_template_renderer_include($output_file, $this); } $compile = new phpbb_template_compile($this->config['tpl_allow_php']); - $output_file = $this->_compiled_file_for_handle($handle); if ($compile->compile_file_to_file($source_file, $output_file) !== false) { $renderer = new phpbb_template_renderer_include($output_file, $this); -- cgit v1.2.1 From 95d24e6fb7389adfeaa46e97a49b1c15b5a740ca Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 21 Sep 2011 13:24:04 +0100 Subject: [ticket/10375] Rework $source_file setting. Only set the file if an mtime check or recompile are required. PHPBB3-10375 --- phpBB/includes/template/template.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/template/template.php') diff --git a/phpBB/includes/template/template.php b/phpBB/includes/template/template.php index e8220b25bf..ec5fbe2829 100644 --- a/phpBB/includes/template/template.php +++ b/phpBB/includes/template/template.php @@ -307,13 +307,22 @@ class phpbb_template */ private function _tpl_load($handle) { - $source_file = $this->locator->get_source_file_for_handle($handle); $output_file = $this->_compiled_file_for_handle($handle); $recompile = defined('DEBUG_EXTRA') || !file_exists($output_file) || - @filesize($output_file) === 0 || - ($this->config['load_tplcompile'] && @filemtime($output_file) < @filemtime($source_file)); + @filesize($output_file) === 0; + + if ($recompile || $this->config['load_tplcompile']) + { + // Set only if a recompile or an mtime check are required. + $source_file = $this->locator->get_source_file_for_handle($handle); + + if (!$recompile && @filemtime($output_file) < @filemtime($source_file)) + { + $recompile = true; + } + } // Recompile page if the original template is newer, otherwise load the compiled version if (!$recompile) -- cgit v1.2.1 From ea46feb11542a9cf54ce083ee0ad03f4c5e02a1e Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Tue, 30 Aug 2011 01:32:11 -0400 Subject: [feature/extension-manager] Add support for templates in extensions. This commit adds a template path provider to separate the process of locating (cached) paths in extensions from the template engine. The locator is supplied with a list of paths from the path provider. Admin templates can now be created in ext//adm/style/ and regular templates go into ext//styles/