Overview

Packages

  • Cli
    • Modular

Classes

  • Horde_Cli_Modular
  • Horde_Cli_Modular_Exception
  • Horde_Cli_Modular_ModuleProvider
  • Horde_Cli_Modular_Modules

Interfaces

  • Horde_Cli_Modular_Module
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Horde_Cli_Modular_Modules:: class handles a set of CLI modules.
  4:  *
  5:  * PHP version 5
  6:  *
  7:  * @category Horde
  8:  * @package  Cli_Modular
  9:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 10:  * @license  http://www.horde.org/licenses/lgpl21 LGPL
 11:  * @link     http://www.horde.org/libraries/Horde_Cli_Modular
 12:  */
 13: 
 14: /**
 15:  * The Horde_Cli_Modular_Modules:: class handles a set of CLI modules.
 16:  *
 17:  * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
 18:  *
 19:  * See the enclosed file COPYING for license information (LGPL). If you
 20:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 21:  *
 22:  * @category Horde
 23:  * @package  Cli_Modular
 24:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 25:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 26:  * @link     http://www.horde.org/libraries/Horde_Cli_Modular
 27:  */
 28: class Horde_Cli_Modular_Modules
 29: implements Iterator, Countable
 30: {
 31:     /**
 32:      * Parameters.
 33:      *
 34:      * @var array
 35:      */
 36:     private $_parameters;
 37: 
 38:     /**
 39:      * The available modules.
 40:      *
 41:      * @var array
 42:      */
 43:     private $_modules;
 44: 
 45:     /**
 46:      * Constructor.
 47:      *
 48:      * @param array $parameters Options for this instance.
 49:      * <pre>
 50:      *  - directory: (string) The path to the directory that holds the modules.
 51:      *  - exclude:   (array) Exclude these modules from the list.
 52:      * </pre>
 53:      */
 54:     public function __construct(array $parameters = null)
 55:     {
 56:         $this->_parameters = $parameters;
 57:         $this->_initModules();
 58:     }
 59: 
 60:     /**
 61:      * Initialize the list of module class names.
 62:      *
 63:      * @return NULL
 64:      *
 65:      * @throws Horde_Cli_Modular_Exception In case the list of modules could not
 66:      *                                     be established.
 67:      */
 68:     private function _initModules()
 69:     {
 70:         if (empty($this->_parameters['directory'])) {
 71:             throw new Horde_Cli_Modular_Exception(
 72:                 'The "directory" parameter is missing!'
 73:             );
 74:         }
 75:         if (!file_exists($this->_parameters['directory'])) {
 76:             throw new Horde_Cli_Modular_Exception(
 77:                 sprintf(
 78:                     'The indicated directory %s does not exist!',
 79:                     $this->_parameters['directory']
 80:                 )
 81:             );
 82:         }
 83:         if (!isset($this->_parameters['exclude'])) {
 84:             $this->_parameters['exclude'] = array();
 85:         } else if (!is_array($this->_parameters['exclude'])) {
 86:             $this->_parameters['exclude'] = array($this->_parameters['exclude']);
 87:         }
 88:         foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->_parameters['directory'])) as $file) {
 89:             if ($file->isFile() && preg_match('/.php$/', $file->getFilename())) {
 90:                 $class = preg_replace("/^(.*)\.php/", '\\1', $file->getFilename());
 91:                 if (!in_array($class, $this->_parameters['exclude'])) {
 92:                     $this->_modules[] = $class;
 93:                 }
 94:             }
 95:         }
 96:     }
 97: 
 98:     /**
 99:      * List the available modules.
100:      *
101:      * @return array The list of modules.
102:      */
103:     public function listModules()
104:     {
105:         return $this->_modules;
106:     }
107: 
108:     /**
109:      * Implementation of the Iterator rewind() method. Rewinds the module list.
110:      *
111:      * return NULL
112:      */
113:     public function rewind()
114:     {
115:         reset($this->_modules);
116:     }
117: 
118:     /**
119:      * Implementation of the Iterator current(). Returns the current module.
120:      *
121:      * @return mixed The current module.
122:      */
123:     public function current()
124:     {
125:         return current($this->_modules);
126:     }
127: 
128:     /**
129:      * Implementation of the Iterator key() method. Returns the key of the current module.
130:      *
131:      * @return mixed The class name of the current module.
132:      */
133:     public function key()
134:     {
135:         return key($this->_modules);
136:     }
137: 
138:     /**
139:      * Implementation of the Iterator next() method. Returns the next module.
140:      *
141:      * @return Cli_Modular_Module|null The next module or null if there are no more
142:      * modules.
143:      */
144:     public function next()
145:     {
146:         return next($this->_modules);
147:     }
148: 
149:     /**
150:      * Implementation of the Iterator valid() method. Indicates if the current element is a valid element.
151:      *
152:      * @return boolean Whether the current element is valid
153:      */
154:     public function valid()
155:     {
156:         return key($this->_modules) !== null;
157:     }
158: 
159:     /**
160:      * Implementation of Countable count() method. Returns the number of modules.
161:      *
162:      * @return integer Number of modules.
163:      */
164:     public function count()
165:     {
166:         return count($this->_modules);
167:     }
168: }
API documentation generated by ApiGen