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 * @link http://pear.php.net/package/PEAR
14 * @since File available since Release 0.1
20 require_once 'PEAR.php';
23 * PEAR commands base class
27 * @author Stig Bakken <ssb@php.net>
28 * @author Greg Beaver <cellog@php.net>
29 * @copyright 1997-2009 The Authors
30 * @license http://opensource.org/licenses/bsd-license.php New BSD License
31 * @version Release: 1.10.1
32 * @link http://pear.php.net/package/PEAR
33 * @since Class available since Release 0.1
35 class PEAR_Command_Common extends PEAR
38 * PEAR_Config object used to pass user system and configuration
39 * on when executing commands
51 * User Interface object, for all interaction with the user.
56 var $_deps_rel_trans = array(
66 var $_deps_type_trans = array(
70 'prog' => 'external program',
71 'ldlib' => 'external library for linking',
72 'rtlib' => 'external runtime library',
73 'os' => 'operating system',
74 'websrv' => 'web server',
75 'sapi' => 'SAPI backend'
79 * PEAR_Command_Common constructor.
83 function __construct(&$ui, &$config)
85 parent::__construct();
86 $this->config = &$config;
91 * Return a list of all the commands defined by this class.
92 * @return array list of commands
95 function getCommands()
98 foreach (array_keys($this->commands) as $command) {
99 $ret[$command] = $this->commands[$command]['summary'];
106 * Return a list of all the command shortcuts defined by this class.
107 * @return array shortcut => command
110 function getShortcuts()
113 foreach (array_keys($this->commands) as $command) {
114 if (isset($this->commands[$command]['shortcut'])) {
115 $ret[$this->commands[$command]['shortcut']] = $command;
122 function getOptions($command)
124 $shortcuts = $this->getShortcuts();
125 if (isset($shortcuts[$command])) {
126 $command = $shortcuts[$command];
129 if (isset($this->commands[$command]) &&
130 isset($this->commands[$command]['options'])) {
131 return $this->commands[$command]['options'];
137 function getGetoptArgs($command, &$short_args, &$long_args)
140 $long_args = array();
141 if (empty($this->commands[$command]) || empty($this->commands[$command]['options'])) {
145 reset($this->commands[$command]['options']);
146 while (list($option, $info) = each($this->commands[$command]['options'])) {
148 if (isset($info['arg'])) {
149 if ($info['arg']{0} == '(') {
152 $arg = substr($info['arg'], 1, -1);
160 if (isset($info['shortopt'])) {
161 $short_args .= $info['shortopt'] . $sarg;
164 $long_args[] = $option . $larg;
169 * Returns the help message for the given command
171 * @param string $command The command
172 * @return mixed A fail string if the command does not have help or
173 * a two elements array containing [0]=>help string,
174 * [1]=> help string for the accepted cmd args
176 function getHelp($command)
178 $config = &PEAR_Config::singleton();
179 if (!isset($this->commands[$command])) {
180 return "No such command \"$command\"";
184 if (isset($this->commands[$command]['doc'])) {
185 $help = $this->commands[$command]['doc'];
189 // XXX (cox) Fallback to summary if there is no doc (show both?)
190 if (!isset($this->commands[$command]['summary'])) {
191 return "No help for command \"$command\"";
193 $help = $this->commands[$command]['summary'];
196 if (preg_match_all('/{config\s+([^\}]+)}/e', $help, $matches)) {
197 foreach($matches[0] as $k => $v) {
198 $help = preg_replace("/$v/", $config->get($matches[1][$k]), $help);
202 return array($help, $this->getHelpArgs($command));
206 * Returns the help for the accepted arguments of a command
208 * @param string $command
209 * @return string The help string
211 function getHelpArgs($command)
213 if (isset($this->commands[$command]['options']) &&
214 count($this->commands[$command]['options']))
216 $help = "Options:\n";
217 foreach ($this->commands[$command]['options'] as $k => $v) {
218 if (isset($v['arg'])) {
219 if ($v['arg'][0] == '(') {
220 $arg = substr($v['arg'], 1, -1);
231 if (isset($v['shortopt'])) {
233 $help .= " -$s$sapp, --$k$lapp\n";
235 $help .= " --$k$lapp\n";
239 $doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
249 function run($command, $options, $params)
251 if (empty($this->commands[$command]['function'])) {
252 // look for shortcuts
253 foreach (array_keys($this->commands) as $cmd) {
254 if (isset($this->commands[$cmd]['shortcut']) && $this->commands[$cmd]['shortcut'] == $command) {
255 if (empty($this->commands[$cmd]['function'])) {
256 return $this->raiseError("unknown command `$command'");
258 $func = $this->commands[$cmd]['function'];
262 //$command = $this->commands[$cmd]['function'];
267 $func = $this->commands[$command]['function'];
270 return $this->$func($command, $options, $params);