From 69695e5c3f389c173ef21c3cfbbbbd66ba3e5a17 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Wed, 18 Jul 2012 12:29:28 +0000 Subject: [PATCH] Replaced plugin interface to plugin abstract class --- .../plugin.mibew.inc.php | 6 ++++- src/messenger/webim/libs/classes/plugin.php | 23 +++++++++++++++---- .../webim/libs/classes/plugin_manager.php | 19 ++++++++++----- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/messenger/tests/webim/libs/classes/phpunit_autotest_plugin_manager/plugin.mibew.inc.php b/src/messenger/tests/webim/libs/classes/phpunit_autotest_plugin_manager/plugin.mibew.inc.php index f125560f..98c21e9a 100644 --- a/src/messenger/tests/webim/libs/classes/phpunit_autotest_plugin_manager/plugin.mibew.inc.php +++ b/src/messenger/tests/webim/libs/classes/phpunit_autotest_plugin_manager/plugin.mibew.inc.php @@ -3,7 +3,7 @@ /** * Test plugin for PHPUnit tests */ -Class Phpunit_autotest_plugin_managerPlugin implements Plugin{ +Class Phpunit_autotest_plugin_managerPlugin extends Plugin{ public $eventsRegistered = false; public $listenersRegistered = false; @@ -32,6 +32,10 @@ Class Phpunit_autotest_plugin_managerPlugin implements Plugin{ throw new Exception(); } + public function __construct(){ + $this->initialized = true; + } + } ?> \ No newline at end of file diff --git a/src/messenger/webim/libs/classes/plugin.php b/src/messenger/webim/libs/classes/plugin.php index 62b7e9ac..7b67029e 100644 --- a/src/messenger/webim/libs/classes/plugin.php +++ b/src/messenger/webim/libs/classes/plugin.php @@ -16,9 +16,22 @@ */ /** - * Base plugin interface + * Base plugin class */ -interface Plugin { +abstract Class Plugin { + + /** + * Constructor must set this value to true after successful initialization + * failures + * @var boolean + */ + public $initialized = false; + + /** + * An array of plugin configuration + * @var array + */ + public static $config = array(); /** * Returns plugin weight. Weight is used for determine loading order and as default @@ -26,17 +39,17 @@ interface Plugin { * * @return int Plugin weight */ - public function getWeight(); + abstract public function getWeight(); /** * Register events */ - public function registerEvents(); + abstract public function registerEvents(); /** * Register listeners */ - public function registerListeners(); + abstract public function registerListeners(); } diff --git a/src/messenger/webim/libs/classes/plugin_manager.php b/src/messenger/webim/libs/classes/plugin_manager.php index bc3cbd47..f653d46f 100644 --- a/src/messenger/webim/libs/classes/plugin_manager.php +++ b/src/messenger/webim/libs/classes/plugin_manager.php @@ -67,19 +67,26 @@ Class PluginManager { ); continue; } - // Check if plugin implements 'Plugin' interface - if (! in_array('Plugin', class_implements($plugin_classname))) { + // Check if plugin extends abstract 'Plugin' class + if ('Plugin' != get_parent_class($plugin_classname)) { trigger_error( - "Plugin class '{$plugin_classname}' does not implement " . - "'Plugin' interface!", + "Plugin class '{$plugin_classname}' does not extend " . + "abstract 'Plugin' class!", E_USER_WARNING ); continue; } // Add plugin to loading queue $plugin_instance = new $plugin_classname($plugin_config); - $loading_queue[$plugin_instance->getWeight() . "_" . $offset] = $plugin_instance; - $offset++; + if ($plugin_instance->initialized) { + $loading_queue[$plugin_instance->getWeight() . "_" . $offset] = $plugin_instance; + $offset++; + } else { + trigger_error( + "Plugin '{$plugin_name}' does not initialized correctly!", + E_USER_WARNING + ); + } } // Sort queue in order to plugins' weights uksort($loading_queue, 'strnatcmp');