Skip to content

Commit

Permalink
PluginManager no longer uses singleton pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHoaro committed Jun 9, 2016
1 parent 01fdfc5 commit 7f444f1
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 94 deletions.
50 changes: 14 additions & 36 deletions application/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +19,11 @@ class PluginManager
*/
private $loadedPlugins = array();

/**
* @var ConfigManager Configuration Manager instance.
*/
protected $conf;

/**
* Plugins subdirectory.
* @var string $PLUGINS_PATH
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -102,9 +79,9 @@ public function load($authorizedPlugins)
/**
* 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
*/
Expand All @@ -122,7 +99,7 @@ public function executeHooks($hook, &$data, $params = array())
$hookFunction = $this->buildHookName($hook, $plugin);

if (function_exists($hookFunction)) {
$data = call_user_func($hookFunction, $data);
$data = call_user_func($hookFunction, $data, $this->conf);
}
}
}
Expand All @@ -148,6 +125,7 @@ private function loadPlugin($dir, $pluginName)
throw new PluginFileNotFoundException($pluginName);
}

$conf = $this->conf;
include_once $pluginFilePath;

$this->loadedPlugins[] = $pluginName;
Expand Down
3 changes: 0 additions & 3 deletions plugins/readityourself/config.php.dist

This file was deleted.

10 changes: 4 additions & 6 deletions plugins/readityourself/readityourself.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 12 additions & 17 deletions plugins/wallabag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 0 additions & 4 deletions plugins/wallabag/config.php.dist

This file was deleted.

10 changes: 4 additions & 6 deletions plugins/wallabag/wallabag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 19 additions & 15 deletions tests/PluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,39 @@ 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.
*
* @return void
*/
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]);
}

Expand All @@ -57,28 +66,23 @@ public function testPlugin()
*/
public function testPluginNotFound()
{
$pluginManager = PluginManager::getInstance();

$pluginManager->load(array());

$pluginManager->load(array('nope', 'renope'));
$this->pluginManager->load(array());
$this->pluginManager->load(array('nope', 'renope'));
}

/**
* Test plugin metadata loading.
*/
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']);
}
Expand Down
10 changes: 6 additions & 4 deletions tests/plugins/PluginReadityourselfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* PluginReadityourselfTest.php.php
*/

// FIXME! add an init method.
$conf = new ConfigManager('');
require_once 'plugins/readityourself/readityourself.php';

/**
Expand All @@ -25,7 +27,7 @@ function setUp()
*/
function testReadityourselfLinklist()
{
$conf = ConfigManager::getInstance();
$conf = new ConfigManager('');
$conf->set('plugins.READITYOUSELF_URL', 'value');
$str = 'http://randomstr.com/test';
$data = array(
Expand All @@ -37,7 +39,7 @@ function testReadityourselfLinklist()
)
);

$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']);
Expand All @@ -53,7 +55,7 @@ function testReadityourselfLinklist()
*/
function testReadityourselfLinklistWithoutConfig()
{
$conf = ConfigManager::getInstance();
$conf = new ConfigManager('');
$conf->set('plugins.READITYOUSELF_URL', null);
$str = 'http://randomstr.com/test';
$data = array(
Expand All @@ -65,7 +67,7 @@ function testReadityourselfLinklistWithoutConfig()
)
);

$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']);
Expand Down
7 changes: 4 additions & 3 deletions tests/plugins/PluginWallabagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* PluginWallabagTest.php.php
*/

// FIXME! add an init method.
$conf = new ConfigManager('');
require_once 'plugins/wallabag/wallabag.php';

/**
Expand All @@ -25,7 +27,7 @@ function setUp()
*/
function testWallabagLinklist()
{
$conf = ConfigManager::getInstance();
$conf = new ConfigManager('');
$conf->set('plugins.WALLABAG_URL', 'value');
$str = 'http://randomstr.com/test';
$data = array(
Expand All @@ -37,7 +39,7 @@ function testWallabagLinklist()
)
);

$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']);
Expand All @@ -49,4 +51,3 @@ function testWallabagLinklist()
$this->assertNotFalse(strpos($link['link_plugin'][0], $conf->get('plugins.WALLABAG_URL')));
}
}

0 comments on commit 7f444f1

Please sign in to comment.