1: <?php
2: /**
3: * Glue class for a modular CLI.
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: * Glue class for a modular CLI.
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
29: {
30: /**
31: * Parameters.
32: *
33: * @var array
34: */
35: private $_parameters;
36:
37: /**
38: * Handler for the list of modules.
39: *
40: * @var Horde_Cli_Modular_Modules
41: */
42: private $_modules;
43:
44: /**
45: * Module provider.
46: *
47: * @var Horde_Cli_Modular_ModuleProvider
48: */
49: private $_provider;
50:
51: /**
52: * Constructor.
53: *
54: * @param array $parameters Options for this instance.
55: * <pre>
56: * - parser
57: * - class: Class name of the parser that should be used to parse
58: * command line arguments.
59: * - usage: The usage decription shown in the help output of the CLI
60: * - modules: Determines the handler for modules. Can be one of:
61: * (array) A parameter array.
62: * See Horde_Cli_Modular_Modules::__construct()
63: * (string) A class name.
64: * (object) An instance of Horde_Cli_Modular_Modules
65: * - provider: Determines the module provider. Can be one of:
66: * (array) A parameter array.
67: * See Horde_Cli_Modular_ModuleProvider::__construct()
68: * (string) A class name.
69: * (object) An instance of Horde_Cli_Modular_ModuleProvider
70: * </pre>
71: */
72: public function __construct(array $parameters = null)
73: {
74: $this->_parameters = $parameters;
75: }
76:
77: /**
78: * Return the class name for the parser that should be used.
79: *
80: * @return string The class name.
81: */
82: public function getParserClass()
83: {
84: if (empty($this->_parameters['parser']['class'])) {
85: return 'Horde_Argv_Parser';
86: } else {
87: return $this->_parameters['parser']['class'];
88: }
89: }
90:
91: /**
92: * Return the usage description for the help output of the parser.
93: *
94: * @return string The usage description.
95: */
96: public function getUsage()
97: {
98: if (empty($this->_parameters['parser']['usage'])) {
99: $usage = '[options]';
100: } else {
101: $usage = $this->_parameters['parser']['usage'];
102: }
103: foreach ($this->getModules() as $module) {
104: $usage .= $this->getProvider()->getModule($module)->getUsage();
105: }
106: return $usage;
107: }
108:
109: /**
110: * Create the parser for command line arguments.
111: *
112: * @return Horde_Argv_Parser The parser.
113: */
114: public function createParser()
115: {
116: $parser_class = $this->getParserClass();
117: $parser = new $parser_class(
118: array(
119: 'usage' => '%prog ' . $this->getUsage()
120: )
121: );
122: foreach ($this->getModules() as $module_name) {
123: $module = $this->getProvider()->getModule($module_name);
124: foreach ($module->getBaseOptions() as $option) {
125: $parser->addOption($option);
126: }
127: if ($module->hasOptionGroup()) {
128: $group = new Horde_Argv_OptionGroup(
129: $parser,
130: $module->getOptionGroupTitle(),
131: $module->getOptionGroupDescription()
132: );
133: foreach ($module->getOptionGroupOptions() as $option) {
134: $group->addOption($option);
135: }
136: $parser->addOptionGroup($group);
137: }
138: }
139: return $parser;
140: }
141:
142: /**
143: * Return the module handler.
144: *
145: * @return Horde_Cli_Modular_Modules The module handler.
146: */
147: public function getModules()
148: {
149: if ($this->_modules === null) {
150: $this->_modules = $this->_createModules();
151: }
152: return $this->_modules;
153: }
154:
155: /**
156: * Create the module handler.
157: *
158: * @return Horde_Cli_Modular_Modules The module handler.
159: */
160: private function _createModules()
161: {
162: if (is_array($this->_parameters['modules'])) {
163: return new Horde_Cli_Modular_Modules(
164: $this->_parameters['modules']
165: );
166: } else if ($this->_parameters['modules'] instanceOf Horde_Cli_Modular_Modules) {
167: return $this->_parameters['modules'];
168: } else if (is_string($this->_parameters['modules'])) {
169: return new $this->_parameters['modules']();
170: } else if (empty($this->_parameters['modules'])) {
171: throw new Horde_Cli_Modular_Exception(
172: 'Missing "modules" parameter!'
173: );
174: } else {
175: throw new Horde_Cli_Modular_Exception(
176: 'Invalid "modules" parameter!'
177: );
178: }
179: }
180:
181: /**
182: * Return the module provider.
183: *
184: * @return Horde_Cli_Modular_ModuleProvider The module provider.
185: */
186: public function getProvider()
187: {
188: if ($this->_provider === null) {
189: $this->_provider = $this->_createProvider();
190: }
191: return $this->_provider;
192: }
193:
194: /**
195: * Create the module provider.
196: *
197: * @return Horde_Cli_Modular_ModuleProvider The module provider.
198: */
199: private function _createProvider()
200: {
201: if (is_array($this->_parameters['provider'])) {
202: return new Horde_Cli_Modular_ModuleProvider(
203: $this->_parameters['provider']
204: );
205: } else if ($this->_parameters['provider'] instanceOf Horde_Cli_Modular_ModuleProvider) {
206: return $this->_parameters['provider'];
207: } else if (is_string($this->_parameters['provider'])) {
208: return new $this->_parameters['provider']();
209: } else if (empty($this->_parameters['provider'])) {
210: throw new Horde_Cli_Modular_Exception(
211: 'Missing "provider" parameter!'
212: );
213: } else {
214: throw new Horde_Cli_Modular_Exception(
215: 'Invalid "provider" parameter!'
216: );
217: }
218: }
219: }