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