X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2FPluginManager.php;h=dca7e63e571621461c72e64e3b42b0c2cd29fea2;hb=8562009682b788ce8fb9da0f312bd6cd5cc53b3a;hp=803f11b4b707000a6bdab1235ee6ab49655ac8db;hpb=fd006c630b64146edc402b68d8503c716f8a55d6;p=github%2Fshaarli%2FShaarli.git diff --git a/application/PluginManager.php b/application/PluginManager.php index 803f11b4..dca7e63e 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,30 @@ 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. + * 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; } /** @@ -96,9 +79,9 @@ 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 */ @@ -116,7 +99,7 @@ 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); } } } @@ -142,6 +125,7 @@ class PluginManager throw new PluginFileNotFoundException($pluginName); } + $conf = $this->conf; include_once $pluginFilePath; $this->loadedPlugins[] = $pluginName; @@ -162,6 +146,51 @@ 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); + + // 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] = ''; + } + } + + return $metaData; + } } /**