From 456d8a491173ef5211938933f351bfff8dc17698 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Mon, 10 Jan 2011 23:24:37 +0100 Subject: [PATCH] Convert to Cli_Modular. --- components/TODO | 2 - components/lib/Components.php | 43 ++++----- components/lib/Components/Config.php | 8 -- components/lib/Components/Config/Cli.php | 34 ------- components/lib/Components/Module.php | 23 +---- components/lib/Components/Module/Base.php | 31 ++++++ components/lib/Components/Modules.php | 137 --------------------------- components/package.xml | 2 - components/test/Components/StoryTestCase.php | 2 +- 9 files changed, 54 insertions(+), 228 deletions(-) delete mode 100644 components/lib/Components/Modules.php diff --git a/components/TODO b/components/TODO index b7da012a0..f5748cb80 100644 --- a/components/TODO +++ b/components/TODO @@ -56,5 +56,3 @@ - Allow absolute/relative paths as arguments - Fix double file task entry bug. - - - Convert to Cli_Modular diff --git a/components/lib/Components.php b/components/lib/Components.php index 3857768b4..2bfc4ece9 100644 --- a/components/lib/Components.php +++ b/components/lib/Components.php @@ -41,17 +41,16 @@ class Components */ static public function main(array $parameters = array()) { - $parser = self::_prepareParser($parameters); - $config = self::_prepareConfig($parser); if (isset($parameters['dependencies']) && $parameters['dependencies'] instanceOf Components_Dependencies) { $dependencies = $parameters['dependencies']; } else { $dependencies = new Components_Dependencies_Injector(); } + $modular = self::_prepareModular($dependencies, $parameters); + $parser = $modular->createParser(); + $config = self::_prepareConfig($parser); $dependencies->initConfig($config); - $modules = self::_prepareModules($dependencies); - $config->handleModules($modules); try { self::_validateArguments($config); } catch (Components_Exception $e) { @@ -59,8 +58,8 @@ class Components return; } try { - foreach ($modules as $module) { - $module->handle($config); + foreach ($modular->getModules() as $module) { + $modular->getProvider()->getModule($module)->handle($config); } } catch (Components_Exception $e) { $dependencies->getOutput()->fail($e); @@ -68,16 +67,23 @@ class Components } } - static private function _prepareParser(array $parameters = array()) - { - if (empty($parameters['cli']['parser']['class'])) { - $parser_class = 'Horde_Argv_Parser'; - } else { - $parser_class = $parameters['cli']['parser']['class']; - } - return new $parser_class( + static private function _prepareModular( + Components_Dependencies $dependencies, array $parameters = array() + ) { + return new Horde_Cli_Modular( array( - 'usage' => '%prog ' . _("[options] PACKAGE_PATH") + 'parser' => array( + 'class' => empty($parameters['parser']['class']) ? 'Horde_Argv_Parser' : $parameters['parser']['class'], + 'usage' => '%prog [options] PACKAGE_PATH' + ), + 'modules' => array( + 'directory' => dirname(__FILE__) . '/Components/Module', + 'exclude' => 'Base' + ), + 'provider' => array( + 'prefix' => 'Components_Module_', + 'dependencies' => $dependencies + ) ) ); } @@ -93,13 +99,6 @@ class Components return $config; } - static private function _prepareModules(Components_Dependencies $dependencies) - { - $modules = new Components_Modules($dependencies); - $modules->addModulesFromDirectory(dirname(__FILE__) . '/Components/Module'); - return $modules; - } - static private function _validateArguments(Components_Config $config) { $arguments = $config->getArguments(); diff --git a/components/lib/Components/Config.php b/components/lib/Components/Config.php index 2b681cbce..3158c593a 100644 --- a/components/lib/Components/Config.php +++ b/components/lib/Components/Config.php @@ -30,14 +30,6 @@ interface Components_Config { /** - * Provide each configuration handler with the list of supported modules. - * - * @param Components_Modules $modules A list of modules. - * @return NULL - */ - public function handleModules(Components_Modules $modules); - - /** * Return the options provided by the configuration handlers. * * @return array An array of options. diff --git a/components/lib/Components/Config/Cli.php b/components/lib/Components/Config/Cli.php index c9968cf62..758ad95a2 100644 --- a/components/lib/Components/Config/Cli.php +++ b/components/lib/Components/Config/Cli.php @@ -110,19 +110,6 @@ implements Components_Config ) ) ); - } - - /** - * Load the options for the list of supported modules. - * - * @param Components_Modules $modules A list of modules. - * @return NULL - */ - public function handleModules(Components_Modules $modules) - { - foreach ($modules as $module) { - $this->_addOptionsFromModule($this->_parser, $module); - } list($this->_options, $this->_arguments) = $this->_parser->parseArgs(); } @@ -158,25 +145,4 @@ implements Components_Config $arguments = $this->getArguments(); return $arguments[0]; } - - /** - * Add an option group from the provided module to the parser. - * - * @param Horde_Argv_Parser $parser The parser. - * @param Components_Module $module The module providing the option group. - * - * @return NULL - */ - private function _addOptionsFromModule($parser, $module) - { - $group = new Horde_Argv_OptionGroup( - $parser, - $module->getOptionGroupTitle(), - $module->getOptionGroupDescription() - ); - foreach ($module->getOptionGroupOptions() as $option) { - $group->addOption($option); - } - $parser->addOptionGroup($group); - } } diff --git a/components/lib/Components/Module.php b/components/lib/Components/Module.php index 1f76896a5..1f254ff8d 100644 --- a/components/lib/Components/Module.php +++ b/components/lib/Components/Module.php @@ -25,30 +25,9 @@ * @license http://www.fsf.org/copyleft/lgpl.html LGPL * @link http://pear.horde.org/index.php?package=Components */ -interface Components_Module +interface Components_Module extends Horde_Cli_Modular_Module { /** - * 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(); - - /** * Determine if this module should act. Run all required actions if it has * been instructed to do so. * diff --git a/components/lib/Components/Module/Base.php b/components/lib/Components/Module/Base.php index 54091ddb9..79310f607 100644 --- a/components/lib/Components/Module/Base.php +++ b/components/lib/Components/Module/Base.php @@ -48,6 +48,37 @@ implements Components_Module } /** + * Get the usage description for this module. + * + * @return string The description. + */ + public function getUsage() + { + return ''; + } + + /** + * Get a set of base options that this module adds to the CLI argument + * parser. + * + * @return array The options. + */ + public function getBaseOptions() + { + return array(); + } + + /** + * Indicate if the module provides an option group. + * + * @return boolean True if an option group should be added. + */ + public function hasOptionGroup() + { + return true; + } + + /** * Validate that there is a package.xml file in the provided directory. * * @param string $directory The package directory. diff --git a/components/lib/Components/Modules.php b/components/lib/Components/Modules.php deleted file mode 100644 index 6af57078d..000000000 --- a/components/lib/Components/Modules.php +++ /dev/null @@ -1,137 +0,0 @@ - - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Components - */ - -/** - * The Components_Modules:: class handles a set of Components modules. - * - * Copyright 2010 The Horde Project (http://www.horde.org/) - * - * See the enclosed file COPYING for license information (LGPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. - * - * @category Horde - * @package Components - * @author Gunnar Wrobel - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Components - */ -class Components_Modules -implements Iterator, Countable -{ - /** - * The available modules. - * - * @var array - */ - private $_modules; - - /** - * The dependency provider. - * - * @var Components_Dependencies - */ - private $_dependencies; - - /** - * Constructor. - * - * @param Components_Dependencies $dependencies The dependency provider. - */ - public function __construct(Components_Dependencies $dependencies) - { - $this->_dependencies = $dependencies; - $this->_modules = array(); - } - - /** - * Add all modules found in the specified directory. - * - * @param string $module_directory Load the modules from this directory. - * - * @return NULL - */ - public function addModulesFromDirectory( - $module_directory, - $base = 'Components_Module_' - ) { - foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($module_directory)) as $file) { - if ($file->isFile() && preg_match('/.php$/', $file->getFilename())) { - $class = $base . preg_replace("/^(.*)\.php/", '\\1', $file->getFilename()); - if ($class != 'Components_Module_Base') { - $this->_modules[$class] = new $class($this->_dependencies); - } - } - } - } - - /** - * Implementation of the Iterator rewind() method. Rewinds the module list. - * - * return NULL - */ - public function rewind() - { - reset($this->_modules); - } - - /** - * Implementation of the Iterator current(). Returns the current module. - * - * @return mixed The current module. - */ - public function current() - { - return current($this->_modules); - } - - /** - * Implementation of the Iterator key() method. Returns the key of the current module. - * - * @return mixed The class name of the current module. - */ - public function key() - { - return key($this->_modules); - } - - /** - * Implementation of the Iterator next() method. Returns the next module. - * - * @return Components_Module|null The next module or null if there are no more - * modules. - */ - public function next() - { - return next($this->_modules); - } - - /** - * Implementation of the Iterator valid() method. Indicates if the current element is a valid element. - * - * @return boolean Whether the current element is valid - */ - public function valid() - { - return key($this->_modules) !== null; - } - - /** - * Implementation of Countable count() method. Returns the number of modules. - * - * @return integer Number of modules. - */ - public function count() - { - return count($this->_modules); - } -} \ No newline at end of file diff --git a/components/package.xml b/components/package.xml index 7d195d9f6..74c829028 100644 --- a/components/package.xml +++ b/components/package.xml @@ -116,7 +116,6 @@ - @@ -293,7 +292,6 @@ - diff --git a/components/test/Components/StoryTestCase.php b/components/test/Components/StoryTestCase.php index 2b35925f1..5b69167c1 100644 --- a/components/test/Components/StoryTestCase.php +++ b/components/test/Components/StoryTestCase.php @@ -671,7 +671,7 @@ extends PHPUnit_Extensions_Story_TestCase private function _callComponents(array $parameters, $callback) { ob_start(); - $parameters['cli']['parser']['class'] = 'Horde_Test_Stub_Parser'; + $parameters['parser']['class'] = 'Horde_Test_Stub_Parser'; $parameters['dependencies'] = new Components_Dependencies_Injector(); $parameters['dependencies']->setInstance( 'Horde_Cli', -- 2.11.0