From 3dbab9bedc02d0850dafdce9c13ae01a3a298b89 Mon Sep 17 00:00:00 2001 From: mohrt Date: Wed, 26 Jun 2002 14:51:12 +0000 Subject: [PATCH] allow plugins_dir to be an array of directories --- NEWS | 2 + Smarty.class.php | 61 +++++++++++++--------- Smarty_Compiler.class.php | 16 +----- libs/Smarty.class.php | 61 +++++++++++++--------- libs/Smarty_Compiler.class.php | 16 +----- libs/plugins/function.html_select_date.php | 4 +- libs/plugins/function.html_select_time.php | 4 +- libs/plugins/modifier.date_format.php | 2 +- plugins/function.html_select_date.php | 4 +- plugins/function.html_select_time.php | 4 +- plugins/modifier.date_format.php | 2 +- 11 files changed, 90 insertions(+), 86 deletions(-) diff --git a/NEWS b/NEWS index 87c51a41..d7422be1 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,5 @@ + - allow $plugins_dir to be an array of directories + (Andreas Kossmeier, Monte) - move debug.tpl to SMARTY_DIR, add to constructor (Monte) - fixed warning message in function.assign_debug_info (Monte) - fixed $template_dir, $compile_dir, $cache_dir, $config_dir diff --git a/Smarty.class.php b/Smarty.class.php index 61219ad3..8fcb45b5 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -67,8 +67,7 @@ class Smarty var $template_dir = 'templates'; // name of directory for templates var $compile_dir = 'templates_c'; // name of directory for compiled templates var $config_dir = 'configs'; // directory where config files are located - var $plugins_dir = 'plugins'; // directory where plugins are kept - // (relative to Smarty directory) + var $plugins_dir = array('plugins'); // plugin directories var $debugging = false; // enable debugging console true/false var $debug_tpl = ''; // path to debug console template @@ -1660,7 +1659,36 @@ function _run_insert_handler($args) return true; } + +/*======================================================================*\ + Function: _get_plugin_filepath + Purpose: get filepath of requested plugin +\*======================================================================*/ + function _get_plugin_filepath($type, $name) + { + $_plugin_filename = "$type.$name.php"; + + foreach ((array)$this->plugins_dir as $_plugin_dir) { + $_plugin_filepath = $_plugin_dir . DIR_SEP . $_plugin_filename; + + if (!preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $_plugin_dir)) { + // relative path + $_plugin_filepath = SMARTY_DIR . $_plugin_filepath; + } + + if (@is_readable($_plugin_filepath)) { + return $_plugin_filepath; + } + + // didn't find it try include path + if ($this->_get_include_path($_plugin_dir . DIR_SEP . $_plugin_filename, $_include_filepath)) { + return $_include_filepath; + } + } + + return false; + } /*======================================================================*\ Function: _load_plugins @@ -1668,6 +1696,7 @@ function _run_insert_handler($args) \*======================================================================*/ function _load_plugins($plugins) { + foreach ($plugins as $plugin_info) { list($type, $name, $tpl_file, $tpl_line, $delayed_loading) = $plugin_info; $plugin = &$this->_plugins[$type][$name]; @@ -1705,18 +1734,10 @@ function _run_insert_handler($args) } } - $plugin_file = SMARTY_DIR . - $this->plugins_dir . - DIR_SEP . - $type . - '.' . - $name . - '.php'; + $plugin_file = $this->_get_plugin_filepath($type, $name); - $found = true; - if (!file_exists($plugin_file) || !is_readable($plugin_file)) { - $message = "could not load plugin file $plugin_file\n"; - $found = false; + if ($found = ($plugin_file != false)) { + $message = "could not load plugin file '$type.$name.php'\n"; } /* @@ -1811,18 +1832,10 @@ function _run_insert_handler($args) return; } - $plugin_file = SMARTY_DIR . - $this->plugins_dir . DIR_SEP . - 'resource.' . - $type . - '.php'; + $plugin_file = $this->_get_plugin_filepath('resource', $type); + $found = ($plugin_file != false); - $found = true; - if (!file_exists($plugin_file) || !is_readable($plugin_file)) { - $this->_trigger_plugin_error("could not load plugin file $plugin_file"); - $found = false; - } else { - /* + if ($found) { /* * If the plugin file is found, it -must- provide the properly named * plugin functions. */ diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index ae16cba6..d2bba055 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -348,12 +348,6 @@ class Smarty_Compiler extends Smarty { $found = false; $have_function = true; - $plugin_file = SMARTY_DIR . - $this->plugins_dir . DIR_SEP . - 'compiler.' . - $tag_command . - '.php'; - /* * First we check if the compiler function has already been registered * or loaded from a plugin file. @@ -370,7 +364,7 @@ class Smarty_Compiler extends Smarty { * Otherwise we need to load plugin file and look for the function * inside it. */ - else if (file_exists($plugin_file) && is_readable($plugin_file)) { + else if ($plugin_file = $this->_get_plugin_filepath('compiler', $tag_command)) { $found = true; include_once $plugin_file; @@ -418,12 +412,6 @@ class Smarty_Compiler extends Smarty { $found = false; $have_function = true; - $plugin_file = SMARTY_DIR . - $this->plugins_dir . DIR_SEP . - 'block.' . - $tag_command . - '.php'; - /* * First we check if the block function has already been registered * or loaded from a plugin file. @@ -440,7 +428,7 @@ class Smarty_Compiler extends Smarty { * Otherwise we need to load plugin file and look for the function * inside it. */ - else if (file_exists($plugin_file) && is_readable($plugin_file)) { + else if ($plugin_file = $this->_get_plugin_filepath('block', $tag_command)) { $found = true; include_once $plugin_file; diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 61219ad3..8fcb45b5 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -67,8 +67,7 @@ class Smarty var $template_dir = 'templates'; // name of directory for templates var $compile_dir = 'templates_c'; // name of directory for compiled templates var $config_dir = 'configs'; // directory where config files are located - var $plugins_dir = 'plugins'; // directory where plugins are kept - // (relative to Smarty directory) + var $plugins_dir = array('plugins'); // plugin directories var $debugging = false; // enable debugging console true/false var $debug_tpl = ''; // path to debug console template @@ -1660,7 +1659,36 @@ function _run_insert_handler($args) return true; } + +/*======================================================================*\ + Function: _get_plugin_filepath + Purpose: get filepath of requested plugin +\*======================================================================*/ + function _get_plugin_filepath($type, $name) + { + $_plugin_filename = "$type.$name.php"; + + foreach ((array)$this->plugins_dir as $_plugin_dir) { + $_plugin_filepath = $_plugin_dir . DIR_SEP . $_plugin_filename; + + if (!preg_match("/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/", $_plugin_dir)) { + // relative path + $_plugin_filepath = SMARTY_DIR . $_plugin_filepath; + } + + if (@is_readable($_plugin_filepath)) { + return $_plugin_filepath; + } + + // didn't find it try include path + if ($this->_get_include_path($_plugin_dir . DIR_SEP . $_plugin_filename, $_include_filepath)) { + return $_include_filepath; + } + } + + return false; + } /*======================================================================*\ Function: _load_plugins @@ -1668,6 +1696,7 @@ function _run_insert_handler($args) \*======================================================================*/ function _load_plugins($plugins) { + foreach ($plugins as $plugin_info) { list($type, $name, $tpl_file, $tpl_line, $delayed_loading) = $plugin_info; $plugin = &$this->_plugins[$type][$name]; @@ -1705,18 +1734,10 @@ function _run_insert_handler($args) } } - $plugin_file = SMARTY_DIR . - $this->plugins_dir . - DIR_SEP . - $type . - '.' . - $name . - '.php'; + $plugin_file = $this->_get_plugin_filepath($type, $name); - $found = true; - if (!file_exists($plugin_file) || !is_readable($plugin_file)) { - $message = "could not load plugin file $plugin_file\n"; - $found = false; + if ($found = ($plugin_file != false)) { + $message = "could not load plugin file '$type.$name.php'\n"; } /* @@ -1811,18 +1832,10 @@ function _run_insert_handler($args) return; } - $plugin_file = SMARTY_DIR . - $this->plugins_dir . DIR_SEP . - 'resource.' . - $type . - '.php'; + $plugin_file = $this->_get_plugin_filepath('resource', $type); + $found = ($plugin_file != false); - $found = true; - if (!file_exists($plugin_file) || !is_readable($plugin_file)) { - $this->_trigger_plugin_error("could not load plugin file $plugin_file"); - $found = false; - } else { - /* + if ($found) { /* * If the plugin file is found, it -must- provide the properly named * plugin functions. */ diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index ae16cba6..d2bba055 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -348,12 +348,6 @@ class Smarty_Compiler extends Smarty { $found = false; $have_function = true; - $plugin_file = SMARTY_DIR . - $this->plugins_dir . DIR_SEP . - 'compiler.' . - $tag_command . - '.php'; - /* * First we check if the compiler function has already been registered * or loaded from a plugin file. @@ -370,7 +364,7 @@ class Smarty_Compiler extends Smarty { * Otherwise we need to load plugin file and look for the function * inside it. */ - else if (file_exists($plugin_file) && is_readable($plugin_file)) { + else if ($plugin_file = $this->_get_plugin_filepath('compiler', $tag_command)) { $found = true; include_once $plugin_file; @@ -418,12 +412,6 @@ class Smarty_Compiler extends Smarty { $found = false; $have_function = true; - $plugin_file = SMARTY_DIR . - $this->plugins_dir . DIR_SEP . - 'block.' . - $tag_command . - '.php'; - /* * First we check if the block function has already been registered * or loaded from a plugin file. @@ -440,7 +428,7 @@ class Smarty_Compiler extends Smarty { * Otherwise we need to load plugin file and look for the function * inside it. */ - else if (file_exists($plugin_file) && is_readable($plugin_file)) { + else if ($plugin_file = $this->_get_plugin_filepath('block', $tag_command)) { $found = true; include_once $plugin_file; diff --git a/libs/plugins/function.html_select_date.php b/libs/plugins/function.html_select_date.php index 54ab05e2..f5b77658 100644 --- a/libs/plugins/function.html_select_date.php +++ b/libs/plugins/function.html_select_date.php @@ -17,8 +17,8 @@ * month values (Gary Loescher) * ------------------------------------------------------------- */ -require_once SMARTY_DIR . $this->plugins_dir . '/shared.make_timestamp.php'; -require_once SMARTY_DIR . $this->plugins_dir . '/function.html_options.php'; +require_once $this->_get_plugin_filepath('shared','make_timestamp'); +require_once $this->_get_plugin_filepath('function','html_options'); function smarty_function_html_select_date($params, &$smarty) { /* Default values. */ diff --git a/libs/plugins/function.html_select_time.php b/libs/plugins/function.html_select_time.php index 02174725..12996ff6 100644 --- a/libs/plugins/function.html_select_time.php +++ b/libs/plugins/function.html_select_time.php @@ -8,8 +8,8 @@ * Purpose: Prints the dropdowns for time selection * ------------------------------------------------------------- */ -require_once SMARTY_DIR . $this->plugins_dir . '/shared.make_timestamp.php'; -require_once SMARTY_DIR . $this->plugins_dir . '/function.html_options.php'; +require_once $this->_get_plugin_filepath('shared','make_timestamp'); +require_once $this->_get_plugin_filepath('function','html_options'); function smarty_function_html_select_time($params, &$smarty) { /* Default values. */ diff --git a/libs/plugins/modifier.date_format.php b/libs/plugins/modifier.date_format.php index d9069727..aa288b60 100644 --- a/libs/plugins/modifier.date_format.php +++ b/libs/plugins/modifier.date_format.php @@ -11,7 +11,7 @@ * default_date: default date if $string is empty * ------------------------------------------------------------- */ -require_once SMARTY_DIR . $this->plugins_dir . '/shared.make_timestamp.php'; +require_once $this->_get_plugin_filepath('shared','make_timestamp'); function smarty_modifier_date_format($string, $format="%b %e, %Y", $default_date=null) { if($string != '') { diff --git a/plugins/function.html_select_date.php b/plugins/function.html_select_date.php index 54ab05e2..f5b77658 100644 --- a/plugins/function.html_select_date.php +++ b/plugins/function.html_select_date.php @@ -17,8 +17,8 @@ * month values (Gary Loescher) * ------------------------------------------------------------- */ -require_once SMARTY_DIR . $this->plugins_dir . '/shared.make_timestamp.php'; -require_once SMARTY_DIR . $this->plugins_dir . '/function.html_options.php'; +require_once $this->_get_plugin_filepath('shared','make_timestamp'); +require_once $this->_get_plugin_filepath('function','html_options'); function smarty_function_html_select_date($params, &$smarty) { /* Default values. */ diff --git a/plugins/function.html_select_time.php b/plugins/function.html_select_time.php index 02174725..12996ff6 100644 --- a/plugins/function.html_select_time.php +++ b/plugins/function.html_select_time.php @@ -8,8 +8,8 @@ * Purpose: Prints the dropdowns for time selection * ------------------------------------------------------------- */ -require_once SMARTY_DIR . $this->plugins_dir . '/shared.make_timestamp.php'; -require_once SMARTY_DIR . $this->plugins_dir . '/function.html_options.php'; +require_once $this->_get_plugin_filepath('shared','make_timestamp'); +require_once $this->_get_plugin_filepath('function','html_options'); function smarty_function_html_select_time($params, &$smarty) { /* Default values. */ diff --git a/plugins/modifier.date_format.php b/plugins/modifier.date_format.php index d9069727..aa288b60 100644 --- a/plugins/modifier.date_format.php +++ b/plugins/modifier.date_format.php @@ -11,7 +11,7 @@ * default_date: default date if $string is empty * ------------------------------------------------------------- */ -require_once SMARTY_DIR . $this->plugins_dir . '/shared.make_timestamp.php'; +require_once $this->_get_plugin_filepath('shared','make_timestamp'); function smarty_modifier_date_format($string, $format="%b %e, %Y", $default_date=null) { if($string != '') {