Subversion Repositories Applications.gtt

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 jpm 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>
187 mathias 11
 * @copyright  1997-2009 The Authors
12
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
94 jpm 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>
187 mathias 29
 * @copyright  1997-2009 The Authors
30
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
31
 * @version    Release: 1.10.1
94 jpm 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
     */
187 mathias 83
    function __construct(&$ui, &$config)
94 jpm 84
    {
187 mathias 85
        parent::__construct();
94 jpm 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
        }
187 mathias 101
 
94 jpm 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
        }
187 mathias 118
 
94 jpm 119
        return $ret;
120
    }
121
 
122
    function getOptions($command)
123
    {
124
        $shortcuts = $this->getShortcuts();
125
        if (isset($shortcuts[$command])) {
126
            $command = $shortcuts[$command];
127
        }
187 mathias 128
 
94 jpm 129
        if (isset($this->commands[$command]) &&
130
              isset($this->commands[$command]['options'])) {
131
            return $this->commands[$command]['options'];
132
        }
187 mathias 133
 
134
        return null;
94 jpm 135
    }
136
 
137
    function getGetoptArgs($command, &$short_args, &$long_args)
138
    {
187 mathias 139
        $short_args = '';
94 jpm 140
        $long_args = array();
141
        if (empty($this->commands[$command]) || empty($this->commands[$command]['options'])) {
142
            return;
143
        }
187 mathias 144
 
94 jpm 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
            }
187 mathias 159
 
94 jpm 160
            if (isset($info['shortopt'])) {
161
                $short_args .= $info['shortopt'] . $sarg;
162
            }
187 mathias 163
 
94 jpm 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
        }
187 mathias 182
 
94 jpm 183
        $help = null;
184
        if (isset($this->commands[$command]['doc'])) {
185
            $help = $this->commands[$command]['doc'];
186
        }
187 mathias 187
 
94 jpm 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
        }
187 mathias 195
 
94 jpm 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
        }
187 mathias 201
 
94 jpm 202
        return array($help, $this->getHelpArgs($command));
203
    }
204
 
205
    /**
187 mathias 206
     * Returns the help for the accepted arguments of a command
207
     *
208
     * @param  string $command
209
     * @return string The help string
210
     */
94 jpm 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
                }
187 mathias 230
 
94 jpm 231
                if (isset($v['shortopt'])) {
232
                    $s = $v['shortopt'];
233
                    $help .= "  -$s$sapp, --$k$lapp\n";
234
                } else {
235
                    $help .= "  --$k$lapp\n";
236
                }
187 mathias 237
 
94 jpm 238
                $p = "        ";
239
                $doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
240
                $help .= "        $doc\n";
241
            }
187 mathias 242
 
94 jpm 243
            return $help;
244
        }
187 mathias 245
 
94 jpm 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;
187 mathias 261
 
262
                    //$command = $this->commands[$cmd]['function'];
94 jpm 263
                    break;
264
                }
265
            }
266
        } else {
267
            $func = $this->commands[$command]['function'];
268
        }
187 mathias 269
 
94 jpm 270
        return $this->$func($command, $options, $params);
271
    }
272
}