Редакция 69 | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
69 | alex-w | 1 | <?php |
2 | /** |
||
3 | * PEAR_Command_Common base class |
||
4 | * |
||
5 | * PHP versions 4 and 5 |
||
6 | * |
||
7 | * LICENSE: This source file is subject to version 3.0 of the PHP license |
||
8 | * that is available through the world-wide-web at the following URI: |
||
9 | * http://www.php.net/license/3_0.txt. If you did not receive a copy of |
||
10 | * the PHP License and are unable to obtain it through the web, please |
||
11 | * send a note to license@php.net so we can mail you a copy immediately. |
||
12 | * |
||
13 | * @category pear |
||
14 | * @package PEAR |
||
15 | * @author Stig Bakken <ssb@php.net> |
||
16 | * @author Greg Beaver <cellog@php.net> |
||
17 | * @copyright 1997-2008 The PHP Group |
||
18 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
||
19 | * @version CVS: $Id: Common.php,v 1.36 2008/01/03 20:26:36 cellog Exp $ |
||
20 | * @link http://pear.php.net/package/PEAR |
||
21 | * @since File available since Release 0.1 |
||
22 | */ |
||
23 | |||
24 | /** |
||
25 | * base class |
||
26 | */ |
||
27 | require_once 'PEAR.php'; |
||
28 | |||
29 | /** |
||
30 | * PEAR commands base class |
||
31 | * |
||
32 | * @category pear |
||
33 | * @package PEAR |
||
34 | * @author Stig Bakken <ssb@php.net> |
||
35 | * @author Greg Beaver <cellog@php.net> |
||
36 | * @copyright 1997-2008 The PHP Group |
||
37 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
||
38 | * @version Release: 1.7.2 |
||
39 | * @link http://pear.php.net/package/PEAR |
||
40 | * @since Class available since Release 0.1 |
||
41 | */ |
||
42 | class PEAR_Command_Common extends PEAR |
||
43 | { |
||
44 | // {{{ properties |
||
45 | |||
46 | /** |
||
47 | * PEAR_Config object used to pass user system and configuration |
||
48 | * on when executing commands |
||
49 | * |
||
50 | * @var PEAR_Config |
||
51 | */ |
||
52 | var $config; |
||
53 | /** |
||
54 | * @var PEAR_Registry |
||
55 | * @access protected |
||
56 | */ |
||
57 | var $_registry; |
||
58 | |||
59 | /** |
||
60 | * User Interface object, for all interaction with the user. |
||
61 | * @var object |
||
62 | */ |
||
63 | var $ui; |
||
64 | |||
65 | var $_deps_rel_trans = array( |
||
66 | 'lt' => '<', |
||
67 | 'le' => '<=', |
||
68 | 'eq' => '=', |
||
69 | 'ne' => '!=', |
||
70 | 'gt' => '>', |
||
71 | 'ge' => '>=', |
||
72 | 'has' => '==' |
||
73 | ); |
||
74 | |||
75 | var $_deps_type_trans = array( |
||
76 | 'pkg' => 'package', |
||
77 | 'ext' => 'extension', |
||
78 | 'php' => 'PHP', |
||
79 | 'prog' => 'external program', |
||
80 | 'ldlib' => 'external library for linking', |
||
81 | 'rtlib' => 'external runtime library', |
||
82 | 'os' => 'operating system', |
||
83 | 'websrv' => 'web server', |
||
84 | 'sapi' => 'SAPI backend' |
||
85 | ); |
||
86 | |||
87 | // }}} |
||
88 | // {{{ constructor |
||
89 | |||
90 | /** |
||
91 | * PEAR_Command_Common constructor. |
||
92 | * |
||
93 | * @access public |
||
94 | */ |
||
95 | function PEAR_Command_Common(&$ui, &$config) |
||
96 | { |
||
97 | parent::PEAR(); |
||
98 | $this->config = &$config; |
||
99 | $this->ui = &$ui; |
||
100 | } |
||
101 | |||
102 | // }}} |
||
103 | |||
104 | // {{{ getCommands() |
||
105 | |||
106 | /** |
||
107 | * Return a list of all the commands defined by this class. |
||
108 | * @return array list of commands |
||
109 | * @access public |
||
110 | */ |
||
111 | function getCommands() |
||
112 | { |
||
113 | $ret = array(); |
||
114 | foreach (array_keys($this->commands) as $command) { |
||
115 | $ret[$command] = $this->commands[$command]['summary']; |
||
116 | } |
||
117 | return $ret; |
||
118 | } |
||
119 | |||
120 | // }}} |
||
121 | // {{{ getShortcuts() |
||
122 | |||
123 | /** |
||
124 | * Return a list of all the command shortcuts defined by this class. |
||
125 | * @return array shortcut => command |
||
126 | * @access public |
||
127 | */ |
||
128 | function getShortcuts() |
||
129 | { |
||
130 | $ret = array(); |
||
131 | foreach (array_keys($this->commands) as $command) { |
||
132 | if (isset($this->commands[$command]['shortcut'])) { |
||
133 | $ret[$this->commands[$command]['shortcut']] = $command; |
||
134 | } |
||
135 | } |
||
136 | return $ret; |
||
137 | } |
||
138 | |||
139 | // }}} |
||
140 | // {{{ getOptions() |
||
141 | |||
142 | function getOptions($command) |
||
143 | { |
||
144 | $shortcuts = $this->getShortcuts(); |
||
145 | if (isset($shortcuts[$command])) { |
||
146 | $command = $shortcuts[$command]; |
||
147 | } |
||
148 | if (isset($this->commands[$command]) && |
||
149 | isset($this->commands[$command]['options'])) { |
||
150 | return $this->commands[$command]['options']; |
||
151 | } else { |
||
152 | return null; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | // }}} |
||
157 | // {{{ getGetoptArgs() |
||
158 | |||
159 | function getGetoptArgs($command, &$short_args, &$long_args) |
||
160 | { |
||
161 | $short_args = ""; |
||
162 | $long_args = array(); |
||
163 | if (empty($this->commands[$command]) || empty($this->commands[$command]['options'])) { |
||
164 | return; |
||
165 | } |
||
166 | reset($this->commands[$command]['options']); |
||
167 | while (list($option, $info) = each($this->commands[$command]['options'])) { |
||
168 | $larg = $sarg = ''; |
||
169 | if (isset($info['arg'])) { |
||
170 | if ($info['arg']{0} == '(') { |
||
171 | $larg = '=='; |
||
172 | $sarg = '::'; |
||
173 | $arg = substr($info['arg'], 1, -1); |
||
174 | } else { |
||
175 | $larg = '='; |
||
176 | $sarg = ':'; |
||
177 | $arg = $info['arg']; |
||
178 | } |
||
179 | } |
||
180 | if (isset($info['shortopt'])) { |
||
181 | $short_args .= $info['shortopt'] . $sarg; |
||
182 | } |
||
183 | $long_args[] = $option . $larg; |
||
184 | } |
||
185 | } |
||
186 | |||
187 | // }}} |
||
188 | // {{{ getHelp() |
||
189 | /** |
||
190 | * Returns the help message for the given command |
||
191 | * |
||
192 | * @param string $command The command |
||
193 | * @return mixed A fail string if the command does not have help or |
||
194 | * a two elements array containing [0]=>help string, |
||
195 | * [1]=> help string for the accepted cmd args |
||
196 | */ |
||
197 | function getHelp($command) |
||
198 | { |
||
199 | $config = &PEAR_Config::singleton(); |
||
200 | if (!isset($this->commands[$command])) { |
||
201 | return "No such command \"$command\""; |
||
202 | } |
||
203 | $help = null; |
||
204 | if (isset($this->commands[$command]['doc'])) { |
||
205 | $help = $this->commands[$command]['doc']; |
||
206 | } |
||
207 | if (empty($help)) { |
||
208 | // XXX (cox) Fallback to summary if there is no doc (show both?) |
||
209 | if (!isset($this->commands[$command]['summary'])) { |
||
210 | return "No help for command \"$command\""; |
||
211 | } |
||
212 | $help = $this->commands[$command]['summary']; |
||
213 | } |
||
214 | if (preg_match_all('/{config\s+([^\}]+)}/e', $help, $matches)) { |
||
215 | foreach($matches[0] as $k => $v) { |
||
216 | $help = preg_replace("/$v/", $config->get($matches[1][$k]), $help); |
||
217 | } |
||
218 | } |
||
219 | return array($help, $this->getHelpArgs($command)); |
||
220 | } |
||
221 | |||
222 | // }}} |
||
223 | // {{{ getHelpArgs() |
||
224 | /** |
||
225 | * Returns the help for the accepted arguments of a command |
||
226 | * |
||
227 | * @param string $command |
||
228 | * @return string The help string |
||
229 | */ |
||
230 | function getHelpArgs($command) |
||
231 | { |
||
232 | if (isset($this->commands[$command]['options']) && |
||
233 | count($this->commands[$command]['options'])) |
||
234 | { |
||
235 | $help = "Options:\n"; |
||
236 | foreach ($this->commands[$command]['options'] as $k => $v) { |
||
237 | if (isset($v['arg'])) { |
||
238 | if ($v['arg'][0] == '(') { |
||
239 | $arg = substr($v['arg'], 1, -1); |
||
240 | $sapp = " [$arg]"; |
||
241 | $lapp = "[=$arg]"; |
||
242 | } else { |
||
243 | $sapp = " $v[arg]"; |
||
244 | $lapp = "=$v[arg]"; |
||
245 | } |
||
246 | } else { |
||
247 | $sapp = $lapp = ""; |
||
248 | } |
||
249 | if (isset($v['shortopt'])) { |
||
250 | $s = $v['shortopt']; |
||
251 | $help .= " -$s$sapp, --$k$lapp\n"; |
||
252 | } else { |
||
253 | $help .= " --$k$lapp\n"; |
||
254 | } |
||
255 | $p = " "; |
||
256 | $doc = rtrim(str_replace("\n", "\n$p", $v['doc'])); |
||
257 | $help .= " $doc\n"; |
||
258 | } |
||
259 | return $help; |
||
260 | } |
||
261 | return null; |
||
262 | } |
||
263 | |||
264 | // }}} |
||
265 | // {{{ run() |
||
266 | |||
267 | function run($command, $options, $params) |
||
268 | { |
||
269 | if (empty($this->commands[$command]['function'])) { |
||
270 | // look for shortcuts |
||
271 | foreach (array_keys($this->commands) as $cmd) { |
||
272 | if (isset($this->commands[$cmd]['shortcut']) && $this->commands[$cmd]['shortcut'] == $command) { |
||
273 | if (empty($this->commands[$cmd]['function'])) { |
||
274 | return $this->raiseError("unknown command `$command'"); |
||
275 | } else { |
||
276 | $func = $this->commands[$cmd]['function']; |
||
277 | } |
||
278 | $command = $cmd; |
||
279 | break; |
||
280 | } |
||
281 | } |
||
282 | } else { |
||
283 | $func = $this->commands[$command]['function']; |
||
284 | } |
||
285 | return $this->$func($command, $options, $params); |
||
286 | } |
||
287 | |||
288 | // }}} |
||
289 | } |
||
290 | |||
291 | ?> |