Хранилища Subversion ant

Редакция

Редакция 69 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS

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