3 * PEAR_Command_Common base class
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
21 require_once 'PEAR.php';
24 * PEAR commands base class
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
36 class PEAR_Command_Common extends PEAR
39 * PEAR_Config object used to pass user system and configuration
40 * on when executing commands
52 * User Interface object, for all interaction with the user.
57 var $_deps_rel_trans = array(
67 var $_deps_type_trans = array(
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'
80 * PEAR_Command_Common constructor.
84 function PEAR_Command_Common(&$ui, &$config)
87 $this->config = &$config;
92 * Return a list of all the commands defined by this class.
93 * @return array list of commands
96 function getCommands()
99 foreach (array_keys($this->commands) as $command) {
100 $ret[$command] = $this->commands[$command]['summary'];
107 * Return a list of all the command shortcuts defined by this class.
108 * @return array shortcut => command
111 function getShortcuts()
114 foreach (array_keys($this->commands) as $command) {
115 if (isset($this->commands[$command]['shortcut'])) {
116 $ret[$this->commands[$command]['shortcut']] = $command;
123 function getOptions($command)
125 $shortcuts = $this->getShortcuts();
126 if (isset($shortcuts[$command])) {
127 $command = $shortcuts[$command];
130 if (isset($this->commands[$command]) &&
131 isset($this->commands[$command]['options'])) {
132 return $this->commands[$command]['options'];
138 function getGetoptArgs($command, &$short_args, &$long_args)
141 $long_args = array();
142 if (empty($this->commands[$command]) || empty($this->commands[$command]['options'])) {
146 reset($this->commands[$command]['options']);
147 while (list($option, $info) = each($this->commands[$command]['options'])) {
149 if (isset($info['arg'])) {
150 if ($info['arg']{0} == '(') {
153 $arg = substr($info['arg'], 1, -1);
161 if (isset($info['shortopt'])) {
162 $short_args .= $info['shortopt'] . $sarg;
165 $long_args[] = $option . $larg;
170 * Returns the help message for the given command
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
177 function getHelp($command)
179 $config = &PEAR_Config::singleton();
180 if (!isset($this->commands[$command])) {
181 return "No such command \"$command\"";
185 if (isset($this->commands[$command]['doc'])) {
186 $help = $this->commands[$command]['doc'];
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\"";
194 $help = $this->commands[$command]['summary'];
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);
203 return array($help, $this->getHelpArgs($command));
207 * Returns the help for the accepted arguments of a command
209 * @param string $command
210 * @return string The help string
212 function getHelpArgs($command)
214 if (isset($this->commands[$command]['options']) &&
215 count($this->commands[$command]['options']))
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);
232 if (isset($v['shortopt'])) {
234 $help .= " -$s$sapp, --$k$lapp\n";
236 $help .= " --$k$lapp\n";
240 $doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
250 function run($command, $options, $params)
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'");
259 $func = $this->commands[$cmd]['function'];
263 //$command = $this->commands[$cmd]['function'];
268 $func = $this->commands[$command]['function'];
271 return $this->$func($command, $options, $params);