]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
PluginManager no longer uses singleton pattern
authorArthurHoaro <arthur@hoa.ro>
Thu, 9 Jun 2016 18:04:32 +0000 (20:04 +0200)
committerArthurHoaro <arthur@hoa.ro>
Sat, 11 Jun 2016 07:30:56 +0000 (09:30 +0200)
application/PluginManager.php
plugins/readityourself/config.php.dist [deleted file]
plugins/readityourself/readityourself.php
plugins/wallabag/README.md
plugins/wallabag/config.php.dist [deleted file]
plugins/wallabag/wallabag.php
tests/PluginManagerTest.php
tests/plugins/PluginReadityourselfTest.php
tests/plugins/PluginWallabagTest.php

index 787ac6a930f63f4c1245a3178b6de444be6569a9..dca7e63e571621461c72e64e3b42b0c2cd29fea2 100644 (file)
@@ -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
@@ -27,6 +19,11 @@ class PluginManager
      */
     private $loadedPlugins = array();
 
+    /**
+     * @var ConfigManager Configuration Manager instance.
+     */
+    protected $conf;
+
     /**
      * Plugins subdirectory.
      * @var string $PLUGINS_PATH
@@ -40,33 +37,13 @@ class PluginManager
     public static $META_EXT = 'meta';
 
     /**
-     * Private constructor: new instances not allowed.
-     */
-    private function __construct()
-    {
-    }
-
-    /**
-     * Cloning isn't allowed either.
-     *
-     * @return void
-     */
-    private function __clone()
-    {
-    }
-
-    /**
-     * 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;
     }
 
     /**
@@ -102,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
      */
@@ -122,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);
             }
         }
     }
@@ -148,6 +125,7 @@ class PluginManager
             throw new PluginFileNotFoundException($pluginName);
         }
 
+        $conf = $this->conf;
         include_once $pluginFilePath;
 
         $this->loadedPlugins[] = $pluginName;
diff --git a/plugins/readityourself/config.php.dist b/plugins/readityourself/config.php.dist
deleted file mode 100644 (file)
index d6b5cb8..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-<?php
-
-$GLOBALS['plugins']['READITYOUSELF_URL'] = 'http://someurl.com';
\ No newline at end of file
index 9ca73e01098ea08aaadfab52432797be86442428..4bfcf50115c98211743539850bb6606ecbd1b318 100644 (file)
@@ -8,24 +8,22 @@
 // it seems kinda dead.
 // Not tested.
 
-$conf = ConfigManager::getInstance();
 $riyUrl = $conf->get('plugins.READITYOUSELF_URL');
 if (empty($riyUrl)) {
     $GLOBALS['plugin_errors'][] = 'Readityourself plugin error: '.
-        'Please define "$GLOBALS[\'plugins\'][\'READITYOUSELF_URL\']" '.
-        'in "plugins/readityourself/config.php" or in your Shaarli config.php file.';
+        'Please define the "READITYOUSELF_URL" setting in the plugin administration page.';
 }
 
 /**
  * Add readityourself icon to link_plugin when rendering linklist.
  *
- * @param mixed $data - linklist data.
+ * @param mixed         $data Linklist data.
+ * @param ConfigManager $conf Configuration Manager instance.
  *
  * @return mixed - linklist data with readityourself plugin.
  */
-function hook_readityourself_render_linklist($data)
+function hook_readityourself_render_linklist($data, $conf)
 {
-    $conf = ConfigManager::getInstance();
     $riyUrl = $conf->get('plugins.READITYOUSELF_URL');
     if (empty($riyUrl)) {
         return $data;
index 5bc35be11a847fa9cf93620f3446a517bc5d2aac..3f9305643aa7b55bcf4ec34c6f771a65a119f382 100644 (file)
@@ -12,31 +12,26 @@ The directory structure should look like:
     └── plugins
         └── wallabag
             ├── README.md
-            ├── config.php.dist
             ├── wallabag.html
+            ├── wallabag.meta
             ├── wallabag.php
-            └── wallabag.png
+            ├── wallabag.php
+            └── WallabagInstance.php
 ```
 
-To enable the plugin, add `'wallabag'` to your list of enabled plugins in `data/options.php` (`PLUGINS` array).
-This should look like:
+To enable the plugin, you can either:
 
-```
-$GLOBALS['config']['PLUGINS'] = array('qrcode', 'any_other_plugin', 'wallabag')
-```
+  * enable it in the plugins administration page (`?do=pluginadmin`). 
+  * add `wallabag` to your list of enabled plugins in `data/config.json.php` (`general.enabled_plugins` section).
 
 ### Configuration
 
-Copy `config.php.dist` into `config.php` and setup your instance.
+Go to the plugin administration page, and edit the following settings (with the plugin enabled).
 
-*Wallabag instance URL*
-```
-$GLOBALS['config']['WALLABAG_URL'] = 'http://v2.wallabag.org' ;
-```
+**WALLABAG_URL**: *Wallabag instance URL*  
+Example value: `http://v2.wallabag.org`
 
-*Wallabag version*: either `1` (for 1.x) or `2` (for 2.x)
-```
-$GLOBALS['config']['WALLABAG_VERSION'] = 2;
-```
+**WALLABAG_VERSION**: *Wallabag version*  
+Value: either `1` (for 1.x) or `2` (for 2.x)
 
-> Note: these settings can also be set in `data/config.php`.
\ No newline at end of file
+> Note: these settings can also be set in `data/config.json.php`, in the plugins section.
\ No newline at end of file
diff --git a/plugins/wallabag/config.php.dist b/plugins/wallabag/config.php.dist
deleted file mode 100644 (file)
index a602708..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-<?php
-
-$GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org';
-$GLOBALS['plugins']['WALLABAG_VERSION'] = 1;
\ No newline at end of file
index 4726d9369fab4ef8dd9ef0bada8e323a626d3aac..ec09c8a14d03dc89e2865efb794756d5ee199102 100644 (file)
@@ -6,24 +6,22 @@
 
 require_once 'WallabagInstance.php';
 
-$conf = ConfigManager::getInstance();
 $wallabagUrl = $conf->get('plugins.WALLABAG_URL');
 if (empty($wallabagUrl)) {
     $GLOBALS['plugin_errors'][] = 'Wallabag plugin error: '.
-        'Please define "$GLOBALS[\'plugins\'][\'WALLABAG_URL\']" '.
-        'in "plugins/wallabag/config.php" or in your Shaarli config.php file.';
+        'Please define the "WALLABAG_URL" setting in the plugin administration page.';
 }
 
 /**
  * Add wallabag icon to link_plugin when rendering linklist.
  *
- * @param mixed $data - linklist data.
+ * @param mixed         $data Linklist data.
+ * @param ConfigManager $conf Configuration Manager instance.
  *
  * @return mixed - linklist data with wallabag plugin.
  */
-function hook_wallabag_render_linklist($data)
+function hook_wallabag_render_linklist($data, $conf)
 {
-    $conf = ConfigManager::getInstance();
     $wallabagUrl = $conf->get('plugins.WALLABAG_URL');
     if (empty($wallabagUrl)) {
         return $data;
index 348082c763c2dde8a25c474ae0979248a6a9ad0e..61efce68310ba1c791063ecebb7544bf57acf57d 100644 (file)
@@ -23,6 +23,17 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
      */
     private static $pluginName = 'test';
 
+    /**
+     * @var PluginManager $pluginManager Plugin Mananger instance.
+     */
+    protected $pluginManager;
+
+    public function setUp()
+    {
+        $conf = new ConfigManager('');
+        $this->pluginManager = new PluginManager($conf);
+    }
+
     /**
      * Test plugin loading and hook execution.
      *
@@ -30,23 +41,21 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
      */
     public function testPlugin()
     {
-        $pluginManager = PluginManager::getInstance();
-
         PluginManager::$PLUGINS_PATH = self::$pluginPath;
-        $pluginManager->load(array(self::$pluginName));
+        $this->pluginManager->load(array(self::$pluginName));
 
         $this->assertTrue(function_exists('hook_test_random'));
 
         $data = array(0 => 'woot');
-        $pluginManager->executeHooks('random', $data);
+        $this->pluginManager->executeHooks('random', $data);
         $this->assertEquals('woot', $data[1]);
 
         $data = array(0 => 'woot');
-        $pluginManager->executeHooks('random', $data, array('target' => 'test'));
+        $this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
         $this->assertEquals('page test', $data[1]);
 
         $data = array(0 => 'woot');
-        $pluginManager->executeHooks('random', $data, array('loggedin' => true));
+        $this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
         $this->assertEquals('loggedin', $data[1]);
     }
 
@@ -57,11 +66,8 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
      */
     public function testPluginNotFound()
     {
-        $pluginManager = PluginManager::getInstance();
-
-        $pluginManager->load(array());
-
-        $pluginManager->load(array('nope', 'renope'));
+        $this->pluginManager->load(array());
+        $this->pluginManager->load(array('nope', 'renope'));
     }
 
     /**
@@ -69,16 +75,14 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
      */
     public function testGetPluginsMeta()
     {
-        $pluginManager = PluginManager::getInstance();
-
         PluginManager::$PLUGINS_PATH = self::$pluginPath;
-        $pluginManager->load(array(self::$pluginName));
+        $this->pluginManager->load(array(self::$pluginName));
 
         $expectedParameters = array(
             'pop' => '',
             'hip' => '',
         );
-        $meta = $pluginManager->getPluginsMeta();
+        $meta = $this->pluginManager->getPluginsMeta();
         $this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
         $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
     }
index bc5da042206e1b583d178a0ac60f7d8c1d915cd9..d73e666a5157c99987e9b273188e375adace5711 100644 (file)
@@ -4,6 +4,8 @@
  * PluginReadityourselfTest.php.php
  */
 
+// FIXME! add an init method.
+$conf = new ConfigManager('');
 require_once 'plugins/readityourself/readityourself.php';
 
 /**
@@ -25,7 +27,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
      */
     function testReadityourselfLinklist()
     {
-        $conf = ConfigManager::getInstance();
+        $conf = new ConfigManager('');
         $conf->set('plugins.READITYOUSELF_URL', 'value');
         $str = 'http://randomstr.com/test';
         $data = array(
@@ -37,7 +39,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
             )
         );
 
-        $data = hook_readityourself_render_linklist($data);
+        $data = hook_readityourself_render_linklist($data, $conf);
         $link = $data['links'][0];
         // data shouldn't be altered
         $this->assertEquals($str, $data['title']);
@@ -53,7 +55,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
      */
     function testReadityourselfLinklistWithoutConfig()
     {
-        $conf = ConfigManager::getInstance();
+        $conf = new ConfigManager('');
         $conf->set('plugins.READITYOUSELF_URL', null);
         $str = 'http://randomstr.com/test';
         $data = array(
@@ -65,7 +67,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase
             )
         );
 
-        $data = hook_readityourself_render_linklist($data);
+        $data = hook_readityourself_render_linklist($data, $conf);
         $link = $data['links'][0];
         // data shouldn't be altered
         $this->assertEquals($str, $data['title']);
index e6f8a8b64b518502c57903a86d79c6c2c031e7c4..302ee296f023d4f8a97db2270cb08e79d6650e82 100644 (file)
@@ -4,6 +4,8 @@
  * PluginWallabagTest.php.php
  */
 
+// FIXME! add an init method.
+$conf = new ConfigManager('');
 require_once 'plugins/wallabag/wallabag.php';
 
 /**
@@ -25,7 +27,7 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
      */
     function testWallabagLinklist()
     {
-        $conf = ConfigManager::getInstance();
+        $conf = new ConfigManager('');
         $conf->set('plugins.WALLABAG_URL', 'value');
         $str = 'http://randomstr.com/test';
         $data = array(
@@ -37,7 +39,7 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
             )
         );
 
-        $data = hook_wallabag_render_linklist($data);
+        $data = hook_wallabag_render_linklist($data, $conf);
         $link = $data['links'][0];
         // data shouldn't be altered
         $this->assertEquals($str, $data['title']);
@@ -49,4 +51,3 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
         $this->assertNotFalse(strpos($link['link_plugin'][0], $conf->get('plugins.WALLABAG_URL')));
     }
 }
-