Updated PEAR and PEAR packages.
[timetracker.git] / WEB-INF / lib / pear / PEAR / Command / Common.php
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  * @link       http://pear.php.net/package/PEAR
14  * @since      File available since Release 0.1
15  */
16
17 /**
18  * base class
19  */
20 require_once 'PEAR.php';
21
22 /**
23  * PEAR commands base class
24  *
25  * @category   pear
26  * @package    PEAR
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
34  */
35 class PEAR_Command_Common extends PEAR
36 {
37     /**
38      * PEAR_Config object used to pass user system and configuration
39      * on when executing commands
40      *
41      * @var PEAR_Config
42      */
43     var $config;
44     /**
45      * @var PEAR_Registry
46      * @access protected
47      */
48     var $_registry;
49
50     /**
51      * User Interface object, for all interaction with the user.
52      * @var object
53      */
54     var $ui;
55
56     var $_deps_rel_trans = array(
57                                  'lt' => '<',
58                                  'le' => '<=',
59                                  'eq' => '=',
60                                  'ne' => '!=',
61                                  'gt' => '>',
62                                  'ge' => '>=',
63                                  'has' => '=='
64                                  );
65
66     var $_deps_type_trans = array(
67                                   'pkg' => 'package',
68                                   'ext' => 'extension',
69                                   'php' => 'PHP',
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'
76                                   );
77
78     /**
79      * PEAR_Command_Common constructor.
80      *
81      * @access public
82      */
83     function __construct(&$ui, &$config)
84     {
85         parent::__construct();
86         $this->config = &$config;
87         $this->ui = &$ui;
88     }
89
90     /**
91      * Return a list of all the commands defined by this class.
92      * @return array list of commands
93      * @access public
94      */
95     function getCommands()
96     {
97         $ret = array();
98         foreach (array_keys($this->commands) as $command) {
99             $ret[$command] = $this->commands[$command]['summary'];
100         }
101
102         return $ret;
103     }
104
105     /**
106      * Return a list of all the command shortcuts defined by this class.
107      * @return array shortcut => command
108      * @access public
109      */
110     function getShortcuts()
111     {
112         $ret = array();
113         foreach (array_keys($this->commands) as $command) {
114             if (isset($this->commands[$command]['shortcut'])) {
115                 $ret[$this->commands[$command]['shortcut']] = $command;
116             }
117         }
118
119         return $ret;
120     }
121
122     function getOptions($command)
123     {
124         $shortcuts = $this->getShortcuts();
125         if (isset($shortcuts[$command])) {
126             $command = $shortcuts[$command];
127         }
128
129         if (isset($this->commands[$command]) &&
130               isset($this->commands[$command]['options'])) {
131             return $this->commands[$command]['options'];
132         }
133
134         return null;
135     }
136
137     function getGetoptArgs($command, &$short_args, &$long_args)
138     {
139         $short_args = '';
140         $long_args = array();
141         if (empty($this->commands[$command]) || empty($this->commands[$command]['options'])) {
142             return;
143         }
144
145         reset($this->commands[$command]['options']);
146         while (list($option, $info) = each($this->commands[$command]['options'])) {
147             $larg = $sarg = '';
148             if (isset($info['arg'])) {
149                 if ($info['arg']{0} == '(') {
150                     $larg = '==';
151                     $sarg = '::';
152                     $arg = substr($info['arg'], 1, -1);
153                 } else {
154                     $larg = '=';
155                     $sarg = ':';
156                     $arg = $info['arg'];
157                 }
158             }
159
160             if (isset($info['shortopt'])) {
161                 $short_args .= $info['shortopt'] . $sarg;
162             }
163
164             $long_args[] = $option . $larg;
165         }
166     }
167
168     /**
169     * Returns the help message for the given command
170     *
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
175     */
176     function getHelp($command)
177     {
178         $config = &PEAR_Config::singleton();
179         if (!isset($this->commands[$command])) {
180             return "No such command \"$command\"";
181         }
182
183         $help = null;
184         if (isset($this->commands[$command]['doc'])) {
185             $help = $this->commands[$command]['doc'];
186         }
187
188         if (empty($help)) {
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\"";
192             }
193             $help = $this->commands[$command]['summary'];
194         }
195
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);
199             }
200         }
201
202         return array($help, $this->getHelpArgs($command));
203     }
204
205     /**
206      * Returns the help for the accepted arguments of a command
207      *
208      * @param  string $command
209      * @return string The help string
210      */
211     function getHelpArgs($command)
212     {
213         if (isset($this->commands[$command]['options']) &&
214             count($this->commands[$command]['options']))
215         {
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);
221                         $sapp = " [$arg]";
222                         $lapp = "[=$arg]";
223                     } else {
224                         $sapp = " $v[arg]";
225                         $lapp = "=$v[arg]";
226                     }
227                 } else {
228                     $sapp = $lapp = "";
229                 }
230
231                 if (isset($v['shortopt'])) {
232                     $s = $v['shortopt'];
233                     $help .= "  -$s$sapp, --$k$lapp\n";
234                 } else {
235                     $help .= "  --$k$lapp\n";
236                 }
237
238                 $p = "        ";
239                 $doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
240                 $help .= "        $doc\n";
241             }
242
243             return $help;
244         }
245
246         return null;
247     }
248
249     function run($command, $options, $params)
250     {
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'");
257                     } else {
258                         $func = $this->commands[$cmd]['function'];
259                     }
260                     $command = $cmd;
261
262                     //$command = $this->commands[$cmd]['function'];
263                     break;
264                 }
265             }
266         } else {
267             $func = $this->commands[$command]['function'];
268         }
269
270         return $this->$func($command, $options, $params);
271     }
272 }