From: Gunnar Wrobel
Date: Mon, 5 Jul 2010 03:13:28 +0000 (+0200) Subject: Completed option parsing. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=cc0a793f4bc2d6ac67cbb2643ca4c6cbd16c490c;p=horde.git Completed option parsing. --- diff --git a/framework/Qc/lib/Horde/Qc.php b/framework/Qc/lib/Horde/Qc.php index da7a6f6ac..50c799419 100644 --- a/framework/Qc/lib/Horde/Qc.php +++ b/framework/Qc/lib/Horde/Qc.php @@ -29,17 +29,78 @@ */ class Horde_Qc { - static public function main($parameters = array()) + /** + * The main entry point for the application. + * + * @param array $parameters A list of named configuration parameters. + *
+ * 'cli' - (array) CLI configuration parameters. + * 'parser' - (array) Parser configuration parameters. + * 'class' - (string) The class name of the parser to use. + *+ */ + static public function main(array $parameters = array()) { - $modules = new Horde_Qc_Modules(); - if (!isset($parameters['config'])) { - $parameters['config'] = array(); - } - $config = new Horde_Qc_Config($parameters['config']); - $modules->addModulesFromDirectory(dirname(__FILE__) . '/Qc/Module'); + $parser = self::_prepareParser($parameters); + $config = self::_prepareConfig($parser); + $modules = self::_prepareModules(); $config->handleModules($modules); + try { + self::_validateArguments($config->getArguments()); + } catch (Horde_Qc_Exception $e) { + $parser->parserError($e->getMessage()); + return; + } + $options = $config->getOptions(); foreach ($modules as $module) { - $module->handle($config->getOptions()); + $module->handle($options); + } + } + + 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( + array( + 'usage' => '%prog ' . _("[options] PACKAGE_PATH") + ) + ); + } + + static private function _prepareConfig(Horde_Argv_Parser $parser) + { + $config = new Horde_Qc_Configs(); + $config->addConfigurationType( + new Horde_Qc_Config_Cli( + $parser + ) + ); + return $config; + } + + static private function _prepareModules() + { + $modules = new Horde_Qc_Modules(); + $modules->addModulesFromDirectory(dirname(__FILE__) . '/Qc/Module'); + return $modules; + } + + static private function _validateArguments(array $arguments) + { + if (empty($arguments[0])) { + throw new Horde_Qc_Exception('Please specify the path of the PEAR package!'); + } + + if (!is_dir($arguments[0])) { + throw new Horde_Qc_Exception(sprintf('%s specifies no directory!', $arguments[0])); + } + + if (!file_exists($arguments[0] . '/package.xml')) { + throw new Horde_Qc_Exception(sprintf('There is no package.xml at %s!', $arguments[0])); } } } \ No newline at end of file diff --git a/framework/Qc/lib/Horde/Qc/Config.php b/framework/Qc/lib/Horde/Qc/Config.php index 923199b5f..d486dfac8 100644 --- a/framework/Qc/lib/Horde/Qc/Config.php +++ b/framework/Qc/lib/Horde/Qc/Config.php @@ -1,7 +1,7 @@ - * 'cli' - (array) See Horde_Qc_Config_Cli. - * - */ - public function __construct( - $parameters = array() - ) { - if (!isset($parameters['cli'])) { - $parameters['cli'] = array(); - } - $this->_configs = array(); - $this->_configs[] = new Horde_Qc_Config_Cli( - $parameters['cli'] - ); - } - /** * Provide each configuration handler with the list of supported modules. * * @param Horde_Qc_Modules $modules A list of modules. * @return NULL */ - public function handleModules(Horde_Qc_Modules $modules) - { - foreach ($this->_configs as $config) { - $config->handleModules($modules); - } - } + public function handleModules(Horde_Qc_Modules $modules); /** - * Return the options provided by the configuration hadnlers. + * Return the options provided by the configuration handlers. * * @return array An array of options. */ - public function getOptions() - { - $options = array(); - foreach ($this->_configs as $config) { - if (count($config->getOptions()) !== 0) { - $config_options = array(); - foreach ($config->getOptions() as $name => $option) { - $config_options[$name] = $option; - } - $options = array_merge($options, $config_options); - } - } - return $options; - } + public function getOptions(); + /** + * Return the arguments provided by the configuration handlers. + * + * @return array An array of arguments. + */ + public function getArguments(); } \ No newline at end of file diff --git a/framework/Qc/lib/Horde/Qc/Config/Cli.php b/framework/Qc/lib/Horde/Qc/Config/Cli.php index 3a1158581..4e6ae275c 100644 --- a/framework/Qc/lib/Horde/Qc/Config/Cli.php +++ b/framework/Qc/lib/Horde/Qc/Config/Cli.php @@ -28,6 +28,7 @@ * @link http://pear.horde.org/index.php?package=Qc */ class Horde_Qc_Config_Cli +implements Horde_Qc_Config { /** * The command line argument parser. @@ -41,38 +42,23 @@ class Horde_Qc_Config_Cli * * @var array */ - private $_opts; + private $_options; /** * Any additional arguments parsed from the command line. * * @var array */ - private $_args; + private $_arguments; /** * Constructor. * - * @param array $parameters A list of named configuration parameters. - *
- * 'parser' - (array) Parser configuration parameters. - * 'class' - (string) The class name of the parser to use. - **/ public function __construct( - $parameters = array() + Horde_Argv_Parser $parser ) { - if (empty($parameters['parser']['class'])) { - $parser_class = 'Horde_Argv_Parser'; - } else { - $parser_class = $parameters['parser']['class']; - } - $this->_parser = new $parser_class( - array( - 'usage' => '%prog ' . _("[options] PACKAGE_PATH") - ) - ); - + $this->_parser = $parser; } /** @@ -87,17 +73,27 @@ class Horde_Qc_Config_Cli $this->_addOptionsFromModule($this->_parser, $module); } - list($this->_opts, $this->_args) = $this->_parser->parseArgs(); + list($this->_options, $this->_arguments) = $this->_parser->parseArgs(); } /** * Return the options parsed from the command line. * - * @return array An array of options. + * @return Horde_Argv_Values The option values. */ public function getOptions() { - return $this->_opts; + return $this->_options; + } + + /** + * Return the arguments parsed from the command line. + * + * @return array An array of arguments. + */ + public function getArguments() + { + return $this->_arguments; } /** diff --git a/framework/Qc/lib/Horde/Qc/Configs.php b/framework/Qc/lib/Horde/Qc/Configs.php new file mode 100644 index 000000000..2888faf96 --- /dev/null +++ b/framework/Qc/lib/Horde/Qc/Configs.php @@ -0,0 +1,108 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Qc + */ + +/** + * Horde_Qc_Configs:: class represents configuration for the Horde quality + * control tool. + * + * Copyright 2009-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 Qc + * @author Gunnar Wrobel