From 5bd6f22cab23b7c39cb5621a6fd981dbf6d52a55 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Wed, 1 Aug 2012 09:19:26 +0000 Subject: [PATCH] Modified PluginManager to store plugins objects and use objects with regular methods instead static one --- .../webim/libs/classes/PluginManagerTest.php | 48 +++++++++++++++++++ ...phpunit_autotest_plugin_manager_plugin.php | 2 +- src/messenger/webim/libs/classes/plugin.php | 4 +- .../webim/libs/classes/plugin_manager.php | 43 +++++++++++++++-- 4 files changed, 91 insertions(+), 6 deletions(-) diff --git a/src/messenger/tests/webim/libs/classes/PluginManagerTest.php b/src/messenger/tests/webim/libs/classes/PluginManagerTest.php index 1eaf8d5e..0e16aafb 100644 --- a/src/messenger/tests/webim/libs/classes/PluginManagerTest.php +++ b/src/messenger/tests/webim/libs/classes/PluginManagerTest.php @@ -11,6 +11,37 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase { public function testLoadPlugins() { set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__)); + + // Try to load plugin that does not exists + // Following code wait for trigger user error, which converts by PHPUnit to an + // Exception + try { + PluginManager::loadPlugins( + array( + array( + 'name' => 'missed_plugin' + ) + ) + ); + $this->fail("Exception must be thrown"); + } catch(Exception $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 + // Exception + try { + PluginManager::loadPlugins( + array( + array( + 'name' => 'phpunit_autotest_plugin_manager', + 'dependences' => array('missed_plugin') + ) + ) + ); + $this->fail("Exception must be thrown"); + } catch(Exception $e) {} + + // Try to load correct plugin PluginManager::loadPlugins( array( array( @@ -18,11 +49,28 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase { ) ) ); + + // Check if plugin initialized correctry if(empty($GLOBALS['phpunit_autotest_plugin_manager'])) { $this->fail('Plugin not loaded and initialize correctly'); } } + /** + * @depends testLoadPlugins + */ + public function testGetPlugin() { + // Try to get plugin with wrong name + // Following code wait for trigger user warning, which converts by PHPUnit to an + // Exception + try { + PluginManager::getPlugin('wrong_plugin_name'); + $this->fail("Exception must be thrown"); + } catch(Exception $e) {} + + // Try to get loaded plugin + PluginManager::getPlugin('phpunit_autotest_plugin_manager'); + } } ?> diff --git a/src/messenger/tests/webim/libs/classes/phpunit_autotest_plugin_manager/phpunit_autotest_plugin_manager_plugin.php b/src/messenger/tests/webim/libs/classes/phpunit_autotest_plugin_manager/phpunit_autotest_plugin_manager_plugin.php index 5dea2646..7cbff02f 100644 --- a/src/messenger/tests/webim/libs/classes/phpunit_autotest_plugin_manager/phpunit_autotest_plugin_manager_plugin.php +++ b/src/messenger/tests/webim/libs/classes/phpunit_autotest_plugin_manager/phpunit_autotest_plugin_manager_plugin.php @@ -33,7 +33,7 @@ Class Phpunit_autotest_plugin_managerPlugin extends Plugin{ } public function __construct(){ - self::$initialized = true; + $this->initialized = true; } } diff --git a/src/messenger/webim/libs/classes/plugin.php b/src/messenger/webim/libs/classes/plugin.php index ca4be31d..5cf01797 100644 --- a/src/messenger/webim/libs/classes/plugin.php +++ b/src/messenger/webim/libs/classes/plugin.php @@ -25,13 +25,13 @@ abstract Class Plugin { * failures * @var boolean */ - public static $initialized = false; + public $initialized = false; /** * An array of plugin configuration * @var array */ - public static $config = array(); + protected $config = array(); /** * Returns plugin weight. Weight is used for determine loading order and as default diff --git a/src/messenger/webim/libs/classes/plugin_manager.php b/src/messenger/webim/libs/classes/plugin_manager.php index bb960d86..a6002745 100644 --- a/src/messenger/webim/libs/classes/plugin_manager.php +++ b/src/messenger/webim/libs/classes/plugin_manager.php @@ -20,6 +20,24 @@ */ Class PluginManager { + protected static $loaded_plugins = array(); + + /** + * Returns plugin object + * + * @param string $plugin_name + * @return Plugin + */ + public static function getPlugin($plugin_name) { + if (empty(self::$loaded_plugins[$plugin_name])) { + trigger_error( + "Plugin '{$plugin_name}' does not initialized!", + E_USER_WARNING + ); + } + return self::$loaded_plugins[$plugin_name]; + } + /** * Loads plugins and invokes Plugin::registerEvents() and Plugin::registerListeners() * @@ -27,8 +45,11 @@ Class PluginManager { * * $plugins_list = array(); * $plugins_list[] = array( - * 'name' => 'plugin_name', // Obligatory value - * 'config' => array( // Pass to plugin constructor + * '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' * ) @@ -54,7 +75,21 @@ Class PluginManager { } $plugin_name = $plugin['name']; $plugin_config = isset($plugin['config']) ? $plugin['config'] : array(); + $plugin_dependences = isset($plugin['dependences']) + ? $plugin['dependences'] + : array(); $plugin_classname = ucfirst($plugin_name) . "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); @@ -78,7 +113,9 @@ Class PluginManager { } // Add plugin to loading queue $plugin_instance = new $plugin_classname($plugin_config); - if ($plugin_classname::$initialized) { + if ($plugin_instance->initialized) { + // Store plugin instance + self::$loaded_plugins[$plugin_name] = $plugin_instance; $loading_queue[$plugin_instance->getWeight() . "_" . $offset] = $plugin_instance; $offset++; } else {