Move plugins' dependences into their classes

This commit is contained in:
Dmitriy Simushev 2013-06-24 13:01:56 +00:00
parent 48ecf35f1a
commit 13c577d2ae
4 changed files with 68 additions and 26 deletions

View File

@ -24,7 +24,7 @@ class PluginManagerTest extends PHPUnit_Framework_TestCase {
) )
); );
$this->fail("Exception must be thrown"); $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 // Try to load plugin with an absent plugin in dependences list
// Following code wait for trigger user warning, which converts by PHPUnit to an // 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( PluginManager::loadPlugins(
array( array(
array( array(
'name' => 'phpunit_autotest_plugin_manager', 'name' => 'phpunit_autotest_plugin_manager_dependence'
'dependences' => array('missed_plugin')
) )
) )
); );
$this->fail("Exception must be thrown"); $this->fail("Exception must be thrown");
} catch(Exception $e) {} } catch(PHPUnit_Framework_Error_Warning $e) {}
// Try to load correct plugin // Try to load correct plugin
PluginManager::loadPlugins( PluginManager::loadPlugins(

View File

@ -0,0 +1,26 @@
<?php
/**
* Test plugin for PHPUnit tests
*/
Class PhpunitAutotestPluginManagerDependencePlugin extends Plugin{
public $listenersRegistered = false;
public function getWeight() {
return 10;
}
public function registerListeners() {}
public static function getDependences() {
return array('some_missed_dependence');
}
public function __construct(){
$this->initialized = true;
}
}
?>

View File

@ -53,6 +53,17 @@ abstract Class Plugin {
*/ */
abstract public function registerListeners(); 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();
}
} }
?> ?>

View File

@ -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:
* <code> * <code>
* $plugins_list = array(); * $plugins_list = array();
* $plugins_list[] = array( * $plugins_list[] = array(
* 'name' => 'plugin_name', // Obligatory value * 'name' => 'plugin_name', // Obligatory value
* 'dependences' => array( // Dependences of the plugin
* 'another_plugin_name'
* ),
* 'config' => array( // Pass to plugin constructor * 'config' => array( // Pass to plugin constructor
* 'weight' => 100, * 'weight' => 100,
* 'some_configurable_value' => 'value' * 'some_configurable_value' => 'value'
@ -71,7 +72,6 @@ Class PluginManager {
* ) * )
* </code> * </code>
* *
* @see Plugin::registerEvents()
* @see Plugin::registerListeners() * @see Plugin::registerListeners()
*/ */
public static function loadPlugins($plugins_list){ public static function loadPlugins($plugins_list){
@ -90,23 +90,12 @@ 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'] // Build name of the plugin class
: array();
$plugin_name_parts = explode('_', $plugin_name); $plugin_name_parts = explode('_', $plugin_name);
$plugin_name_parts = array_map('ucfirst', $plugin_name_parts); $plugin_name_parts = array_map('ucfirst', $plugin_name_parts);
$plugin_classname = implode('', $plugin_name_parts) . "Plugin"; $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 // 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);
@ -114,7 +103,7 @@ Class PluginManager {
// Check plugin class name // Check plugin class name
if (! class_exists($plugin_classname)) { if (! class_exists($plugin_classname)) {
trigger_error( trigger_error(
"Plugin class '{$plugin_classname}' does not defined!", "Plugin class '{$plugin_classname}' is undefined!",
E_USER_WARNING E_USER_WARNING
); );
continue; continue;
@ -128,6 +117,23 @@ Class PluginManager {
); );
continue; 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 // Add plugin to loading queue
$plugin_instance = new $plugin_classname($plugin_config); $plugin_instance = new $plugin_classname($plugin_config);
if ($plugin_instance->initialized) { if ($plugin_instance->initialized) {
@ -137,7 +143,7 @@ Class PluginManager {
$offset++; $offset++;
} else { } else {
trigger_error( trigger_error(
"Plugin '{$plugin_name}' does not initialized correctly!", "Plugin '{$plugin_name}' was not initialized correctly!",
E_USER_WARNING E_USER_WARNING
); );
} }