PluginManager no longer uses singleton pattern

This commit is contained in:
ArthurHoaro 2016-06-09 20:04:32 +02:00
parent 278d9ee283
commit 51def0d849
9 changed files with 63 additions and 94 deletions

View 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.
* Constructor.
*
* @return void
* @param ConfigManager $conf Configuration Manager instance.
*/
private function __clone()
public function __construct(&$conf)
{
}
/**
* Return existing instance of PluginManager, or create it.
*
* @return PluginManager instance.
*/
public static function getInstance()
{
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;

View file

@ -1,3 +0,0 @@
<?php
$GLOBALS['plugins']['READITYOUSELF_URL'] = 'http://someurl.com';

View 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;

View 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`.
> Note: these settings can also be set in `data/config.json.php`, in the plugins section.

View file

@ -1,4 +0,0 @@
<?php
$GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org';
$GLOBALS['plugins']['WALLABAG_VERSION'] = 1;

View 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;

View 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']);
}

View 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']);

View 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')));
}
}