From: Gunnar Wrobel
Date: Wed, 15 Dec 2010 13:32:07 +0000 (+0100)
Subject: Add the modules handler.
X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=9837b0e496ec49eac3f7ebb7b97359bb6cbf7727;p=horde.git
Add the modules handler.
---
diff --git a/framework/Cli_Modular/lib/Horde/Cli/Modular.php b/framework/Cli_Modular/lib/Horde/Cli/Modular.php
index 738c8d9ed..aafc0cd61 100644
--- a/framework/Cli_Modular/lib/Horde/Cli/Modular.php
+++ b/framework/Cli_Modular/lib/Horde/Cli/Modular.php
@@ -43,6 +43,11 @@ class Horde_Cli_Modular
* - parser
* - class: Class name of the parser that should be used to parse
* command line arguments.
+ * - modules: Determines the handler for modules. Can be one of:
+ * (array) A parameter array.
+ * See Horde_Cli_Modular_Modules::__construct()
+ * (string) A class name.
+ * (object) An instance of Horde_Cli_Modular_Modules
*
*/
public function __construct(array $parameters = null)
@@ -78,4 +83,30 @@ class Horde_Cli_Modular
)
);
}
+
+ /**
+ * Create the module handler.
+ *
+ * @return Horde_Cli_Modular_Modules The module handler.
+ */
+ public function createModules()
+ {
+ if (is_array($this->_parameters['modules'])) {
+ return new Horde_Cli_Modular_Modules(
+ $this->_parameters['modules']
+ );
+ } else if ($this->_parameters['modules'] instanceOf Horde_Cli_Modular_Modules) {
+ return $this->_parameters['modules'];
+ } else if (is_string($this->_parameters['modules'])) {
+ return new $this->_parameters['modules']();
+ } else if (empty($this->_parameters['modules'])) {
+ throw new Horde_Cli_Modular_Exception(
+ 'Missing "modules" parameter!'
+ );
+ } else {
+ throw new Horde_Cli_Modular_Exception(
+ 'Invalid "modules" parameter!'
+ );
+ }
+ }
}
\ No newline at end of file
diff --git a/framework/Cli_Modular/lib/Horde/Cli/Modular/Exception.php b/framework/Cli_Modular/lib/Horde/Cli/Modular/Exception.php
new file mode 100644
index 000000000..42d93f2a9
--- /dev/null
+++ b/framework/Cli_Modular/lib/Horde/Cli/Modular/Exception.php
@@ -0,0 +1,30 @@
+
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Cli_Modular
+ */
+
+/**
+ * This class provides the standard error class for exceptions in this package.
+ *
+ * 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 Cli_Modular
+ * @author Gunnar Wrobel
+ * -
+ *
+ */
+ public function __construct(array $parameters = null)
+ {
+ $this->_parameters = $parameters;
+ $this->_initModules();
+ }
+
+ /**
+ * Initialize the list of module class names.
+ *
+ * @return NULL
+ *
+ * @throws Horde_Cli_Modular_Exception In case the list of modules could not
+ * be established.
+ */
+ private function _initModules()
+ {
+ if (empty($this->_parameters['directory'])) {
+ throw new Horde_Cli_Modular_Exception(
+ 'The "directory" parameter is missing!'
+ );
+ }
+ if (!file_exists($this->_parameters['directory'])) {
+ throw new Horde_Cli_Modular_Exception(
+ sprintf(
+ 'The indicated directory %s does not exist!',
+ $this->_parameters['directory']
+ )
+ );
+ }
+ if (!isset($this->_parameters['exclude'])) {
+ $this->_parameters['exclude'] = array();
+ } else if (!is_array($this->_parameters['exclude'])) {
+ $this->_parameters['exclude'] = array($this->_parameters['exclude']);
+ }
+ foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->_parameters['directory'])) as $file) {
+ if ($file->isFile() && preg_match('/.php$/', $file->getFilename())) {
+ $class = preg_replace("/^(.*)\.php/", '\\1', $file->getFilename());
+ if (!in_array($class, $this->_parameters['exclude'])) {
+ $this->_modules[] = $class;
+ }
+ }
+ }
+ }
+
+ /**
+ * List the available modules.
+ *
+ * @return array The list of modules.
+ */
+ public function listModules()
+ {
+ return $this->_modules;
+ }
+
+ /**
+ * 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 Cli_Modular_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/framework/Cli_Modular/package.xml b/framework/Cli_Modular/package.xml
index 7a7bbc035..a330a48a1 100644
--- a/framework/Cli_Modular/package.xml
+++ b/framework/Cli_Modular/package.xml
@@ -17,7 +17,7 @@