From 13c577d2ae4a2f23667fa34748e317ec785a0e99 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Mon, 24 Jun 2013 13:01:56 +0000 Subject: [PATCH] Move plugins' dependences into their classes --- .../webim/libs/classes/PluginManagerTest.php | 7 ++- ...otest_plugin_manager_dependence_plugin.php | 26 ++++++++++ src/messenger/webim/libs/classes/plugin.php | 11 ++++ .../webim/libs/classes/plugin_manager.php | 50 +++++++++++-------- 4 files changed, 68 insertions(+), 26 deletions(-) create mode 100644 src/messenger/tests/server_side/webim/plugins/phpunit_autotest_plugin_manager_dependence/phpunit_autotest_plugin_manager_dependence_plugin.php diff --git a/src/messenger/tests/server_side/webim/libs/classes/PluginManagerTest.php b/src/messenger/tests/server_side/webim/libs/classes/PluginManagerTest.php index 42259aaf..84e69ee2 100644 --- a/src/messenger/tests/server_side/webim/libs/classes/PluginManagerTest.php +++ b/src/messenger/tests/server_side/webim/libs/classes/PluginManagerTest.php @@ -24,7 +24,7 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase { ) ); $this->fail("Exception must be thrown"); - } catch(Exception $e) {} + } catch(PHPUnit_Framework_Error_Warning $e) {} // Try to load plugin with an absent plugin in dependences list // Following code wait for trigger user warning, which converts by PHPUnit to an @@ -33,13 +33,12 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase { PluginManager::loadPlugins( array( array( - 'name' => 'phpunit_autotest_plugin_manager', - 'dependences' => array('missed_plugin') + 'name' => 'phpunit_autotest_plugin_manager_dependence' ) ) ); $this->fail("Exception must be thrown"); - } catch(Exception $e) {} + } catch(PHPUnit_Framework_Error_Warning $e) {} // Try to load correct plugin PluginManager::loadPlugins( diff --git a/src/messenger/tests/server_side/webim/plugins/phpunit_autotest_plugin_manager_dependence/phpunit_autotest_plugin_manager_dependence_plugin.php b/src/messenger/tests/server_side/webim/plugins/phpunit_autotest_plugin_manager_dependence/phpunit_autotest_plugin_manager_dependence_plugin.php new file mode 100644 index 00000000..28e8adc2 --- /dev/null +++ b/src/messenger/tests/server_side/webim/plugins/phpunit_autotest_plugin_manager_dependence/phpunit_autotest_plugin_manager_dependence_plugin.php @@ -0,0 +1,26 @@ +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 0016264b..c0a41cd6 100644 --- a/src/messenger/webim/libs/classes/plugin.php +++ b/src/messenger/webim/libs/classes/plugin.php @@ -53,6 +53,17 @@ abstract Class Plugin { */ abstract public function registerListeners(); + /** + * Returns list of plugin's dependences. + * + * Each element of dependenses list is a string with a plugin name. + * If plugin have no dependenses do not override this method. + * + * @return array List of plugin's dependences. + */ + public static function getDependences() { + return array(); + } } ?> \ No newline at end of file diff --git a/src/messenger/webim/libs/classes/plugin_manager.php b/src/messenger/webim/libs/classes/plugin_manager.php index a47dac64..345cceb1 100644 --- a/src/messenger/webim/libs/classes/plugin_manager.php +++ b/src/messenger/webim/libs/classes/plugin_manager.php @@ -54,16 +54,17 @@ Class PluginManager { } /** - * Loads plugins and invokes Plugin::registerEvents() and Plugin::registerListeners() + * Loads plugins. * - * @param array $plugins_list List of plugins' names and configurations. For example: + * The method checks dependences and plugin avaiulability before loading and + * invokes Plugin::registerListeners() after loading. + * + * @param array $plugins_list List of plugins' names and configurations. + * For example: * * $plugins_list = array(); * $plugins_list[] = array( * 'name' => 'plugin_name', // Obligatory value - * 'dependences' => array( // Dependences of the plugin - * 'another_plugin_name' - * ), * 'config' => array( // Pass to plugin constructor * 'weight' => 100, * 'some_configurable_value' => 'value' @@ -71,7 +72,6 @@ Class PluginManager { * ) * * - * @see Plugin::registerEvents() * @see Plugin::registerListeners() */ public static function loadPlugins($plugins_list){ @@ -90,23 +90,12 @@ Class PluginManager { } $plugin_name = $plugin['name']; $plugin_config = isset($plugin['config']) ? $plugin['config'] : array(); - $plugin_dependences = isset($plugin['dependences']) - ? $plugin['dependences'] - : array(); + + // Build name of the plugin class $plugin_name_parts = explode('_', $plugin_name); $plugin_name_parts = array_map('ucfirst', $plugin_name_parts); $plugin_classname = implode('', $plugin_name_parts) . "Plugin"; - // Check plugin dependences - foreach ($plugin_dependences as $dependence) { - if (empty(self::$loaded_plugins[$dependence])) { - trigger_error( - "Plugin '{$dependence}' not loaded yet, but " . - "exists in '{$plugin_name}' dependences list!", - E_USER_WARNING - ); - continue 2; - } - } + // Try to load plugin file if (! (include_once $plugin_name."/".$plugin_name."_plugin.php")) { trigger_error("Cannot load plugin file!", E_USER_ERROR); @@ -114,7 +103,7 @@ Class PluginManager { // Check plugin class name if (! class_exists($plugin_classname)) { trigger_error( - "Plugin class '{$plugin_classname}' does not defined!", + "Plugin class '{$plugin_classname}' is undefined!", E_USER_WARNING ); continue; @@ -128,6 +117,23 @@ Class PluginManager { ); continue; } + + // Check plugin dependences + $plugin_dependences = call_user_func(array( + $plugin_classname, + 'getDependences' + )); + foreach ($plugin_dependences as $dependence) { + if (empty(self::$loaded_plugins[$dependence])) { + trigger_error( + "Plugin '{$dependence}' was not loaded yet, but " . + "exists in '{$plugin_name}' dependences list!", + E_USER_WARNING + ); + continue 2; + } + } + // Add plugin to loading queue $plugin_instance = new $plugin_classname($plugin_config); if ($plugin_instance->initialized) { @@ -137,7 +143,7 @@ Class PluginManager { $offset++; } else { trigger_error( - "Plugin '{$plugin_name}' does not initialized correctly!", + "Plugin '{$plugin_name}' was not initialized correctly!", E_USER_WARNING ); }