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 + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Cli_Modular + */ +class Horde_Cli_Modular_Exception extends Horde_Exception_Prior +{ +} \ No newline at end of file diff --git a/framework/Cli_Modular/lib/Horde/Cli/Modular/Modules.php b/framework/Cli_Modular/lib/Horde/Cli/Modular/Modules.php new file mode 100644 index 000000000..8d4129431 --- /dev/null +++ b/framework/Cli_Modular/lib/Horde/Cli/Modular/Modules.php @@ -0,0 +1,174 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Cli_Modular + */ + +/** + * The Horde_Cli_Modular_Modules:: class handles a set of CLI 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 Cli_Modular + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Cli_Modular + */ +class Horde_Cli_Modular_Modules +implements Iterator, Countable +{ + /** + * Parameters. + * + * @var array + */ + private $_parameters; + + /** + * The available modules. + * + * @var array + */ + private $_modules; + + /** + * The dependency provider. + * + * @var Cli_Modular_Dependencies + */ + private $_dependencies; + + /** + * Constructor. + * + * @param array $parameters Options for this instance. + *
+     *  - 
+     * 
+ */ + 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 @@ yes 2010-12-15 - + 0.0.1 0.0.1 @@ -35,7 +35,10 @@ - + + + + @@ -44,8 +47,18 @@ + + + + + + + + + + @@ -70,13 +83,18 @@ - + + + + + + diff --git a/framework/Cli_Modular/test/Horde/Cli/Modular/Autoload.php b/framework/Cli_Modular/test/Horde/Cli/Modular/Autoload.php index 1d7365feb..e94f40936 100644 --- a/framework/Cli_Modular/test/Horde/Cli/Modular/Autoload.php +++ b/framework/Cli_Modular/test/Horde/Cli/Modular/Autoload.php @@ -24,3 +24,6 @@ error_reporting(E_ALL | E_STRICT); /** Load the basic test definition */ require_once dirname(__FILE__) . '/TestCase.php'; + +/** Load stub classes */ +require_once dirname(__FILE__) . '/Stub/Modules.php'; \ No newline at end of file diff --git a/framework/Cli_Modular/test/Horde/Cli/Modular/Stub/Modules.php b/framework/Cli_Modular/test/Horde/Cli/Modular/Stub/Modules.php new file mode 100644 index 000000000..c5595d02e --- /dev/null +++ b/framework/Cli_Modular/test/Horde/Cli/Modular/Stub/Modules.php @@ -0,0 +1,9 @@ +assertInstanceOf('Horde_Test_Stub_Parser', $modular->createParser()); } + + /** + * @expectedException Horde_Cli_Modular_Exception + */ + public function testMissingModules() + { + $modular = new Horde_Cli_Modular(); + $this->assertInstanceOf('Horde_Cli_Modular_Modules', $modular->createModules()); + } + + /** + * @expectedException Horde_Cli_Modular_Exception + */ + public function testInvalidModules() + { + $modular = new Horde_Cli_Modular(array('modules' => 1.0)); + $this->assertInstanceOf('Horde_Cli_Modular_Modules', $modular->createModules()); + } + + public function testObjectModules() + { + $modular = new Horde_Cli_Modular( + array('modules' => new Horde_Cli_Modular_Modules( + array( + 'directory' => dirname(__FILE__) . '/../fixtures/Module' + ) + ) + ) + ); + $this->assertInstanceOf('Horde_Cli_Modular_Modules', $modular->createModules()); + } + + public function testStringModules() + { + $modular = new Horde_Cli_Modular( + array( + 'modules' => 'Horde_Cli_Modular_Stub_Modules' + ) + ); + $this->assertInstanceOf('Horde_Cli_Modular_Modules', $modular->createModules()); + } + + public function testArrayModules() + { + $modular = new Horde_Cli_Modular( + array( + 'modules' => array( + 'directory' => dirname(__FILE__) . '/../fixtures/Module' + ), + ) + ); + $this->assertInstanceOf('Horde_Cli_Modular_Modules', $modular->createModules()); + } + } diff --git a/framework/Cli_Modular/test/Horde/Cli/Modular/Unit/ModulesTest.php b/framework/Cli_Modular/test/Horde/Cli/Modular/Unit/ModulesTest.php new file mode 100644 index 000000000..1908d4d33 --- /dev/null +++ b/framework/Cli_Modular/test/Horde/Cli/Modular/Unit/ModulesTest.php @@ -0,0 +1,95 @@ + + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Cli_Modular + */ + +/** + * Prepare the test setup. + */ +require_once dirname(__FILE__) . '/../Autoload.php'; + +/** + * Test the modules handler. + * + * 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 Kolab + * @package Cli_Modular + * @subpackage UnitTests + * @author Gunnar Wrobel + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @link http://pear.horde.org/index.php?package=Cli_Modular + */ +class Horde_Cli_Modular_Unit_ModulesTest +extends Horde_Cli_Modular_TestCase +{ + + /** + * @expectedException Horde_Cli_Modular_Exception + */ + public function testMissingDirectory() + { + $modules = new Horde_Cli_Modular_Modules(); + } + + /** + * @expectedException Horde_Cli_Modular_Exception + */ + public function testInvalidDirectory() + { + $modules = new Horde_Cli_Modular_Modules( + array('directory' => dirname(__FILE__) . '/DOES_NOT_EXIST') + ); + } + + public function testList() + { + $modules = new Horde_Cli_Modular_Modules( + array('directory' => dirname(__FILE__) . '/../fixtures/Module') + ); + $this->assertEquals(array('One', 'Two'), $modules->listModules()); + } + + public function testExclusion() + { + $modules = new Horde_Cli_Modular_Modules( + array( + 'directory' => dirname(__FILE__) . '/../fixtures/Module', + 'exclude' => 'One' + ) + ); + $this->assertEquals(array('Two'), $modules->listModules()); + } + + public function testIteration() + { + $modules = new Horde_Cli_Modular_Modules( + array('directory' => dirname(__FILE__) . '/../fixtures/Module') + ); + $result = array(); + foreach ($modules as $name => $module) { + $result[] = $module; + } + $this->assertEquals(array('One', 'Two'), $result); + } + + public function testCount() + { + $modules = new Horde_Cli_Modular_Modules( + array('directory' => dirname(__FILE__) . '/../fixtures/Module') + ); + $this->assertEquals(2, count($modules)); + } +} diff --git a/framework/Cli_Modular/test/Horde/Cli/Modular/fixtures/Module/One.php b/framework/Cli_Modular/test/Horde/Cli/Modular/fixtures/Module/One.php new file mode 100644 index 000000000..b3d9bbc7f --- /dev/null +++ b/framework/Cli_Modular/test/Horde/Cli/Modular/fixtures/Module/One.php @@ -0,0 +1 @@ +