X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2FPluginManager.php;h=cf6038453e499e7e4030b68b1fffd13bfadbb79b;hb=4fa9a3c5d83a1024678596a586afe5df14a345b5;hp=e572ff7cc63df8bb8b42895904601b891811ed6a;hpb=6fc14d530369740d27d6bd641369d4f5f5f04080;p=github%2Fshaarli%2FShaarli.git diff --git a/application/PluginManager.php b/application/PluginManager.php index e572ff7c..cf603845 100644 --- a/application/PluginManager.php +++ b/application/PluginManager.php @@ -4,17 +4,9 @@ * 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 @@ -28,39 +20,36 @@ class PluginManager 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. + * @var array List of plugin errors. */ - private function __construct() - { - } + protected $errors; /** - * Cloning isn't allowed either. - * - * @return void + * Plugins subdirectory. + * @var string $PLUGINS_PATH */ - private function __clone() - { - } + public static $PLUGINS_PATH = 'plugins'; + + /** + * Plugins meta files extension. + * @var string $META_EXT + */ + 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(); } /** @@ -96,10 +85,10 @@ class PluginManager /** * 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 */ public function executeHooks($hook, &$data, $params = array()) @@ -116,13 +105,14 @@ class PluginManager $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. @@ -142,8 +132,17 @@ class PluginManager 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; } @@ -162,6 +161,68 @@ class PluginManager { 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); + + 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']); + } 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'] = 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; + } } /** @@ -179,6 +240,6 @@ class PluginFileNotFoundException extends Exception */ 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 +}