* 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;
/**
- * Plugins meta files extension.
- * @var string $META_EXT
+ * @var array List of plugin errors.
*/
- public static $META_EXT = 'meta';
+ protected $errors;
/**
- * 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;
+ $this->errors = array();
}
/**
/**
* 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);
}
}
}
/**
* Load a single plugin from its files.
+ * Call the init function if it exists, and collect errors.
* Add them in $loadedPlugins if successful.
*
* @param string $dir plugin's directory.
throw new PluginFileNotFoundException($pluginName);
}
+ $conf = $this->conf;
include_once $pluginFilePath;
+ $initFunction = $pluginName . '_init';
+ if (function_exists($initFunction)) {
+ $errors = call_user_func($initFunction, $this->conf);
+ if (!empty($errors)) {
+ $this->errors = array_merge($this->errors, $errors);
+ }
+ }
+
$this->loadedPlugins[] = $pluginName;
}
$metaData[$plugin] = parse_ini_file($metaFile);
$metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins);
+ if (isset($metaData[$plugin]['description'])) {
+ $metaData[$plugin]['description'] = t($metaData[$plugin]['description']);
+ }
// Read parameters and format them into an array.
if (isset($metaData[$plugin]['parameters'])) {
$params = explode(';', $metaData[$plugin]['parameters']);
continue;
}
- $metaData[$plugin]['parameters'][$param] = '';
+ $metaData[$plugin]['parameters'][$param]['value'] = '';
+ // Optional parameter description in parameter.PARAM_NAME=
+ if (isset($metaData[$plugin]['parameter.'. $param])) {
+ $metaData[$plugin]['parameters'][$param]['desc'] = t($metaData[$plugin]['parameter.'. $param]);
+ }
}
}
return $metaData;
}
+
+ /**
+ * Return the list of encountered errors.
+ *
+ * @return array List of errors (empty array if none exists).
+ */
+ public function getErrors()
+ {
+ return $this->errors;
+ }
}
/**
*/
public function __construct($pluginName)
{
- $this->message = 'Plugin "'. $pluginName .'" files not found.';
+ $this->message = sprintf(t('Plugin "%s" files not found.'), $pluginName);
}
-}
\ No newline at end of file
+}