* 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
*/
private $loadedPlugins = array();
+ /**
+ * @var ConfigManager Configuration Manager instance.
+ */
+ protected $conf;
+
/**
* Plugins subdirectory.
* @var string $PLUGINS_PATH
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;
}
/**
/**
* 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
*/
$hookFunction = $this->buildHookName($hook, $plugin);
if (function_exists($hookFunction)) {
- $data = call_user_func($hookFunction, $data);
+ $data = call_user_func($hookFunction, $data, $this->conf);
}
}
}
throw new PluginFileNotFoundException($pluginName);
}
+ $conf = $this->conf;
include_once $pluginFilePath;
$this->loadedPlugins[] = $pluginName;
+++ /dev/null
-<?php
-
-$GLOBALS['plugins']['READITYOUSELF_URL'] = 'http://someurl.com';
\ No newline at end of file
// 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;
└── 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
+++ /dev/null
-<?php
-
-$GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org';
-$GLOBALS['plugins']['WALLABAG_VERSION'] = 1;
\ No newline at end of file
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;
*/
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.
*
*/
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]);
}
*/
public function testPluginNotFound()
{
- $pluginManager = PluginManager::getInstance();
-
- $pluginManager->load(array());
-
- $pluginManager->load(array('nope', 'renope'));
+ $this->pluginManager->load(array());
+ $this->pluginManager->load(array('nope', 'renope'));
}
/**
*/
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']);
}
* PluginReadityourselfTest.php.php
*/
+// FIXME! add an init method.
+$conf = new ConfigManager('');
require_once 'plugins/readityourself/readityourself.php';
/**
*/
function testReadityourselfLinklist()
{
- $conf = ConfigManager::getInstance();
+ $conf = new ConfigManager('');
$conf->set('plugins.READITYOUSELF_URL', 'value');
$str = 'http://randomstr.com/test';
$data = array(
)
);
- $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']);
*/
function testReadityourselfLinklistWithoutConfig()
{
- $conf = ConfigManager::getInstance();
+ $conf = new ConfigManager('');
$conf->set('plugins.READITYOUSELF_URL', null);
$str = 'http://randomstr.com/test';
$data = array(
)
);
- $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']);
* PluginWallabagTest.php.php
*/
+// FIXME! add an init method.
+$conf = new ConfigManager('');
require_once 'plugins/wallabag/wallabag.php';
/**
*/
function testWallabagLinklist()
{
- $conf = ConfigManager::getInstance();
+ $conf = new ConfigManager('');
$conf->set('plugins.WALLABAG_URL', 'value');
$str = 'http://randomstr.com/test';
$data = array(
)
);
- $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']);
$this->assertNotFalse(strpos($link['link_plugin'][0], $conf->get('plugins.WALLABAG_URL')));
}
}
-