From: Gunnar Wrobel
Date: Wed, 15 Dec 2010 20:14:11 +0000 (+0100) Subject: Finish the module provider and complete the module definition. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=57561b2f38badf93291fed59fdbdac447a73faa6;p=horde.git Finish the module provider and complete the module definition. --- diff --git a/framework/Cli_Modular/lib/Horde/Cli/Modular.php b/framework/Cli_Modular/lib/Horde/Cli/Modular.php index ad81c3175..112472518 100644 --- a/framework/Cli_Modular/lib/Horde/Cli/Modular.php +++ b/framework/Cli_Modular/lib/Horde/Cli/Modular.php @@ -42,6 +42,13 @@ class Horde_Cli_Modular private $_modules; /** + * Module provider. + * + * @var Horde_Cli_Modular_ModuleProvider + */ + private $_provider; + + /** * Constructor. * * @param array $parameters Options for this instance. @@ -56,6 +63,11 @@ class Horde_Cli_Modular * See Horde_Cli_Modular_Modules::__construct() * (string) A class name. * (object) An instance of Horde_Cli_Modular_Modules + * - provider: Determines the module provider. Can be one of: + * (array) A parameter array. + * See Horde_Cli_Modular_ModuleProvider::__construct() + * (string) A class name. + * (object) An instance of Horde_Cli_Modular_ModuleProvider * */ public function __construct(array $parameters = null) @@ -90,7 +102,7 @@ class Horde_Cli_Modular $usage = $this->_parameters['cli']['parser']['usage']; } foreach ($this->getModules() as $module) { - $usage .= ''; + $usage .= $this->getProvider()->getModule($module)->getUsage(); } return $usage; } @@ -103,15 +115,33 @@ class Horde_Cli_Modular public function createParser() { $parser_class = $this->getParserClass(); - return new $parser_class( + $parser = new $parser_class( array( 'usage' => '%prog ' . $this->getUsage() ) ); + foreach ($this->getModules() as $module_name) { + $module = $this->getProvider()->getModule($module_name); + foreach ($module->getBaseOptions() as $option) { + $parser->addOption($option); + } + if ($module->hasOptionGroup()) { + $group = new Horde_Argv_OptionGroup( + $parser, + $module->getOptionGroupTitle(), + $module->getOptionGroupDescription() + ); + foreach ($module->getOptionGroupOptions() as $option) { + $group->addOption($option); + } + $parser->addOptionGroup($group); + } + } + return $parser; } /** - * Create the module handler. + * Return the module handler. * * @return Horde_Cli_Modular_Modules The module handler. */ @@ -148,4 +178,43 @@ class Horde_Cli_Modular ); } } + + /** + * Return the module provider. + * + * @return Horde_Cli_Modular_ModuleProvider The module provider. + */ + public function getProvider() + { + if ($this->_provider === null) { + $this->_provider = $this->_createProvider(); + } + return $this->_provider; + } + + /** + * Create the module provider. + * + * @return Horde_Cli_Modular_ModuleProvider The module provider. + */ + private function _createProvider() + { + if (is_array($this->_parameters['provider'])) { + return new Horde_Cli_Modular_ModuleProvider( + $this->_parameters['provider'] + ); + } else if ($this->_parameters['provider'] instanceOf Horde_Cli_Modular_ModuleProvider) { + return $this->_parameters['provider']; + } else if (is_string($this->_parameters['provider'])) { + return new $this->_parameters['provider'](); + } else if (empty($this->_parameters['provider'])) { + throw new Horde_Cli_Modular_Exception( + 'Missing "provider" parameter!' + ); + } else { + throw new Horde_Cli_Modular_Exception( + 'Invalid "provider" parameter!' + ); + } + } } \ No newline at end of file diff --git a/framework/Cli_Modular/lib/Horde/Cli/Modular/Module.php b/framework/Cli_Modular/lib/Horde/Cli/Modular/Module.php index 1b4837a65..d722a87cf 100644 --- a/framework/Cli_Modular/lib/Horde/Cli/Modular/Module.php +++ b/framework/Cli_Modular/lib/Horde/Cli/Modular/Module.php @@ -32,5 +32,41 @@ interface Horde_Cli_Modular_Module * * @return string The description. */ - static public function getUsage(); + public function getUsage(); + + /** + * Get a set of base options that this module adds to the CLI argument + * parser. + * + * @return array The options. + */ + public function getBaseOptions(); + + /** + * Indicate if the module provides an option group. + * + * @return boolean True if an option group should be added. + */ + public function hasOptionGroup(); + + /** + * Return the title for the option group representing this module. + * + * @return string The group title. + */ + public function getOptionGroupTitle(); + + /** + * Return the description for the option group representing this module. + * + * @return string The group description. + */ + public function getOptionGroupDescription(); + + /** + * Return the options for this module. + * + * @return array The group options. + */ + public function getOptionGroupOptions(); } \ No newline at end of file diff --git a/framework/Cli_Modular/lib/Horde/Cli/Modular/ModuleProvider.php b/framework/Cli_Modular/lib/Horde/Cli/Modular/ModuleProvider.php index 78949e799..7b27b1447 100644 --- a/framework/Cli_Modular/lib/Horde/Cli/Modular/ModuleProvider.php +++ b/framework/Cli_Modular/lib/Horde/Cli/Modular/ModuleProvider.php @@ -37,11 +37,27 @@ class Horde_Cli_Modular_ModuleProvider private $_prefix; /** + * Constructor argument for CLI modules. Likely to be a Horde_Injector + * instance. + * + * @var mixed + */ + private $_dependencies; + + /** + * A cache for initialized module instances. + * + * @var array + */ + private $_instances; + + /** * Constructor. * * @param array $parameters Options for this instance. *
- * -
+ * - prefix: The module class name prefix.
+ * - dependencies: Constructor argument for CLI modules.
*
*/
public function __construct(array $parameters = null)
@@ -52,29 +68,47 @@ class Horde_Cli_Modular_ModuleProvider
);
}
$this->_prefix = $parameters['prefix'];
+ if (isset($parameters['dependencies'])) {
+ $this->_dependencies = $parameters['dependencies'];
+ }
+ }
+
+ /**
+ * Return the specified module.
+ *
+ * @param string $module The desired module.
+ *
+ * @return Horde_Cli_Modular_Module The module instance.
+ *
+ * @throws Horde_Cli_Modular_Exception In case the specified module does not
+ * exist.
+ */
+ public function getModule($module)
+ {
+ if (!isset($this->_instances[$module])) {
+ $this->_instances[$module] = $this->createModule($module);
+ }
+ return $this->_instances[$module];
}
/**
- * Get the usage string for the specified module.
+ * Create the specified module.
*
* @param string $module The desired module.
*
- * @return string The usage description for this module.
+ * @return Horde_Cli_Modular_Module The module instance.
*
* @throws Horde_Cli_Modular_Exception In case the specified module does not
* exist.
*/
- public function getUsage($module)
+ protected function createModule($module)
{
- if (!class_exists($this->_prefix . $module)) {
+ $class = $this->_prefix . $module;
+ if (!class_exists($class)) {
throw new Horde_Cli_Modular_Exception(
- sprintf(
- 'Invalid module %s!', $this->_prefix . $module
- )
+ sprintf('Invalid module %s!', $class)
);
}
- return call_user_func_array(
- array($this->_prefix . $module, 'getUsage'), array()
- );
+ return new $class();
}
}
\ No newline at end of file
diff --git a/framework/Cli_Modular/package.xml b/framework/Cli_Modular/package.xml
index ea46cb3a9..f810662d3 100644
--- a/framework/Cli_Modular/package.xml
+++ b/framework/Cli_Modular/package.xml
@@ -85,7 +85,17 @@