* Class PluginManager
*
* Use to manage, load and execute plugins.
- *
- * Using Singleton design pattern.
*/
class PluginManager
{
- /**
- * PluginManager singleton instance.
- * @var PluginManager $instance
- */
- private static $instance;
-
/**
* List of authorized plugins from configuration file.
* @var array $authorizedPlugins
private $loadedPlugins = array();
/**
- * Plugins subdirectory.
- * @var string $PLUGINS_PATH
+ * @var ConfigManager Configuration Manager instance.
*/
- public static $PLUGINS_PATH = 'plugins';
+ protected $conf;
/**
- * Private constructor: new instances not allowed.
+ * Plugins subdirectory.
+ * @var string $PLUGINS_PATH
*/
- private function __construct()
- {
- }
+ public static $PLUGINS_PATH = 'plugins';
/**
- * Cloning isn't allowed either.
- *
- * @return void
+ * Plugins meta files extension.
+ * @var string $META_EXT
*/
- private function __clone()
- {
- }
+ public static $META_EXT = 'meta';
/**
- * Return existing instance of PluginManager, or create it.
+ * Constructor.
*
- * @return PluginManager instance.
+ * @param ConfigManager $conf Configuration Manager instance.
*/
- public static function getInstance()
+ public function __construct(&$conf)
{
- if (!(self::$instance instanceof self)) {
- self::$instance = new self();
- }
-
- return self::$instance;
+ $this->conf = $conf;
}
/**
/**
* Execute all plugins registered hook.
*
- * @param string $hook name of the hook to trigger.
- * @param array $data list of data to manipulate passed by reference.
- * @param array $params additional parameters such as page target.
+ * @param string $hook name of the hook to trigger.
+ * @param array $data list of data to manipulate passed by reference.
+ * @param array $params additional parameters such as page target.
*
* @return void
*/
$hookFunction = $this->buildHookName($hook, $plugin);
if (function_exists($hookFunction)) {
- $data = call_user_func($hookFunction, $data);
+ $data = call_user_func($hookFunction, $data, $this->conf);
}
}
}
throw new PluginFileNotFoundException($pluginName);
}
+ $conf = $this->conf;
include_once $pluginFilePath;
$this->loadedPlugins[] = $pluginName;
{
return 'hook_' . $pluginName . '_' . $hook;
}
+
+ /**
+ * Retrieve plugins metadata from *.meta (INI) files into an array.
+ * Metadata contains:
+ * - plugin description [description]
+ * - parameters split with ';' [parameters]
+ *
+ * Respects plugins order from settings.
+ *
+ * @return array plugins metadata.
+ */
+ public function getPluginsMeta()
+ {
+ $metaData = array();
+ $dirs = glob(self::$PLUGINS_PATH . '/*', GLOB_ONLYDIR | GLOB_MARK);
+
+ // Browse all plugin directories.
+ foreach ($dirs as $pluginDir) {
+ $plugin = basename($pluginDir);
+ $metaFile = $pluginDir . $plugin . '.' . self::$META_EXT;
+ if (!is_file($metaFile) || !is_readable($metaFile)) {
+ continue;
+ }
+
+ $metaData[$plugin] = parse_ini_file($metaFile);
+ $metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins);
+
+ // Read parameters and format them into an array.
+ if (isset($metaData[$plugin]['parameters'])) {
+ $params = explode(';', $metaData[$plugin]['parameters']);
+ } else {
+ $params = array();
+ }
+ $metaData[$plugin]['parameters'] = array();
+ foreach ($params as $param) {
+ if (empty($param)) {
+ continue;
+ }
+
+ $metaData[$plugin]['parameters'][$param]['value'] = '';
+ // Optional parameter description in parameter.PARAM_NAME=
+ if (isset($metaData[$plugin]['parameter.'. $param])) {
+ $metaData[$plugin]['parameters'][$param]['desc'] = $metaData[$plugin]['parameter.'. $param];
+ }
+ }
+ }
+
+ return $metaData;
+ }
}
/**
{
$this->message = 'Plugin "'. $pluginName .'" files not found.';
}
-}
\ No newline at end of file
+}