From 716c3a5a3a36206e93431eb5f8df3471546ec4b2 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 4 Sep 2011 20:42:32 -0400 Subject: start to add a plugin management framework --- index.php | 3 ++ plugin-conf.php | 5 +++ src/lib/ContactMethod.class.php | 17 +++----- src/lib/MessageManager.class.php | 9 ++++ src/lib/Plugin.class.php | 8 +++- src/lib/PluginManager.class.php | 90 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 plugin-conf.php create mode 100644 src/lib/PluginManager.class.php diff --git a/index.php b/index.php index 3a3a646..ceab3f3 100644 --- a/index.php +++ b/index.php @@ -41,6 +41,9 @@ require_once('Model.class.php'); require_once('Controller.class.php'); require_once('Router.class.php'); +require_once('ContactMethod.class.php'); +require('plugin-conf.php'); + global $mm; require_once('MessageManager.class.php'); $mm = new MessageManager(BASEPATH.'/conf.php'); diff --git a/plugin-conf.php b/plugin-conf.php new file mode 100644 index 0000000..7b397cc --- /dev/null +++ b/plugin-conf.php @@ -0,0 +1,5 @@ +$verb_slug = $verb_slug; - $this->$addr_slug = $addr_slug; - $this->$verb_text = $verb_text; - $this->$addr_Text = $addr_text; + $this->verb_slug = $verb_slug; + $this->addr_slug = $addr_slug; + $this->verb_text = $verb_text; + $this->addr_text = $addr_text; global $CONTACT_METHODS; - $CONTACT_METHODS[$slug] = $this; + $CONTACT_METHODS[$verb_slug] = $this; } public function setHandler($handler) { - $this->$handler = $handler; + $this->handler = $handler; } } - -new ContactMethod('sms' , 'phone' , - 'text message', 'cell number' ); -new ContactMethod('email' , 'email' , - 'email' , 'email address'); diff --git a/src/lib/MessageManager.class.php b/src/lib/MessageManager.class.php index d9d9fbc..cfcdac7 100644 --- a/src/lib/MessageManager.class.php +++ b/src/lib/MessageManager.class.php @@ -6,6 +6,7 @@ class MessageManager { private $db_prefix; private $pw_hasher; private $template; + private $pluginManager; private $base; private $users = array(); @@ -386,6 +387,14 @@ class MessageManager { } return $this->template; } + + public function pluginManager() { + if (!isset($this->pluginManager)) { + require_once('PluginManager.class.php'); + $this->pluginManager = new PluginManager(); + } + return $this->pluginManager; + } public function login($username, $password) { $uid = $this->getUID($username); diff --git a/src/lib/Plugin.class.php b/src/lib/Plugin.class.php index f2251d2..8c7fad8 100644 --- a/src/lib/Plugin.class.php +++ b/src/lib/Plugin.class.php @@ -11,6 +11,12 @@ abstract class Plugin { $this->config[$param]=$value; } } + + public function userConfig() { return array(); } + protected function addConfigGroup($arr, $group) { + if (!isset($arr[$group])) + $arr[$group] = array(); + } public abstract function init(); -} \ No newline at end of file +} diff --git a/src/lib/PluginManager.class.php b/src/lib/PluginManager.class.php new file mode 100644 index 0000000..22d7b0c --- /dev/null +++ b/src/lib/PluginManager.class.php @@ -0,0 +1,90 @@ + $type) { + $value = $mm->getPluginConf($plugin_name, $param); + if ($value!==false) { + switch ($type) { + case 'text': + case 'password': + $value = "$value"; + break; + case 'int': + $value = (int)$value; + break; + } + $obj->configSet($param, $value); + } + } + return $obj; + } + + /** + * Return an array of available plugin names. + */ + public function listPlugins() { + $plugins = array(); + + $dirs = explode(PATH_SEPARATOR, PLUGINPATH); + foreach ($dirs as $dir) { + // Find all files in $dir with the ext `.class.php' + $files = glob($dir.'/*.class.php'); + foreach ($files as $file) { + $plugins[] = preg_replace('@\.class\.php$@', '$1', basename($file)); + } + } + + return $plugins; + } + + /** + * Return an array of enabled plugin names. + */ + public function getActivePlugins() { + global $mm; + $string = $mm->getSysConf('plugins'); + return $mm->valueToArray($string); + } + + /** + * Set the enabled plugins. + */ + public function setActivePlugins($plugins) { + global $mm; + $string = $mm->arrayToValue($plugins); + return $mm->setSysConf('plugins', $string); + } + + /** + * Load the enabled plugins. + */ + public function loadPlugins() { + if ($this->loaded) return; + $plugin_names = $this->getActivePlugins(); + foreach ($plugin_names as $name) { + $this->plugins[$name] = $this->loadPlugin($name); + } + $this->loaded = true; + } + + public function callHook($hook, $arg=null) { + $this->loadPlugins(); + $ret = array(); + foreach ($this->plugins as $name => $plugin) { + $ret[$name] = call_user_func(array($plugin, $hook), + &$arg); + } + return $ret; + } +} -- cgit v1.2.3