diff options
author | Luke Shumaker <LukeShu@sbcglobal.net> | 2011-09-04 20:42:32 -0400 |
---|---|---|
committer | Luke Shumaker <LukeShu@sbcglobal.net> | 2011-09-04 20:42:32 -0400 |
commit | 716c3a5a3a36206e93431eb5f8df3471546ec4b2 (patch) | |
tree | faccf06e355ce37f233f823a87b4a93d9f531cb2 /src/lib/PluginManager.class.php | |
parent | 103332a30f8976fcc224c8f55dc23aba7b99e578 (diff) |
start to add a plugin management framework
Diffstat (limited to 'src/lib/PluginManager.class.php')
-rw-r--r-- | src/lib/PluginManager.class.php | 90 |
1 files changed, 90 insertions, 0 deletions
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 @@ +<?php + +class PluginManager { + public $plugins = array(); + private $loaded = false; + /** + * Return an instance of the plugin with $plugin_name + */ + public function loadPlugin($plugin_name) { + global $mm; + + require_once("$plugin_name.class.php"); + $obj = new $plugin_name; + $params = call_user_func("$plugin_name::configList"); + foreach ($params as $param => $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; + } +} |