Completed option parsing.
authorGunnar Wrobel <p@rdus.de>
Mon, 5 Jul 2010 03:13:28 +0000 (05:13 +0200)
committerGunnar Wrobel <p@rdus.de>
Mon, 23 Aug 2010 07:51:50 +0000 (09:51 +0200)
framework/Qc/lib/Horde/Qc.php
framework/Qc/lib/Horde/Qc/Config.php
framework/Qc/lib/Horde/Qc/Config/Cli.php
framework/Qc/lib/Horde/Qc/Configs.php [new file with mode: 0644]
framework/Qc/lib/Horde/Qc/Exception.php [new file with mode: 0644]
framework/Qc/package.xml
framework/Qc/test/Horde/Qc/StoryTestCase.php
framework/Qc/test/Horde/Qc/fixture/package.xml [new file with mode: 0644]

index da7a6f6..50c7994 100644 (file)
  */
 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.
+     * <pre>
+     * 'cli'        - (array)  CLI configuration parameters.
+     *   'parser'   - (array)  Parser configuration parameters.
+     *     'class'  - (string) The class name of the parser to use.
+     * </pre>
+     */
+    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
index 923199b..d486dfa 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /**
- * Horde_Qc_Config:: class represents configuration for the Horde quality
- * control tool.
+ * Horde_Qc_Config:: interface represents a configuration type for the Horde
+ * quality control tool.
  *
  * PHP version 5
  *
@@ -13,8 +13,8 @@
  */
 
 /**
- * Horde_Qc_Config:: class represents configuration for the Horde quality
- * control tool.
+ * Horde_Qc_Config:: interface represents a configuration type for the Horde
+ * quality control tool.
  *
  * Copyright 2009-2010 The Horde Project (http://www.horde.org/)
  *
  * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
  * @link     http://pear.horde.org/index.php?package=Qc
  */
-class Horde_Qc_Config
+interface Horde_Qc_Config
 {
-
-    /**
-     * The different configuration handlers.
-     *
-     * @var array
-     */
-    private $_configs;
-
-    /**
-     * Constructor.
-     *
-     * @param array            $parameters A list of named configuration parameters.
-     * <pre>
-     * 'cli' - (array)  See Horde_Qc_Config_Cli.
-     * </pre>
-     */
-    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
index 3a11585..4e6ae27 100644 (file)
@@ -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.
-     * <pre>
-     * 'parser' - (array)  Parser configuration parameters.
-     *   'class'  - (string) The class name of the parser to use.
-     * </pre>
      */
     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 (file)
index 0000000..2888faf
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+/**
+ * Horde_Qc_Configs:: class represents configuration for the Horde quality
+ * control tool.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Qc
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @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 <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Qc
+ */
+class Horde_Qc_Configs
+implements Horde_Qc_Config
+{
+
+    /**
+     * The different configuration handlers.
+     *
+     * @var array
+     */
+    private $_configs;
+
+    /**
+     * Constructor.
+     */
+    public function __construct() {
+        $this->_configs = array();
+    }
+
+    /**
+     * Add a configuration type to the configuration handler.
+     *
+     * @param Horde_Qc_Config $type The configuration type.
+     *
+     * @return NULL
+     */
+    public function addConfigurationType(Horde_Qc_Config $type) {
+        $this->_configs[] = $type;
+    }
+
+    /**
+     * 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);
+        }
+    }
+
+    /**
+     * 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;
+    }
+
+    /**
+     * Return the arguments provided by the configuration handlers.
+     *
+     * @return array An array of arguments.
+     */
+    public function getArguments()
+    {
+        $arguments = array();
+        foreach ($this->_configs as $config) {
+            $config_arguments = $config->getArguments();
+            if (!empty($config_arguments)) {
+                $arguments = array_merge($arguments, $config_arguments);
+            }
+        }
+        return $arguments;
+    }
+}
\ No newline at end of file
diff --git a/framework/Qc/lib/Horde/Qc/Exception.php b/framework/Qc/lib/Horde/Qc/Exception.php
new file mode 100644 (file)
index 0000000..9e8d004
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+/**
+ * This class provides the standard error class for the Qc package.
+ *
+ * PHP version 5
+ *
+ * @category Horde
+ * @package  Qc
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Qc
+ */
+
+/**
+ * This class provides the standard error class for the Qc package.
+ *
+ * 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  Qc
+ * @author   Gunnar Wrobel <wrobel@pardus.de>
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link     http://pear.horde.org/index.php?package=Qc
+ */
+class Horde_Qc_Exception
+extends Exception
+{
+}
index b3dce26..1296807 100644 (file)
@@ -48,6 +48,8 @@
        <file name="Cli.php" role="php" />
       </dir> <!-- /lib/Horde/Qc/Config -->
       <file name="Config.php" role="php" />
+      <file name="Configs.php" role="php" />
+      <file name="Exception.php" role="php" />
       <dir name="Module">
        <file name="PearPackageXml.php" role="php" />
       </dir> <!-- /lib/Horde/Qc/Module -->
@@ -93,6 +95,8 @@
    <install as="Horde/Qc/Autoloader.php" name="lib/Horde/Qc/Autoloader.php" />
    <install as="Horde/Qc/Config.php" name="lib/Horde/Qc/Config.php" />
    <install as="Horde/Qc/Config/Cli.php" name="lib/Horde/Qc/Config/Cli.php" />
+   <install as="Horde/Qc/Configs.php" name="lib/Horde/Qc/Configs.php" />
+   <install as="Horde/Qc/Exception.php" name="lib/Horde/Qc/Exception.php" />
    <install as="Horde/Qc/Module.php" name="lib/Horde/Qc/Module.php" />
    <install as="Horde/Qc/Module/PearPackageXml.php" name="lib/Horde/Qc/Module/PearPackageXml.php" />
    <install as="Horde/Qc/Modules.php" name="lib/Horde/Qc/Modules.php" />
index 8649540..63c4329 100644 (file)
@@ -62,10 +62,15 @@ extends PHPUnit_Extensions_Story_TestCase
     {
         switch($action) {
         case 'calling the package with the help option':
-            $_SERVER['argv'] = array('hqc', '--help', '--packagexml');
+            $_SERVER['argv'] = array(
+                'hqc',
+                '--help',
+                '--packagexml',
+                dirname(__FILE__) . '/fixture'
+            );
             ob_start();
             $parameters = array();
-            $parameters['config']['cli']['parser']['class'] = 'Horde_Qc_Stub_Parser';
+            $parameters['cli']['parser']['class'] = 'Horde_Qc_Stub_Parser';
             Horde_Qc::main($parameters);
             $world['output'] = ob_get_contents();
             ob_end_clean();
diff --git a/framework/Qc/test/Horde/Qc/fixture/package.xml b/framework/Qc/test/Horde/Qc/fixture/package.xml
new file mode 100644 (file)
index 0000000..e69de29