aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/template/locator.php
diff options
context:
space:
mode:
authorNils Adermann <naderman@naderman.de>2011-08-30 01:32:11 -0400
committerNils Adermann <naderman@naderman.de>2011-09-29 16:15:53 +0200
commitea46feb11542a9cf54ce083ee0ad03f4c5e02a1e (patch)
tree0990527e0c05bd9da495a063350ec199eb2ed227 /phpBB/includes/template/locator.php
parent6ea6d50ccb9607429486a01d3144c7d32322e1b5 (diff)
downloadforums-ea46feb11542a9cf54ce083ee0ad03f4c5e02a1e.tar
forums-ea46feb11542a9cf54ce083ee0ad03f4c5e02a1e.tar.gz
forums-ea46feb11542a9cf54ce083ee0ad03f4c5e02a1e.tar.bz2
forums-ea46feb11542a9cf54ce083ee0ad03f4c5e02a1e.tar.xz
forums-ea46feb11542a9cf54ce083ee0ad03f4c5e02a1e.zip
[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/<ext>/adm/style/ and regular templates go into ext/<ext>/styles/<style>/template/. Extension templates override regular templates. So if an extension supplies a file with a name used in phpBB, the extension's file will be used. A side-effect of this commit: Locator and Provider are now able to deal with arbitrary levels of template inheritance. So we can expose this through phpbb_template if we choose to, and allow styles to inherit from inherited styles. PHPBB3-10323
Diffstat (limited to 'phpBB/includes/template/locator.php')
-rw-r--r--phpBB/includes/template/locator.php97
1 files changed, 50 insertions, 47 deletions
diff --git a/phpBB/includes/template/locator.php b/phpBB/includes/template/locator.php
index 35e33bb9e6..44cd2cd7b9 100644
--- a/phpBB/includes/template/locator.php
+++ b/phpBB/includes/template/locator.php
@@ -28,62 +28,70 @@ if (!defined('IN_PHPBB'))
class phpbb_template_locator
{
/**
- * @var string Path to directory that templates are stored in.
+ * Paths to directories that templates are stored in.
+ * @var array
*/
- private $root = '';
+ private $roots = array();
/**
- * @var string Path to parent/fallback template directory.
+ * Index of the main template in the roots array
+ * @var int
*/
- private $inherit_root = '';
+ private $main_root_id = 0;
/**
- * @var array Map from handles to source template file paths.
+ * Map from root index to handles to source template file paths.
* Normally it only contains paths for handles that are used
* (or are likely to be used) by the page being rendered and not
* all templates that exist on the filesystem.
+ * @var array
*/
private $files = array();
/**
- * @var array Map from handles to source template file names.
+ * Map from handles to source template file names.
* Covers the same data as $files property but maps to basenames
* instead of paths.
+ * @var array
*/
private $filenames = array();
/**
- * @var array Map from handles to parent/fallback source template
- * file paths. Covers the same data as $files.
+ * Set main template location (must have been added through set_paths first).
+ *
+ * @param string $template_path Path to template directory
+ * @return null
*/
- private $files_inherit = array();
+ public function set_main_template($template)
+ {
+ $this->main_root_id = array_search($template, $this->roots, true);
+ }
/**
- * Set custom template location (able to use directory outside of phpBB).
+ * Sets the list of template paths
*
- * Note: Templates are still compiled to phpBB's cache directory.
+ * These paths will be searched for template files in the provided order.
+ * Paths may be outside of phpBB, but will still be cached to phpBB cache
+ * directory.
*
- * @param string $template_path Path to template directory
- * @param string|bool $fallback_template_path Path to fallback template, or false to disable fallback
+ * @param array $template_paths An array of paths to template directories
+ * @return null
*/
- public function set_custom_template($template_path, $fallback_template_path = false)
+ public function set_paths($template_paths)
{
- // 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->roots = array();
+ $this->files = array();
+ $this->filenames = array();
+ $this->main_root_id = 0;
- if ($fallback_template_path !== false)
+ foreach ($template_paths as $path)
{
- if (substr($fallback_template_path, -1) == '/')
+ // Make sure $path has no ending slash
+ if (substr($path, -1) === '/')
{
- $fallback_template_path = substr($fallback_template_path, 0, -1);
+ $path = substr($path, 0, -1);
}
-
- $this->inherit_root = $fallback_template_path;
+ $this->roots[] = $path;
}
}
@@ -103,11 +111,10 @@ class phpbb_template_locator
}
$this->filename[$handle] = $filename;
- $this->files[$handle] = $this->root . '/' . $filename;
- if ($this->inherit_root)
+ foreach ($this->roots as $root_index => $root)
{
- $this->files_inherit[$handle] = $this->inherit_root . '/' . $filename;
+ $this->files[$root_index][$handle] = $root . '/' . $filename;
}
}
}
@@ -154,12 +161,12 @@ class phpbb_template_locator
public function get_virtual_source_file_for_handle($handle)
{
// If we don't have a file assigned to this handle, die.
- if (!isset($this->files[$handle]))
+ if (!isset($this->files[$this->main_root_id][$handle]))
{
trigger_error("template locator: No file specified for handle $handle", E_USER_ERROR);
}
- $source_file = $this->files[$handle];
+ $source_file = $this->files[$this->main_root_id][$handle];
return $source_file;
}
@@ -182,30 +189,26 @@ class phpbb_template_locator
public function get_source_file_for_handle($handle)
{
// If we don't have a file assigned to this handle, die.
- if (!isset($this->files[$handle]))
+ if (!isset($this->files[$this->main_root_id][$handle]))
{
trigger_error("template locator: No file specified for handle $handle", E_USER_ERROR);
}
- $source_file = $this->files[$handle];
+ // locate a source file that exists
+ $source_file = $this->files[0][$handle];
+ $tried = $source_file;
+ for ($i = 1, $n = count($this->roots); $i < $n && !file_exists($source_file); $i++)
+ {
+ $source_file = $this->files[$i][$handle];
+ $tried .= ', ' . $source_file;
+ }
- // Try and open template for reading
+ // search failed
if (!file_exists($source_file))
{
- if (isset($this->files_inherit[$handle]) && $this->files_inherit[$handle])
- {
- $parent_source_file = $this->files_inherit[$handle];
- if (!file_exists($parent_source_file))
- {
- trigger_error("template locator: Neither $source_file nor $parent_source_file exist", E_USER_ERROR);
- }
- $source_file = $parent_source_file;
- }
- else
- {
- trigger_error("template locator: File $source_file does not exist", E_USER_ERROR);
- }
+ trigger_error("template locator: File for handle $handle does not exist. Could not find: $tried", E_USER_ERROR);
}
+
return $source_file;
}
}