From 6fc14d530369740d27d6bd641369d4f5f5f04080 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 15 Jul 2015 11:42:15 +0200 Subject: Plugin system - CORE see shaarli/Shaarli#275 --- application/PluginManager.php | 184 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 application/PluginManager.php (limited to 'application/PluginManager.php') diff --git a/application/PluginManager.php b/application/PluginManager.php new file mode 100644 index 00000000..e572ff7c --- /dev/null +++ b/application/PluginManager.php @@ -0,0 +1,184 @@ +authorizedPlugins = $authorizedPlugins; + + $dirs = glob(self::$PLUGINS_PATH . '/*', GLOB_ONLYDIR); + $dirnames = array_map('basename', $dirs); + foreach ($this->authorizedPlugins as $plugin) { + $index = array_search($plugin, $dirnames); + + // plugin authorized, but its folder isn't listed + if ($index === false) { + continue; + } + + try { + $this->loadPlugin($dirs[$index], $plugin); + } + catch (PluginFileNotFoundException $e) { + error_log($e->getMessage()); + } + } + } + + /** + * 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. + * + * @return void + */ + public function executeHooks($hook, &$data, $params = array()) + { + if (!empty($params['target'])) { + $data['_PAGE_'] = $params['target']; + } + + if (isset($params['loggedin'])) { + $data['_LOGGEDIN_'] = $params['loggedin']; + } + + foreach ($this->loadedPlugins as $plugin) { + $hookFunction = $this->buildHookName($hook, $plugin); + + if (function_exists($hookFunction)) { + $data = call_user_func($hookFunction, $data); + } + } + } + + /** + * Load a single plugin from its files. + * Add them in $loadedPlugins if successful. + * + * @param string $dir plugin's directory. + * @param string $pluginName plugin's name. + * + * @return void + * @throws PluginFileNotFoundException - plugin files not found. + */ + private function loadPlugin($dir, $pluginName) + { + if (!is_dir($dir)) { + throw new PluginFileNotFoundException($pluginName); + } + + $pluginFilePath = $dir . '/' . $pluginName . '.php'; + if (!is_file($pluginFilePath)) { + throw new PluginFileNotFoundException($pluginName); + } + + include_once $pluginFilePath; + + $this->loadedPlugins[] = $pluginName; + } + + /** + * Construct normalize hook name for a specific plugin. + * + * Format: + * hook__ + * + * @param string $hook hook name. + * @param string $pluginName plugin name. + * + * @return string - plugin's hook name. + */ + public function buildHookName($hook, $pluginName) + { + return 'hook_' . $pluginName . '_' . $hook; + } +} + +/** + * Class PluginFileNotFoundException + * + * Raise when plugin files can't be found. + */ +class PluginFileNotFoundException extends Exception +{ + /** + * Construct exception with plugin name. + * Generate message. + * + * @param string $pluginName name of the plugin not found + */ + public function __construct($pluginName) + { + $this->message = 'Plugin "'. $pluginName .'" files not found.'; + } +} \ No newline at end of file -- cgit v1.2.3