diff options
-rw-r--r-- | application/PluginManager.php | 50 | ||||
-rw-r--r-- | plugins/readityourself/config.php.dist | 3 | ||||
-rw-r--r-- | plugins/readityourself/readityourself.php | 10 | ||||
-rw-r--r-- | plugins/wallabag/README.md | 29 | ||||
-rw-r--r-- | plugins/wallabag/config.php.dist | 4 | ||||
-rw-r--r-- | plugins/wallabag/wallabag.php | 10 | ||||
-rw-r--r-- | tests/PluginManagerTest.php | 34 | ||||
-rw-r--r-- | tests/plugins/PluginReadityourselfTest.php | 10 | ||||
-rw-r--r-- | tests/plugins/PluginWallabagTest.php | 7 |
9 files changed, 63 insertions, 94 deletions
diff --git a/application/PluginManager.php b/application/PluginManager.php index 787ac6a9..dca7e63e 100644 --- a/application/PluginManager.php +++ b/application/PluginManager.php | |||
@@ -4,18 +4,10 @@ | |||
4 | * Class PluginManager | 4 | * Class PluginManager |
5 | * | 5 | * |
6 | * Use to manage, load and execute plugins. | 6 | * Use to manage, load and execute plugins. |
7 | * | ||
8 | * Using Singleton design pattern. | ||
9 | */ | 7 | */ |
10 | class PluginManager | 8 | class PluginManager |
11 | { | 9 | { |
12 | /** | 10 | /** |
13 | * PluginManager singleton instance. | ||
14 | * @var PluginManager $instance | ||
15 | */ | ||
16 | private static $instance; | ||
17 | |||
18 | /** | ||
19 | * List of authorized plugins from configuration file. | 11 | * List of authorized plugins from configuration file. |
20 | * @var array $authorizedPlugins | 12 | * @var array $authorizedPlugins |
21 | */ | 13 | */ |
@@ -28,6 +20,11 @@ class PluginManager | |||
28 | private $loadedPlugins = array(); | 20 | private $loadedPlugins = array(); |
29 | 21 | ||
30 | /** | 22 | /** |
23 | * @var ConfigManager Configuration Manager instance. | ||
24 | */ | ||
25 | protected $conf; | ||
26 | |||
27 | /** | ||
31 | * Plugins subdirectory. | 28 | * Plugins subdirectory. |
32 | * @var string $PLUGINS_PATH | 29 | * @var string $PLUGINS_PATH |
33 | */ | 30 | */ |
@@ -40,33 +37,13 @@ class PluginManager | |||
40 | public static $META_EXT = 'meta'; | 37 | public static $META_EXT = 'meta'; |
41 | 38 | ||
42 | /** | 39 | /** |
43 | * Private constructor: new instances not allowed. | 40 | * Constructor. |
44 | */ | ||
45 | private function __construct() | ||
46 | { | ||
47 | } | ||
48 | |||
49 | /** | ||
50 | * Cloning isn't allowed either. | ||
51 | * | ||
52 | * @return void | ||
53 | */ | ||
54 | private function __clone() | ||
55 | { | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * Return existing instance of PluginManager, or create it. | ||
60 | * | 41 | * |
61 | * @return PluginManager instance. | 42 | * @param ConfigManager $conf Configuration Manager instance. |
62 | */ | 43 | */ |
63 | public static function getInstance() | 44 | public function __construct(&$conf) |
64 | { | 45 | { |
65 | if (!(self::$instance instanceof self)) { | 46 | $this->conf = $conf; |
66 | self::$instance = new self(); | ||
67 | } | ||
68 | |||
69 | return self::$instance; | ||
70 | } | 47 | } |
71 | 48 | ||
72 | /** | 49 | /** |
@@ -102,9 +79,9 @@ class PluginManager | |||
102 | /** | 79 | /** |
103 | * Execute all plugins registered hook. | 80 | * Execute all plugins registered hook. |
104 | * | 81 | * |
105 | * @param string $hook name of the hook to trigger. | 82 | * @param string $hook name of the hook to trigger. |
106 | * @param array $data list of data to manipulate passed by reference. | 83 | * @param array $data list of data to manipulate passed by reference. |
107 | * @param array $params additional parameters such as page target. | 84 | * @param array $params additional parameters such as page target. |
108 | * | 85 | * |
109 | * @return void | 86 | * @return void |
110 | */ | 87 | */ |
@@ -122,7 +99,7 @@ class PluginManager | |||
122 | $hookFunction = $this->buildHookName($hook, $plugin); | 99 | $hookFunction = $this->buildHookName($hook, $plugin); |
123 | 100 | ||
124 | if (function_exists($hookFunction)) { | 101 | if (function_exists($hookFunction)) { |
125 | $data = call_user_func($hookFunction, $data); | 102 | $data = call_user_func($hookFunction, $data, $this->conf); |
126 | } | 103 | } |
127 | } | 104 | } |
128 | } | 105 | } |
@@ -148,6 +125,7 @@ class PluginManager | |||
148 | throw new PluginFileNotFoundException($pluginName); | 125 | throw new PluginFileNotFoundException($pluginName); |
149 | } | 126 | } |
150 | 127 | ||
128 | $conf = $this->conf; | ||
151 | include_once $pluginFilePath; | 129 | include_once $pluginFilePath; |
152 | 130 | ||
153 | $this->loadedPlugins[] = $pluginName; | 131 | $this->loadedPlugins[] = $pluginName; |
diff --git a/plugins/readityourself/config.php.dist b/plugins/readityourself/config.php.dist deleted file mode 100644 index d6b5cb85..00000000 --- a/plugins/readityourself/config.php.dist +++ /dev/null | |||
@@ -1,3 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | $GLOBALS['plugins']['READITYOUSELF_URL'] = 'http://someurl.com'; \ No newline at end of file | ||
diff --git a/plugins/readityourself/readityourself.php b/plugins/readityourself/readityourself.php index 9ca73e01..4bfcf501 100644 --- a/plugins/readityourself/readityourself.php +++ b/plugins/readityourself/readityourself.php | |||
@@ -8,24 +8,22 @@ | |||
8 | // it seems kinda dead. | 8 | // it seems kinda dead. |
9 | // Not tested. | 9 | // Not tested. |
10 | 10 | ||
11 | $conf = ConfigManager::getInstance(); | ||
12 | $riyUrl = $conf->get('plugins.READITYOUSELF_URL'); | 11 | $riyUrl = $conf->get('plugins.READITYOUSELF_URL'); |
13 | if (empty($riyUrl)) { | 12 | if (empty($riyUrl)) { |
14 | $GLOBALS['plugin_errors'][] = 'Readityourself plugin error: '. | 13 | $GLOBALS['plugin_errors'][] = 'Readityourself plugin error: '. |
15 | 'Please define "$GLOBALS[\'plugins\'][\'READITYOUSELF_URL\']" '. | 14 | 'Please define the "READITYOUSELF_URL" setting in the plugin administration page.'; |
16 | 'in "plugins/readityourself/config.php" or in your Shaarli config.php file.'; | ||
17 | } | 15 | } |
18 | 16 | ||
19 | /** | 17 | /** |
20 | * Add readityourself icon to link_plugin when rendering linklist. | 18 | * Add readityourself icon to link_plugin when rendering linklist. |
21 | * | 19 | * |
22 | * @param mixed $data - linklist data. | 20 | * @param mixed $data Linklist data. |
21 | * @param ConfigManager $conf Configuration Manager instance. | ||
23 | * | 22 | * |
24 | * @return mixed - linklist data with readityourself plugin. | 23 | * @return mixed - linklist data with readityourself plugin. |
25 | */ | 24 | */ |
26 | function hook_readityourself_render_linklist($data) | 25 | function hook_readityourself_render_linklist($data, $conf) |
27 | { | 26 | { |
28 | $conf = ConfigManager::getInstance(); | ||
29 | $riyUrl = $conf->get('plugins.READITYOUSELF_URL'); | 27 | $riyUrl = $conf->get('plugins.READITYOUSELF_URL'); |
30 | if (empty($riyUrl)) { | 28 | if (empty($riyUrl)) { |
31 | return $data; | 29 | return $data; |
diff --git a/plugins/wallabag/README.md b/plugins/wallabag/README.md index 5bc35be1..3f930564 100644 --- a/plugins/wallabag/README.md +++ b/plugins/wallabag/README.md | |||
@@ -12,31 +12,26 @@ The directory structure should look like: | |||
12 | └── plugins | 12 | └── plugins |
13 | └── wallabag | 13 | └── wallabag |
14 | ├── README.md | 14 | ├── README.md |
15 | ├── config.php.dist | ||
16 | ├── wallabag.html | 15 | ├── wallabag.html |
16 | ├── wallabag.meta | ||
17 | ├── wallabag.php | 17 | ├── wallabag.php |
18 | └── wallabag.png | 18 | ├── wallabag.php |
19 | └── WallabagInstance.php | ||
19 | ``` | 20 | ``` |
20 | 21 | ||
21 | To enable the plugin, add `'wallabag'` to your list of enabled plugins in `data/options.php` (`PLUGINS` array). | 22 | To enable the plugin, you can either: |
22 | This should look like: | ||
23 | 23 | ||
24 | ``` | 24 | * enable it in the plugins administration page (`?do=pluginadmin`). |
25 | $GLOBALS['config']['PLUGINS'] = array('qrcode', 'any_other_plugin', 'wallabag') | 25 | * add `wallabag` to your list of enabled plugins in `data/config.json.php` (`general.enabled_plugins` section). |
26 | ``` | ||
27 | 26 | ||
28 | ### Configuration | 27 | ### Configuration |
29 | 28 | ||
30 | Copy `config.php.dist` into `config.php` and setup your instance. | 29 | Go to the plugin administration page, and edit the following settings (with the plugin enabled). |
31 | 30 | ||
32 | *Wallabag instance URL* | 31 | **WALLABAG_URL**: *Wallabag instance URL* |
33 | ``` | 32 | Example value: `http://v2.wallabag.org` |
34 | $GLOBALS['config']['WALLABAG_URL'] = 'http://v2.wallabag.org' ; | ||
35 | ``` | ||
36 | 33 | ||
37 | *Wallabag version*: either `1` (for 1.x) or `2` (for 2.x) | 34 | **WALLABAG_VERSION**: *Wallabag version* |
38 | ``` | 35 | Value: either `1` (for 1.x) or `2` (for 2.x) |
39 | $GLOBALS['config']['WALLABAG_VERSION'] = 2; | ||
40 | ``` | ||
41 | 36 | ||
42 | > Note: these settings can also be set in `data/config.php`. \ No newline at end of file | 37 | > 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 index a602708f..00000000 --- a/plugins/wallabag/config.php.dist +++ /dev/null | |||
@@ -1,4 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | $GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org'; | ||
4 | $GLOBALS['plugins']['WALLABAG_VERSION'] = 1; \ No newline at end of file | ||
diff --git a/plugins/wallabag/wallabag.php b/plugins/wallabag/wallabag.php index 4726d936..ec09c8a1 100644 --- a/plugins/wallabag/wallabag.php +++ b/plugins/wallabag/wallabag.php | |||
@@ -6,24 +6,22 @@ | |||
6 | 6 | ||
7 | require_once 'WallabagInstance.php'; | 7 | require_once 'WallabagInstance.php'; |
8 | 8 | ||
9 | $conf = ConfigManager::getInstance(); | ||
10 | $wallabagUrl = $conf->get('plugins.WALLABAG_URL'); | 9 | $wallabagUrl = $conf->get('plugins.WALLABAG_URL'); |
11 | if (empty($wallabagUrl)) { | 10 | if (empty($wallabagUrl)) { |
12 | $GLOBALS['plugin_errors'][] = 'Wallabag plugin error: '. | 11 | $GLOBALS['plugin_errors'][] = 'Wallabag plugin error: '. |
13 | 'Please define "$GLOBALS[\'plugins\'][\'WALLABAG_URL\']" '. | 12 | 'Please define the "WALLABAG_URL" setting in the plugin administration page.'; |
14 | 'in "plugins/wallabag/config.php" or in your Shaarli config.php file.'; | ||
15 | } | 13 | } |
16 | 14 | ||
17 | /** | 15 | /** |
18 | * Add wallabag icon to link_plugin when rendering linklist. | 16 | * Add wallabag icon to link_plugin when rendering linklist. |
19 | * | 17 | * |
20 | * @param mixed $data - linklist data. | 18 | * @param mixed $data Linklist data. |
19 | * @param ConfigManager $conf Configuration Manager instance. | ||
21 | * | 20 | * |
22 | * @return mixed - linklist data with wallabag plugin. | 21 | * @return mixed - linklist data with wallabag plugin. |
23 | */ | 22 | */ |
24 | function hook_wallabag_render_linklist($data) | 23 | function hook_wallabag_render_linklist($data, $conf) |
25 | { | 24 | { |
26 | $conf = ConfigManager::getInstance(); | ||
27 | $wallabagUrl = $conf->get('plugins.WALLABAG_URL'); | 25 | $wallabagUrl = $conf->get('plugins.WALLABAG_URL'); |
28 | if (empty($wallabagUrl)) { | 26 | if (empty($wallabagUrl)) { |
29 | return $data; | 27 | return $data; |
diff --git a/tests/PluginManagerTest.php b/tests/PluginManagerTest.php index 348082c7..61efce68 100644 --- a/tests/PluginManagerTest.php +++ b/tests/PluginManagerTest.php | |||
@@ -24,29 +24,38 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase | |||
24 | private static $pluginName = 'test'; | 24 | private static $pluginName = 'test'; |
25 | 25 | ||
26 | /** | 26 | /** |
27 | * @var PluginManager $pluginManager Plugin Mananger instance. | ||
28 | */ | ||
29 | protected $pluginManager; | ||
30 | |||
31 | public function setUp() | ||
32 | { | ||
33 | $conf = new ConfigManager(''); | ||
34 | $this->pluginManager = new PluginManager($conf); | ||
35 | } | ||
36 | |||
37 | /** | ||
27 | * Test plugin loading and hook execution. | 38 | * Test plugin loading and hook execution. |
28 | * | 39 | * |
29 | * @return void | 40 | * @return void |
30 | */ | 41 | */ |
31 | public function testPlugin() | 42 | public function testPlugin() |
32 | { | 43 | { |
33 | $pluginManager = PluginManager::getInstance(); | ||
34 | |||
35 | PluginManager::$PLUGINS_PATH = self::$pluginPath; | 44 | PluginManager::$PLUGINS_PATH = self::$pluginPath; |
36 | $pluginManager->load(array(self::$pluginName)); | 45 | $this->pluginManager->load(array(self::$pluginName)); |
37 | 46 | ||
38 | $this->assertTrue(function_exists('hook_test_random')); | 47 | $this->assertTrue(function_exists('hook_test_random')); |
39 | 48 | ||
40 | $data = array(0 => 'woot'); | 49 | $data = array(0 => 'woot'); |
41 | $pluginManager->executeHooks('random', $data); | 50 | $this->pluginManager->executeHooks('random', $data); |
42 | $this->assertEquals('woot', $data[1]); | 51 | $this->assertEquals('woot', $data[1]); |
43 | 52 | ||
44 | $data = array(0 => 'woot'); | 53 | $data = array(0 => 'woot'); |
45 | $pluginManager->executeHooks('random', $data, array('target' => 'test')); | 54 | $this->pluginManager->executeHooks('random', $data, array('target' => 'test')); |
46 | $this->assertEquals('page test', $data[1]); | 55 | $this->assertEquals('page test', $data[1]); |
47 | 56 | ||
48 | $data = array(0 => 'woot'); | 57 | $data = array(0 => 'woot'); |
49 | $pluginManager->executeHooks('random', $data, array('loggedin' => true)); | 58 | $this->pluginManager->executeHooks('random', $data, array('loggedin' => true)); |
50 | $this->assertEquals('loggedin', $data[1]); | 59 | $this->assertEquals('loggedin', $data[1]); |
51 | } | 60 | } |
52 | 61 | ||
@@ -57,11 +66,8 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase | |||
57 | */ | 66 | */ |
58 | public function testPluginNotFound() | 67 | public function testPluginNotFound() |
59 | { | 68 | { |
60 | $pluginManager = PluginManager::getInstance(); | 69 | $this->pluginManager->load(array()); |
61 | 70 | $this->pluginManager->load(array('nope', 'renope')); | |
62 | $pluginManager->load(array()); | ||
63 | |||
64 | $pluginManager->load(array('nope', 'renope')); | ||
65 | } | 71 | } |
66 | 72 | ||
67 | /** | 73 | /** |
@@ -69,16 +75,14 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase | |||
69 | */ | 75 | */ |
70 | public function testGetPluginsMeta() | 76 | public function testGetPluginsMeta() |
71 | { | 77 | { |
72 | $pluginManager = PluginManager::getInstance(); | ||
73 | |||
74 | PluginManager::$PLUGINS_PATH = self::$pluginPath; | 78 | PluginManager::$PLUGINS_PATH = self::$pluginPath; |
75 | $pluginManager->load(array(self::$pluginName)); | 79 | $this->pluginManager->load(array(self::$pluginName)); |
76 | 80 | ||
77 | $expectedParameters = array( | 81 | $expectedParameters = array( |
78 | 'pop' => '', | 82 | 'pop' => '', |
79 | 'hip' => '', | 83 | 'hip' => '', |
80 | ); | 84 | ); |
81 | $meta = $pluginManager->getPluginsMeta(); | 85 | $meta = $this->pluginManager->getPluginsMeta(); |
82 | $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); | 86 | $this->assertEquals('test plugin', $meta[self::$pluginName]['description']); |
83 | $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']); | 87 | $this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']); |
84 | } | 88 | } |
diff --git a/tests/plugins/PluginReadityourselfTest.php b/tests/plugins/PluginReadityourselfTest.php index bc5da042..d73e666a 100644 --- a/tests/plugins/PluginReadityourselfTest.php +++ b/tests/plugins/PluginReadityourselfTest.php | |||
@@ -4,6 +4,8 @@ | |||
4 | * PluginReadityourselfTest.php.php | 4 | * PluginReadityourselfTest.php.php |
5 | */ | 5 | */ |
6 | 6 | ||
7 | // FIXME! add an init method. | ||
8 | $conf = new ConfigManager(''); | ||
7 | require_once 'plugins/readityourself/readityourself.php'; | 9 | require_once 'plugins/readityourself/readityourself.php'; |
8 | 10 | ||
9 | /** | 11 | /** |
@@ -25,7 +27,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase | |||
25 | */ | 27 | */ |
26 | function testReadityourselfLinklist() | 28 | function testReadityourselfLinklist() |
27 | { | 29 | { |
28 | $conf = ConfigManager::getInstance(); | 30 | $conf = new ConfigManager(''); |
29 | $conf->set('plugins.READITYOUSELF_URL', 'value'); | 31 | $conf->set('plugins.READITYOUSELF_URL', 'value'); |
30 | $str = 'http://randomstr.com/test'; | 32 | $str = 'http://randomstr.com/test'; |
31 | $data = array( | 33 | $data = array( |
@@ -37,7 +39,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase | |||
37 | ) | 39 | ) |
38 | ); | 40 | ); |
39 | 41 | ||
40 | $data = hook_readityourself_render_linklist($data); | 42 | $data = hook_readityourself_render_linklist($data, $conf); |
41 | $link = $data['links'][0]; | 43 | $link = $data['links'][0]; |
42 | // data shouldn't be altered | 44 | // data shouldn't be altered |
43 | $this->assertEquals($str, $data['title']); | 45 | $this->assertEquals($str, $data['title']); |
@@ -53,7 +55,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase | |||
53 | */ | 55 | */ |
54 | function testReadityourselfLinklistWithoutConfig() | 56 | function testReadityourselfLinklistWithoutConfig() |
55 | { | 57 | { |
56 | $conf = ConfigManager::getInstance(); | 58 | $conf = new ConfigManager(''); |
57 | $conf->set('plugins.READITYOUSELF_URL', null); | 59 | $conf->set('plugins.READITYOUSELF_URL', null); |
58 | $str = 'http://randomstr.com/test'; | 60 | $str = 'http://randomstr.com/test'; |
59 | $data = array( | 61 | $data = array( |
@@ -65,7 +67,7 @@ class PluginReadityourselfTest extends PHPUnit_Framework_TestCase | |||
65 | ) | 67 | ) |
66 | ); | 68 | ); |
67 | 69 | ||
68 | $data = hook_readityourself_render_linklist($data); | 70 | $data = hook_readityourself_render_linklist($data, $conf); |
69 | $link = $data['links'][0]; | 71 | $link = $data['links'][0]; |
70 | // data shouldn't be altered | 72 | // data shouldn't be altered |
71 | $this->assertEquals($str, $data['title']); | 73 | $this->assertEquals($str, $data['title']); |
diff --git a/tests/plugins/PluginWallabagTest.php b/tests/plugins/PluginWallabagTest.php index e6f8a8b6..302ee296 100644 --- a/tests/plugins/PluginWallabagTest.php +++ b/tests/plugins/PluginWallabagTest.php | |||
@@ -4,6 +4,8 @@ | |||
4 | * PluginWallabagTest.php.php | 4 | * PluginWallabagTest.php.php |
5 | */ | 5 | */ |
6 | 6 | ||
7 | // FIXME! add an init method. | ||
8 | $conf = new ConfigManager(''); | ||
7 | require_once 'plugins/wallabag/wallabag.php'; | 9 | require_once 'plugins/wallabag/wallabag.php'; |
8 | 10 | ||
9 | /** | 11 | /** |
@@ -25,7 +27,7 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase | |||
25 | */ | 27 | */ |
26 | function testWallabagLinklist() | 28 | function testWallabagLinklist() |
27 | { | 29 | { |
28 | $conf = ConfigManager::getInstance(); | 30 | $conf = new ConfigManager(''); |
29 | $conf->set('plugins.WALLABAG_URL', 'value'); | 31 | $conf->set('plugins.WALLABAG_URL', 'value'); |
30 | $str = 'http://randomstr.com/test'; | 32 | $str = 'http://randomstr.com/test'; |
31 | $data = array( | 33 | $data = array( |
@@ -37,7 +39,7 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase | |||
37 | ) | 39 | ) |
38 | ); | 40 | ); |
39 | 41 | ||
40 | $data = hook_wallabag_render_linklist($data); | 42 | $data = hook_wallabag_render_linklist($data, $conf); |
41 | $link = $data['links'][0]; | 43 | $link = $data['links'][0]; |
42 | // data shouldn't be altered | 44 | // data shouldn't be altered |
43 | $this->assertEquals($str, $data['title']); | 45 | $this->assertEquals($str, $data['title']); |
@@ -49,4 +51,3 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase | |||
49 | $this->assertNotFalse(strpos($link['link_plugin'][0], $conf->get('plugins.WALLABAG_URL'))); | 51 | $this->assertNotFalse(strpos($link['link_plugin'][0], $conf->get('plugins.WALLABAG_URL'))); |
50 | } | 52 | } |
51 | } | 53 | } |
52 | |||