diff options
author | Vjacheslav Trushkin <arty@phpbb.com> | 2012-04-01 09:52:55 +0300 |
---|---|---|
committer | Vjacheslav Trushkin <arty@phpbb.com> | 2012-04-01 09:52:55 +0300 |
commit | f80512f1066ebfc09d2a4ffac412c56a3fb247de (patch) | |
tree | b6e15ac28a9d5ac5f93f9bd7cfb3951cad44cf29 /phpBB | |
parent | 2ce73baeab35adc3515a04c1db38af62c98d5c93 (diff) | |
download | forums-f80512f1066ebfc09d2a4ffac412c56a3fb247de.tar forums-f80512f1066ebfc09d2a4ffac412c56a3fb247de.tar.gz forums-f80512f1066ebfc09d2a4ffac412c56a3fb247de.tar.bz2 forums-f80512f1066ebfc09d2a4ffac412c56a3fb247de.tar.xz forums-f80512f1066ebfc09d2a4ffac412c56a3fb247de.zip |
[ticket/10733] Adding functions to locate resources
Adding $style->locate() and $template->locate() functions
PHPBB3-10733
Diffstat (limited to 'phpBB')
-rw-r--r-- | phpBB/includes/style/resource_locator.php | 51 | ||||
-rw-r--r-- | phpBB/includes/style/style.php | 28 | ||||
-rw-r--r-- | phpBB/includes/style/template.php | 36 |
3 files changed, 115 insertions, 0 deletions
diff --git a/phpBB/includes/style/resource_locator.php b/phpBB/includes/style/resource_locator.php index 1b35752111..3e6dd5d6aa 100644 --- a/phpBB/includes/style/resource_locator.php +++ b/phpBB/includes/style/resource_locator.php @@ -237,4 +237,55 @@ class phpbb_style_resource_locator return ($find_all) ? $found_all : $source_file; } + + /** + * Locates source file path, accounting for styles tree and verifying that + * the path exists. + * + * Unlike previous functions, this function works without template handle + * and it can search for more than one file. If more than one file name is + * specified, it will return location of file that it finds first. + * + * @param array $files List of files to locate. + * @param bool $return_default Determines what to return if file does not + * exist. If true, function will return location where file is + * supposed to be. If false, function will return false. + * @param bool $return_full_path If true, function will return full path + * to file. If false, function will return file name. This + * parameter can be used to check which one of set of files + * is available. + * @return string or boolean Source file path if file exists or $return_default is + * true. False if file does not exist and $return_default is false + */ + public function get_first_file_location($files, $return_default = false, $return_full_path = true) + { + // set default value + $default_result = false; + + // check all available paths + foreach ($this->roots as $root_paths) + { + foreach ($root_paths as $path) + { + // check all files + foreach ($files as $filename) + { + $source_file = $path . '/' . $filename; + if (file_exists($source_file)) + { + return ($return_full_path) ? $source_file : $filename; + } + + // assign first file as result if $return_default is true + if ($return_default && $default_result === false) + { + $default_result = $source_file; + } + } + } + } + + // search failed + return $default_result; + } } diff --git a/phpBB/includes/style/style.php b/phpBB/includes/style/style.php index dc1e71dff6..5ac61c9f10 100644 --- a/phpBB/includes/style/style.php +++ b/phpBB/includes/style/style.php @@ -150,4 +150,32 @@ class phpbb_style { $this->provider->set_ext_dir_prefix($ext_dir_prefix); } + + /** + * Locates source file path, accounting for styles tree and verifying that + * the path exists. + * + * @param string or array $files List of files to locate. If there is only + * one file, $files can be a string to make code easier to read. + * @param bool $return_default Determines what to return if file does not + * exist. If true, function will return location where file is + * supposed to be. If false, function will return false. + * @param bool $return_full_path If true, function will return full path + * to file. If false, function will return file name. This + * parameter can be used to check which one of set of files + * is available. + * @return string or boolean Source file path if file exists or $return_default is + * true. False if file does not exist and $return_default is false + */ + public function locate($files, $return_default = false, $return_full_path = true) + { + // convert string to array + if (is_string($files)) + { + $files = array($files); + } + + // use resource locator to find files + return $this->locator->get_first_file_location($files, $return_default, $return_full_path); + } } diff --git a/phpBB/includes/style/template.php b/phpBB/includes/style/template.php index 21a2e821dd..aebf1da603 100644 --- a/phpBB/includes/style/template.php +++ b/phpBB/includes/style/template.php @@ -456,4 +456,40 @@ class phpbb_style_template } include($file); } + + /** + * Locates source template path, accounting for styles tree and verifying that + * the path exists. + * + * @param string or array $files List of templates to locate. If there is only + * one template, $files can be a string to make code easier to read. + * @param bool $return_default Determines what to return if template does not + * exist. If true, function will return location where template is + * supposed to be. If false, function will return false. + * @param bool $return_full_path If true, function will return full path + * to template. If false, function will return template file name. + * This parameter can be used to check which one of set of template + * files is available. + * @return string or boolean Source template path if template exists or $return_default is + * true. False if template does not exist and $return_default is false + */ + public function locate($files, $return_default = false, $return_full_path = true) + { + // add tempalte path prefix + $templates = array(); + if (is_string($files)) + { + $templates[] = $this->template_path . $files; + } + else + { + foreach ($files as $file) + { + $templates[] = $this->template_path . $file; + } + } + + // use resource locator to find files + return $this->locator->get_first_file_location($templates, $return_default, $return_full_path); + } } |