mirror of
https://github.com/Mibew/tray.git
synced 2025-01-22 18:10:34 +03:00
Modified PluginManager to store plugins objects and use objects with regular methods instead static one
This commit is contained in:
parent
69c54ba37c
commit
5bd6f22cab
@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -33,7 +33,7 @@ Class Phpunit_autotest_plugin_managerPlugin extends Plugin{
|
||||
}
|
||||
|
||||
public function __construct(){
|
||||
self::$initialized = true;
|
||||
$this->initialized = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
*
|
||||
@ -28,6 +46,9 @@ Class PluginManager {
|
||||
* $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'
|
||||
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user