From 7fde6de1212323418401c15efba06026c704ca87 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 14 Oct 2016 13:22:58 +0200 Subject: New init function for plugins, supports errors reporting All plugins can optionally add an init function named `pluginname_init()` which is called when the plugin is loaded. This function is aware of the config, and can return initialization errors, which are displayed in the header template. Note that the previous error system hack no longer work. --- application/PageBuilder.php | 3 --- application/PluginManager.php | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) (limited to 'application') diff --git a/application/PageBuilder.php b/application/PageBuilder.php index 42932f32..32c7f9f1 100644 --- a/application/PageBuilder.php +++ b/application/PageBuilder.php @@ -77,9 +77,6 @@ class PageBuilder $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false)); $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', false)); $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); - if (!empty($GLOBALS['plugin_errors'])) { - $this->tpl->assign('plugin_errors', $GLOBALS['plugin_errors']); - } $this->tpl->assign('token', getToken($this->conf)); // To be removed with a proper theme configuration. $this->tpl->assign('conf', $this->conf); diff --git a/application/PluginManager.php b/application/PluginManager.php index 1e132a7f..59ece4fa 100644 --- a/application/PluginManager.php +++ b/application/PluginManager.php @@ -24,6 +24,11 @@ class PluginManager */ protected $conf; + /** + * @var array List of plugin errors. + */ + protected $errors; + /** * Plugins subdirectory. * @var string $PLUGINS_PATH @@ -44,6 +49,7 @@ class PluginManager public function __construct(&$conf) { $this->conf = $conf; + $this->errors = array(); } /** @@ -106,6 +112,7 @@ class PluginManager /** * 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. @@ -128,6 +135,14 @@ class PluginManager $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; } @@ -195,6 +210,16 @@ class PluginManager return $metaData; } + + /** + * Return the list of encountered errors. + * + * @return array List of errors (empty array if none exists). + */ + public function getErrors() + { + return $this->errors; + } } /** -- cgit v1.2.3