]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Merge pull request #659 from ArthurHoaro/plugin-errors
authorArthur <arthur@hoa.ro>
Mon, 17 Oct 2016 06:50:18 +0000 (08:50 +0200)
committerGitHub <noreply@github.com>
Mon, 17 Oct 2016 06:50:18 +0000 (08:50 +0200)
New init function for plugins, supports errors reporting

application/PageBuilder.php
application/PluginManager.php
index.php
plugins/demo_plugin/demo_plugin.php
plugins/readityourself/readityourself.php
plugins/wallabag/wallabag.php
tests/plugins/PluginReadityourselfTest.php
tests/plugins/PluginWallabagTest.php

index 42932f32681a512f35de7c69ceb137c264f311b9..32c7f9f18b01ba131be61bb778dc3d7a9239b727 100644 (file)
@@ -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);
index 1e132a7f652c78aa077a86ed3c1013ad58b7c95b..59ece4fa9c735b8c2d150b939d48794fddeb2279 100644 (file)
@@ -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;
+    }
 }
 
 /**
index 5bc13d49fd812f88d662c1d21bc53e3ae02c2a10..f7e73bde956c161d043d627361b92d7ca6eed519 100644 (file)
--- a/index.php
+++ b/index.php
@@ -778,6 +778,7 @@ function renderPage($conf, $pluginManager)
     $PAGE = new PageBuilder($conf);
     $PAGE->assign('linkcount', count($LINKSDB));
     $PAGE->assign('privateLinkcount', count_private($LINKSDB));
+    $PAGE->assign('plugin_errors', $pluginManager->getErrors());
 
     // Determine which page will be rendered.
     $query = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : '';
index 18834e5331d91cc9d217c18a284056097bcc23db..7335c9d4665a0429549d7fd3977ca961f81e58fa 100644 (file)
  * and check user status with _LOGGEDIN_.
  */
 
+/**
+ * Initialization function.
+ * It will be called when the plugin is loaded.
+ * This function can be used to return a list of initialization errors.
+ *
+ * @param $conf ConfigManager instance.
+ *
+ * @return array List of errors (optional).
+ */
+function demo_plugin_init($conf)
+{
+    $conf->get('toto', 'nope');
+
+    $errors[] = 'This a demo init error.';
+    return $errors;
+}
+
 /**
  * Hook render_header.
  * Executed on every page redering.
index 4bfcf50115c98211743539850bb6606ecbd1b318..961c5bda0c2f79347e9e69e63d4eedfde776c6cb 100644 (file)
@@ -8,10 +8,21 @@
 // it seems kinda dead.
 // Not tested.
 
-$riyUrl = $conf->get('plugins.READITYOUSELF_URL');
-if (empty($riyUrl)) {
-    $GLOBALS['plugin_errors'][] = 'Readityourself plugin error: '.
-        'Please define the "READITYOUSELF_URL" setting in the plugin administration page.';
+/**
+ * Init function, return an error if the server is not set.
+ *
+ * @param $conf ConfigManager instance.
+ *
+ * @return array Eventual error.
+ */
+function readityourself_init($conf)
+{
+    $riyUrl = $conf->get('plugins.READITYOUSELF_URL');
+    if (empty($riyUrl)) {
+        $error = 'Readityourself plugin error: '.
+            'Please define the "READITYOUSELF_URL" setting in the plugin administration page.';
+        return array($error);
+    }
 }
 
 /**
index ec09c8a14d03dc89e2865efb794756d5ee199102..641e4cc237ed4f20a44fafda7eae1b871bf2e118 100644 (file)
@@ -6,10 +6,21 @@
 
 require_once 'WallabagInstance.php';
 
-$wallabagUrl = $conf->get('plugins.WALLABAG_URL');
-if (empty($wallabagUrl)) {
-    $GLOBALS['plugin_errors'][] = 'Wallabag plugin error: '.
-        'Please define the "WALLABAG_URL" setting in the plugin administration page.';
+/**
+ * Init function, return an error if the server is not set.
+ *
+ * @param $conf ConfigManager instance.
+ *
+ * @return array Eventual error.
+ */
+function wallabag_init($conf)
+{
+    $wallabagUrl = $conf->get('plugins.WALLABAG_URL');
+    if (empty($wallabagUrl)) {
+        $error = 'Wallabag plugin error: '.
+            'Please define the "WALLABAG_URL" setting in the plugin administration page.';
+        return array($error);
+    }
 }
 
 /**
index d73e666a5157c99987e9b273188e375adace5711..532db14626d5e177566ddd8c352f5fab621f91a2 100644 (file)
@@ -4,8 +4,6 @@
  * PluginReadityourselfTest.php.php
  */
 
-// FIXME! add an init method.
-$conf = new ConfigManager('');
 require_once 'plugins/readityourself/readityourself.php';
 
 /**
@@ -22,6 +20,27 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
         PluginManager::$PLUGINS_PATH = 'plugins';
     }
 
+    /**
+     * Test Readityourself init without errors.
+     */
+    function testReadityourselfInitNoError()
+    {
+        $conf = new ConfigManager('');
+        $conf->set('plugins.READITYOUSELF_URL', 'value');
+        $errors = readityourself_init($conf);
+        $this->assertEmpty($errors);
+    }
+
+    /**
+     * Test Readityourself init with errors.
+     */
+    function testReadityourselfInitError()
+    {
+        $conf = new ConfigManager('');
+        $errors = readityourself_init($conf);
+        $this->assertNotEmpty($errors);
+    }
+
     /**
      * Test render_linklist hook.
      */
index 302ee296f023d4f8a97db2270cb08e79d6650e82..2c268cbd1b714b3b9aa1c29891bbf40e8592b24a 100644 (file)
@@ -4,8 +4,6 @@
  * PluginWallabagTest.php.php
  */
 
-// FIXME! add an init method.
-$conf = new ConfigManager('');
 require_once 'plugins/wallabag/wallabag.php';
 
 /**
@@ -22,6 +20,27 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
         PluginManager::$PLUGINS_PATH = 'plugins';
     }
 
+    /**
+     * Test wallabag init without errors.
+     */
+    function testWallabagInitNoError()
+    {
+        $conf = new ConfigManager('');
+        $conf->set('plugins.WALLABAG_URL', 'value');
+        $errors = wallabag_init($conf);
+        $this->assertEmpty($errors);
+    }
+
+    /**
+     * Test wallabag init with errors.
+     */
+    function testWallabagInitError()
+    {
+        $conf = new ConfigManager('');
+        $errors = wallabag_init($conf);
+        $this->assertNotEmpty($errors);
+    }
+
     /**
      * Test render_linklist hook.
      */