From e1850388348d4bfdf463a5aa341bc470da79cf32 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Tue, 4 Dec 2018 00:26:50 +0100 Subject: namespacing: \Shaarli\Plugin\PluginManager Signed-off-by: VirtualTam --- application/PluginManager.php | 244 --------------------- application/plugin/PluginManager.php | 233 ++++++++++++++++++++ .../exception/PluginFileNotFoundException.php | 23 ++ composer.json | 2 + index.php | 2 +- plugins/archiveorg/archiveorg.php | 2 + plugins/demo_plugin/demo_plugin.php | 1 + plugins/isso/isso.php | 1 + plugins/markdown/markdown.php | 1 + plugins/piwik/piwik.php | 2 + plugins/playvideos/playvideos.php | 1 + plugins/pubsubhubbub/pubsubhubbub.php | 1 + plugins/qrcode/qrcode.php | 1 + plugins/wallabag/wallabag.php | 1 + tests/PluginManagerTest.php | 10 +- tests/plugins/PluginAddlinkTest.php | 9 +- tests/plugins/PluginArchiveorgTest.php | 2 + tests/plugins/PluginIssoTest.php | 1 + tests/plugins/PluginMarkdownTest.php | 1 + tests/plugins/PluginPlayvideosTest.php | 1 + tests/plugins/PluginPubsubhubbubTest.php | 1 + tests/plugins/PluginQrcodeTest.php | 1 + tests/plugins/PluginWallabagTest.php | 3 +- 23 files changed, 284 insertions(+), 260 deletions(-) delete mode 100644 application/PluginManager.php create mode 100644 application/plugin/PluginManager.php create mode 100644 application/plugin/exception/PluginFileNotFoundException.php diff --git a/application/PluginManager.php b/application/PluginManager.php deleted file mode 100644 index 1ed4db4b..00000000 --- a/application/PluginManager.php +++ /dev/null @@ -1,244 +0,0 @@ -conf = $conf; - $this->errors = array(); - } - - /** - * Load plugins listed in $authorizedPlugins. - * - * @param array $authorizedPlugins Names of plugin authorized to be loaded. - * - * @return void - */ - public function load($authorizedPlugins) - { - $this->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, $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. - * @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); - } - - $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; - } - - /** - * 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; - } - - /** - * 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; - } -} - -/** - * 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 = sprintf(t('Plugin "%s" files not found.'), $pluginName); - } -} diff --git a/application/plugin/PluginManager.php b/application/plugin/PluginManager.php new file mode 100644 index 00000000..f7b24a8e --- /dev/null +++ b/application/plugin/PluginManager.php @@ -0,0 +1,233 @@ +conf = $conf; + $this->errors = array(); + } + + /** + * Load plugins listed in $authorizedPlugins. + * + * @param array $authorizedPlugins Names of plugin authorized to be loaded. + * + * @return void + */ + public function load($authorizedPlugins) + { + $this->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, $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. + * @param string $pluginName plugin's name. + * + * @return void + * @throws \Shaarli\Plugin\Exception\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); + } + + $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; + } + + /** + * 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; + } + + /** + * 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; + } +} diff --git a/application/plugin/exception/PluginFileNotFoundException.php b/application/plugin/exception/PluginFileNotFoundException.php new file mode 100644 index 00000000..e5386f02 --- /dev/null +++ b/application/plugin/exception/PluginFileNotFoundException.php @@ -0,0 +1,23 @@ +message = sprintf(t('Plugin "%s" files not found.'), $pluginName); + } +} diff --git a/composer.json b/composer.json index c1f47317..a2df466a 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,8 @@ "Shaarli\\Feed\\": "application/feed", "Shaarli\\Http\\": "application/http", "Shaarli\\Netscape\\": "application/netscape", + "Shaarli\\Plugin\\": "application/plugin", + "Shaarli\\Plugin\\Exception\\": "application/plugin/exception", "Shaarli\\Render\\": "application/render", "Shaarli\\Security\\": "application/security", "Shaarli\\Updater\\": "application/updater", diff --git a/index.php b/index.php index bff8e8ff..633ab89e 100644 --- a/index.php +++ b/index.php @@ -65,7 +65,6 @@ require_once 'application/updater/UpdaterUtils.php'; require_once 'application/FileUtils.php'; require_once 'application/TimeZone.php'; require_once 'application/Utils.php'; -require_once 'application/PluginManager.php'; use \Shaarli\ApplicationUtils; use \Shaarli\Bookmark\Exception\LinkNotFoundException; @@ -76,6 +75,7 @@ use \Shaarli\Feed\FeedBuilder; use \Shaarli\History; use \Shaarli\Languages; use \Shaarli\Netscape\NetscapeBookmarkUtils; +use \Shaarli\Plugin\PluginManager; use \Shaarli\Render\PageBuilder; use \Shaarli\Render\ThemeUtils; use \Shaarli\Router; diff --git a/plugins/archiveorg/archiveorg.php b/plugins/archiveorg/archiveorg.php index 5dcea5a6..0ee1c73c 100644 --- a/plugins/archiveorg/archiveorg.php +++ b/plugins/archiveorg/archiveorg.php @@ -5,6 +5,8 @@ * Add an icon in the link list for archive.org. */ +use Shaarli\Plugin\PluginManager; + /** * Add archive.org icon to link_plugin when rendering linklist. * diff --git a/plugins/demo_plugin/demo_plugin.php b/plugins/demo_plugin/demo_plugin.php index 94ce38f8..95ea7fe2 100644 --- a/plugins/demo_plugin/demo_plugin.php +++ b/plugins/demo_plugin/demo_plugin.php @@ -15,6 +15,7 @@ */ use Shaarli\Config\ConfigManager; +use Shaarli\Plugin\PluginManager; use Shaarli\Router; /** diff --git a/plugins/isso/isso.php b/plugins/isso/isso.php index 9bdd5909..dab75dd5 100644 --- a/plugins/isso/isso.php +++ b/plugins/isso/isso.php @@ -5,6 +5,7 @@ */ use Shaarli\Config\ConfigManager; +use Shaarli\Plugin\PluginManager; use Shaarli\Router; /** diff --git a/plugins/markdown/markdown.php b/plugins/markdown/markdown.php index 9928a488..628970d6 100644 --- a/plugins/markdown/markdown.php +++ b/plugins/markdown/markdown.php @@ -7,6 +7,7 @@ */ use Shaarli\Config\ConfigManager; +use Shaarli\Plugin\PluginManager; use Shaarli\Router; /* diff --git a/plugins/piwik/piwik.php b/plugins/piwik/piwik.php index ca00c2be..17b1aecc 100644 --- a/plugins/piwik/piwik.php +++ b/plugins/piwik/piwik.php @@ -4,6 +4,8 @@ * Adds tracking code on each page. */ +use Shaarli\Plugin\PluginManager; + /** * Initialization function. * It will be called when the plugin is loaded. diff --git a/plugins/playvideos/playvideos.php b/plugins/playvideos/playvideos.php index bb5b9e98..0341ed59 100644 --- a/plugins/playvideos/playvideos.php +++ b/plugins/playvideos/playvideos.php @@ -6,6 +6,7 @@ * Note: this plugin adds jQuery. */ +use Shaarli\Plugin\PluginManager; use Shaarli\Router; /** diff --git a/plugins/pubsubhubbub/pubsubhubbub.php b/plugins/pubsubhubbub/pubsubhubbub.php index a7bd34c1..2878c050 100644 --- a/plugins/pubsubhubbub/pubsubhubbub.php +++ b/plugins/pubsubhubbub/pubsubhubbub.php @@ -12,6 +12,7 @@ use pubsubhubbub\publisher\Publisher; use Shaarli\Config\ConfigManager; use Shaarli\Feed\FeedBuilder; +use Shaarli\Plugin\PluginManager; use Shaarli\Router; /** diff --git a/plugins/qrcode/qrcode.php b/plugins/qrcode/qrcode.php index 21908cee..34eef8be 100644 --- a/plugins/qrcode/qrcode.php +++ b/plugins/qrcode/qrcode.php @@ -5,6 +5,7 @@ * Display a QRCode icon in link list. */ +use Shaarli\Plugin\PluginManager; use Shaarli\Router; /** diff --git a/plugins/wallabag/wallabag.php b/plugins/wallabag/wallabag.php index a6476c71..5ba1611d 100644 --- a/plugins/wallabag/wallabag.php +++ b/plugins/wallabag/wallabag.php @@ -6,6 +6,7 @@ require_once 'WallabagInstance.php'; use Shaarli\Config\ConfigManager; +use Shaarli\Plugin\PluginManager; /** * Init function, return an error if the server is not set. diff --git a/tests/PluginManagerTest.php b/tests/PluginManagerTest.php index 01de959c..71761ac1 100644 --- a/tests/PluginManagerTest.php +++ b/tests/PluginManagerTest.php @@ -1,16 +1,12 @@