Modified PluginManager to store plugins objects and use objects with regular methods instead static one

This commit is contained in:
Dmitriy Simushev 2012-08-01 09:19:26 +00:00
parent 69c54ba37c
commit 5bd6f22cab
4 changed files with 91 additions and 6 deletions

View File

@ -11,6 +11,37 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase {
public function testLoadPlugins() { public function testLoadPlugins() {
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__)); 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( PluginManager::loadPlugins(
array( array(
array( array(
@ -18,11 +49,28 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase {
) )
) )
); );
// Check if plugin initialized correctry
if(empty($GLOBALS['phpunit_autotest_plugin_manager'])) { if(empty($GLOBALS['phpunit_autotest_plugin_manager'])) {
$this->fail('Plugin not loaded and initialize correctly'); $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');
}
} }
?> ?>

View File

@ -33,7 +33,7 @@ Class Phpunit_autotest_plugin_managerPlugin extends Plugin{
} }
public function __construct(){ public function __construct(){
self::$initialized = true; $this->initialized = true;
} }
} }

View File

@ -25,13 +25,13 @@ abstract Class Plugin {
* failures * failures
* @var boolean * @var boolean
*/ */
public static $initialized = false; public $initialized = false;
/** /**
* An array of plugin configuration * An array of plugin configuration
* @var array * @var array
*/ */
public static $config = array(); protected $config = array();
/** /**
* Returns plugin weight. Weight is used for determine loading order and as default * Returns plugin weight. Weight is used for determine loading order and as default

View File

@ -20,6 +20,24 @@
*/ */
Class PluginManager { 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() * Loads plugins and invokes Plugin::registerEvents() and Plugin::registerListeners()
* *
@ -27,8 +45,11 @@ Class PluginManager {
* <code> * <code>
* $plugins_list = array(); * $plugins_list = array();
* $plugins_list[] = array( * $plugins_list[] = array(
* 'name' => 'plugin_name', // Obligatory value * 'name' => 'plugin_name', // Obligatory value
* 'config' => array( // Pass to plugin constructor * 'dependences' => array( // Dependences of the plugin
* 'another_plugin_name'
* ),
* 'config' => array( // Pass to plugin constructor
* 'weight' => 100, * 'weight' => 100,
* 'some_configurable_value' => 'value' * 'some_configurable_value' => 'value'
* ) * )
@ -54,7 +75,21 @@ Class PluginManager {
} }
$plugin_name = $plugin['name']; $plugin_name = $plugin['name'];
$plugin_config = isset($plugin['config']) ? $plugin['config'] : array(); $plugin_config = isset($plugin['config']) ? $plugin['config'] : array();
$plugin_dependences = isset($plugin['dependences'])
? $plugin['dependences']
: array();
$plugin_classname = ucfirst($plugin_name) . "Plugin"; $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 // Try to load plugin file
if (! (include_once $plugin_name."/".$plugin_name."_plugin.php")) { if (! (include_once $plugin_name."/".$plugin_name."_plugin.php")) {
trigger_error("Cannot load plugin file!", E_USER_ERROR); trigger_error("Cannot load plugin file!", E_USER_ERROR);
@ -78,7 +113,9 @@ Class PluginManager {
} }
// Add plugin to loading queue // Add plugin to loading queue
$plugin_instance = new $plugin_classname($plugin_config); $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; $loading_queue[$plugin_instance->getWeight() . "_" . $offset] = $plugin_instance;
$offset++; $offset++;
} else { } else {