PluginManager no longer uses singleton pattern
This commit is contained in:
parent
278d9ee283
commit
51def0d849
9 changed files with 63 additions and 94 deletions
|
@ -4,17 +4,9 @@
|
||||||
* Class PluginManager
|
* Class PluginManager
|
||||||
*
|
*
|
||||||
* Use to manage, load and execute plugins.
|
* Use to manage, load and execute plugins.
|
||||||
*
|
|
||||||
* Using Singleton design pattern.
|
|
||||||
*/
|
*/
|
||||||
class PluginManager
|
class PluginManager
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* PluginManager singleton instance.
|
|
||||||
* @var PluginManager $instance
|
|
||||||
*/
|
|
||||||
private static $instance;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of authorized plugins from configuration file.
|
* List of authorized plugins from configuration file.
|
||||||
* @var array $authorizedPlugins
|
* @var array $authorizedPlugins
|
||||||
|
@ -27,6 +19,11 @@ class PluginManager
|
||||||
*/
|
*/
|
||||||
private $loadedPlugins = array();
|
private $loadedPlugins = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ConfigManager Configuration Manager instance.
|
||||||
|
*/
|
||||||
|
protected $conf;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugins subdirectory.
|
* Plugins subdirectory.
|
||||||
* @var string $PLUGINS_PATH
|
* @var string $PLUGINS_PATH
|
||||||
|
@ -40,33 +37,13 @@ class PluginManager
|
||||||
public static $META_EXT = 'meta';
|
public static $META_EXT = 'meta';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor: new instances not allowed.
|
* Constructor.
|
||||||
*/
|
|
||||||
private function __construct()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cloning isn't allowed either.
|
|
||||||
*
|
*
|
||||||
* @return void
|
* @param ConfigManager $conf Configuration Manager instance.
|
||||||
*/
|
*/
|
||||||
private function __clone()
|
public function __construct(&$conf)
|
||||||
{
|
{
|
||||||
}
|
$this->conf = $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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -122,7 +99,7 @@ public function executeHooks($hook, &$data, $params = array())
|
||||||
$hookFunction = $this->buildHookName($hook, $plugin);
|
$hookFunction = $this->buildHookName($hook, $plugin);
|
||||||
|
|
||||||
if (function_exists($hookFunction)) {
|
if (function_exists($hookFunction)) {
|
||||||
$data = call_user_func($hookFunction, $data);
|
$data = call_user_func($hookFunction, $data, $this->conf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,6 +125,7 @@ private function loadPlugin($dir, $pluginName)
|
||||||
throw new PluginFileNotFoundException($pluginName);
|
throw new PluginFileNotFoundException($pluginName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$conf = $this->conf;
|
||||||
include_once $pluginFilePath;
|
include_once $pluginFilePath;
|
||||||
|
|
||||||
$this->loadedPlugins[] = $pluginName;
|
$this->loadedPlugins[] = $pluginName;
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
$GLOBALS['plugins']['READITYOUSELF_URL'] = 'http://someurl.com';
|
|
|
@ -8,24 +8,22 @@
|
||||||
// it seems kinda dead.
|
// it seems kinda dead.
|
||||||
// Not tested.
|
// Not tested.
|
||||||
|
|
||||||
$conf = ConfigManager::getInstance();
|
|
||||||
$riyUrl = $conf->get('plugins.READITYOUSELF_URL');
|
$riyUrl = $conf->get('plugins.READITYOUSELF_URL');
|
||||||
if (empty($riyUrl)) {
|
if (empty($riyUrl)) {
|
||||||
$GLOBALS['plugin_errors'][] = 'Readityourself plugin error: '.
|
$GLOBALS['plugin_errors'][] = 'Readityourself plugin error: '.
|
||||||
'Please define "$GLOBALS[\'plugins\'][\'READITYOUSELF_URL\']" '.
|
'Please define the "READITYOUSELF_URL" setting in the plugin administration page.';
|
||||||
'in "plugins/readityourself/config.php" or in your Shaarli config.php file.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add readityourself icon to link_plugin when rendering linklist.
|
* 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.
|
* @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');
|
$riyUrl = $conf->get('plugins.READITYOUSELF_URL');
|
||||||
if (empty($riyUrl)) {
|
if (empty($riyUrl)) {
|
||||||
return $data;
|
return $data;
|
||||||
|
|
|
@ -12,31 +12,26 @@ The directory structure should look like:
|
||||||
└── plugins
|
└── plugins
|
||||||
└── wallabag
|
└── wallabag
|
||||||
├── README.md
|
├── README.md
|
||||||
├── config.php.dist
|
|
||||||
├── wallabag.html
|
├── wallabag.html
|
||||||
|
├── wallabag.meta
|
||||||
├── wallabag.php
|
├── 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).
|
To enable the plugin, you can either:
|
||||||
This should look like:
|
|
||||||
|
|
||||||
```
|
* enable it in the plugins administration page (`?do=pluginadmin`).
|
||||||
$GLOBALS['config']['PLUGINS'] = array('qrcode', 'any_other_plugin', 'wallabag')
|
* add `wallabag` to your list of enabled plugins in `data/config.json.php` (`general.enabled_plugins` section).
|
||||||
```
|
|
||||||
|
|
||||||
### Configuration
|
### 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*
|
**WALLABAG_URL**: *Wallabag instance URL*
|
||||||
```
|
Example value: `http://v2.wallabag.org`
|
||||||
$GLOBALS['config']['WALLABAG_URL'] = 'http://v2.wallabag.org' ;
|
|
||||||
```
|
|
||||||
|
|
||||||
*Wallabag version*: either `1` (for 1.x) or `2` (for 2.x)
|
**WALLABAG_VERSION**: *Wallabag version*
|
||||||
```
|
Value: either `1` (for 1.x) or `2` (for 2.x)
|
||||||
$GLOBALS['config']['WALLABAG_VERSION'] = 2;
|
|
||||||
```
|
|
||||||
|
|
||||||
> 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.
|
|
@ -1,4 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
$GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org';
|
|
||||||
$GLOBALS['plugins']['WALLABAG_VERSION'] = 1;
|
|
|
@ -6,24 +6,22 @@
|
||||||
|
|
||||||
require_once 'WallabagInstance.php';
|
require_once 'WallabagInstance.php';
|
||||||
|
|
||||||
$conf = ConfigManager::getInstance();
|
|
||||||
$wallabagUrl = $conf->get('plugins.WALLABAG_URL');
|
$wallabagUrl = $conf->get('plugins.WALLABAG_URL');
|
||||||
if (empty($wallabagUrl)) {
|
if (empty($wallabagUrl)) {
|
||||||
$GLOBALS['plugin_errors'][] = 'Wallabag plugin error: '.
|
$GLOBALS['plugin_errors'][] = 'Wallabag plugin error: '.
|
||||||
'Please define "$GLOBALS[\'plugins\'][\'WALLABAG_URL\']" '.
|
'Please define the "WALLABAG_URL" setting in the plugin administration page.';
|
||||||
'in "plugins/wallabag/config.php" or in your Shaarli config.php file.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add wallabag icon to link_plugin when rendering linklist.
|
* 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.
|
* @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');
|
$wallabagUrl = $conf->get('plugins.WALLABAG_URL');
|
||||||
if (empty($wallabagUrl)) {
|
if (empty($wallabagUrl)) {
|
||||||
return $data;
|
return $data;
|
||||||
|
|
|
@ -23,6 +23,17 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
|
||||||
*/
|
*/
|
||||||
private static $pluginName = 'test';
|
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.
|
* Test plugin loading and hook execution.
|
||||||
*
|
*
|
||||||
|
@ -30,23 +41,21 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase
|
||||||
*/
|
*/
|
||||||
public function testPlugin()
|
public function testPlugin()
|
||||||
{
|
{
|
||||||
$pluginManager = PluginManager::getInstance();
|
|
||||||
|
|
||||||
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
||||||
$pluginManager->load(array(self::$pluginName));
|
$this->pluginManager->load(array(self::$pluginName));
|
||||||
|
|
||||||
$this->assertTrue(function_exists('hook_test_random'));
|
$this->assertTrue(function_exists('hook_test_random'));
|
||||||
|
|
||||||
$data = array(0 => 'woot');
|
$data = array(0 => 'woot');
|
||||||
$pluginManager->executeHooks('random', $data);
|
$this->pluginManager->executeHooks('random', $data);
|
||||||
$this->assertEquals('woot', $data[1]);
|
$this->assertEquals('woot', $data[1]);
|
||||||
|
|
||||||
$data = array(0 => 'woot');
|
$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]);
|
$this->assertEquals('page test', $data[1]);
|
||||||
|
|
||||||
$data = array(0 => 'woot');
|
$data = array(0 => 'woot');
|
||||||
$pluginManager->executeHooks('random', $data, array('loggedin' => true));
|
$this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
|
||||||
$this->assertEquals('loggedin', $data[1]);
|
$this->assertEquals('loggedin', $data[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,11 +66,8 @@ public function testPlugin()
|
||||||
*/
|
*/
|
||||||
public function testPluginNotFound()
|
public function testPluginNotFound()
|
||||||
{
|
{
|
||||||
$pluginManager = PluginManager::getInstance();
|
$this->pluginManager->load(array());
|
||||||
|
$this->pluginManager->load(array('nope', 'renope'));
|
||||||
$pluginManager->load(array());
|
|
||||||
|
|
||||||
$pluginManager->load(array('nope', 'renope'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,16 +75,14 @@ public function testPluginNotFound()
|
||||||
*/
|
*/
|
||||||
public function testGetPluginsMeta()
|
public function testGetPluginsMeta()
|
||||||
{
|
{
|
||||||
$pluginManager = PluginManager::getInstance();
|
|
||||||
|
|
||||||
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
PluginManager::$PLUGINS_PATH = self::$pluginPath;
|
||||||
$pluginManager->load(array(self::$pluginName));
|
$this->pluginManager->load(array(self::$pluginName));
|
||||||
|
|
||||||
$expectedParameters = array(
|
$expectedParameters = array(
|
||||||
'pop' => '',
|
'pop' => '',
|
||||||
'hip' => '',
|
'hip' => '',
|
||||||
);
|
);
|
||||||
$meta = $pluginManager->getPluginsMeta();
|
$meta = $this->pluginManager->getPluginsMeta();
|
||||||
$this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
|
$this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
|
||||||
$this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
|
$this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
* PluginReadityourselfTest.php.php
|
* PluginReadityourselfTest.php.php
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// FIXME! add an init method.
|
||||||
|
$conf = new ConfigManager('');
|
||||||
require_once 'plugins/readityourself/readityourself.php';
|
require_once 'plugins/readityourself/readityourself.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,7 +27,7 @@ function setUp()
|
||||||
*/
|
*/
|
||||||
function testReadityourselfLinklist()
|
function testReadityourselfLinklist()
|
||||||
{
|
{
|
||||||
$conf = ConfigManager::getInstance();
|
$conf = new ConfigManager('');
|
||||||
$conf->set('plugins.READITYOUSELF_URL', 'value');
|
$conf->set('plugins.READITYOUSELF_URL', 'value');
|
||||||
$str = 'http://randomstr.com/test';
|
$str = 'http://randomstr.com/test';
|
||||||
$data = array(
|
$data = array(
|
||||||
|
@ -37,7 +39,7 @@ function testReadityourselfLinklist()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$data = hook_readityourself_render_linklist($data);
|
$data = hook_readityourself_render_linklist($data, $conf);
|
||||||
$link = $data['links'][0];
|
$link = $data['links'][0];
|
||||||
// data shouldn't be altered
|
// data shouldn't be altered
|
||||||
$this->assertEquals($str, $data['title']);
|
$this->assertEquals($str, $data['title']);
|
||||||
|
@ -53,7 +55,7 @@ function testReadityourselfLinklist()
|
||||||
*/
|
*/
|
||||||
function testReadityourselfLinklistWithoutConfig()
|
function testReadityourselfLinklistWithoutConfig()
|
||||||
{
|
{
|
||||||
$conf = ConfigManager::getInstance();
|
$conf = new ConfigManager('');
|
||||||
$conf->set('plugins.READITYOUSELF_URL', null);
|
$conf->set('plugins.READITYOUSELF_URL', null);
|
||||||
$str = 'http://randomstr.com/test';
|
$str = 'http://randomstr.com/test';
|
||||||
$data = array(
|
$data = array(
|
||||||
|
@ -65,7 +67,7 @@ function testReadityourselfLinklistWithoutConfig()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$data = hook_readityourself_render_linklist($data);
|
$data = hook_readityourself_render_linklist($data, $conf);
|
||||||
$link = $data['links'][0];
|
$link = $data['links'][0];
|
||||||
// data shouldn't be altered
|
// data shouldn't be altered
|
||||||
$this->assertEquals($str, $data['title']);
|
$this->assertEquals($str, $data['title']);
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
* PluginWallabagTest.php.php
|
* PluginWallabagTest.php.php
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// FIXME! add an init method.
|
||||||
|
$conf = new ConfigManager('');
|
||||||
require_once 'plugins/wallabag/wallabag.php';
|
require_once 'plugins/wallabag/wallabag.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,7 +27,7 @@ function setUp()
|
||||||
*/
|
*/
|
||||||
function testWallabagLinklist()
|
function testWallabagLinklist()
|
||||||
{
|
{
|
||||||
$conf = ConfigManager::getInstance();
|
$conf = new ConfigManager('');
|
||||||
$conf->set('plugins.WALLABAG_URL', 'value');
|
$conf->set('plugins.WALLABAG_URL', 'value');
|
||||||
$str = 'http://randomstr.com/test';
|
$str = 'http://randomstr.com/test';
|
||||||
$data = array(
|
$data = array(
|
||||||
|
@ -37,7 +39,7 @@ function testWallabagLinklist()
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$data = hook_wallabag_render_linklist($data);
|
$data = hook_wallabag_render_linklist($data, $conf);
|
||||||
$link = $data['links'][0];
|
$link = $data['links'][0];
|
||||||
// data shouldn't be altered
|
// data shouldn't be altered
|
||||||
$this->assertEquals($str, $data['title']);
|
$this->assertEquals($str, $data['title']);
|
||||||
|
@ -49,4 +51,3 @@ function testWallabagLinklist()
|
||||||
$this->assertNotFalse(strpos($link['link_plugin'][0], $conf->get('plugins.WALLABAG_URL')));
|
$this->assertNotFalse(strpos($link['link_plugin'][0], $conf->get('plugins.WALLABAG_URL')));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue