Subversion Repositories Applications.gtt

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
Line 2... Line 2...
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
-
 
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
-
 
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.
-
 
12
 *
-
 
13
 * @category   pear
7
 * @category   pear
14
 * @package    PEAR
8
 * @package    PEAR
15
 * @author     Stig Bakken <ssb@php.net>
9
 * @author     Stig Bakken <ssb@php.net>
16
 * @author     Greg Beaver <cellog@php.net>
10
 * @author     Greg Beaver <cellog@php.net>
17
 * @copyright  1997-2006 The PHP Group
11
 * @copyright  1997-2009 The Authors
18
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
12
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
19
 * @version    CVS: $Id: Common.php,v 1.35 2006/06/08 22:25:18 pajoye Exp $
-
 
20
 * @link       http://pear.php.net/package/PEAR
13
 * @link       http://pear.php.net/package/PEAR
21
 * @since      File available since Release 0.1
14
 * @since      File available since Release 0.1
22
 */
15
 */
Line 23... Line 16...
23
 
16
 
Line 31... Line 24...
31
 *
24
 *
32
 * @category   pear
25
 * @category   pear
33
 * @package    PEAR
26
 * @package    PEAR
34
 * @author     Stig Bakken <ssb@php.net>
27
 * @author     Stig Bakken <ssb@php.net>
35
 * @author     Greg Beaver <cellog@php.net>
28
 * @author     Greg Beaver <cellog@php.net>
36
 * @copyright  1997-2006 The PHP Group
29
 * @copyright  1997-2009 The Authors
37
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
30
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
38
 * @version    Release: 1.5.1
31
 * @version    Release: 1.10.1
39
 * @link       http://pear.php.net/package/PEAR
32
 * @link       http://pear.php.net/package/PEAR
40
 * @since      Class available since Release 0.1
33
 * @since      Class available since Release 0.1
41
 */
34
 */
42
class PEAR_Command_Common extends PEAR
35
class PEAR_Command_Common extends PEAR
43
{
36
{
44
    // {{{ properties
-
 
45
 
-
 
46
    /**
37
    /**
47
     * PEAR_Config object used to pass user system and configuration
38
     * PEAR_Config object used to pass user system and configuration
48
     * on when executing commands
39
     * on when executing commands
49
     *
40
     *
50
     * @var PEAR_Config
41
     * @var PEAR_Config
Line 82... Line 73...
82
                                  'os' => 'operating system',
73
                                  'os' => 'operating system',
83
                                  'websrv' => 'web server',
74
                                  'websrv' => 'web server',
84
                                  'sapi' => 'SAPI backend'
75
                                  'sapi' => 'SAPI backend'
85
                                  );
76
                                  );
Line 86... Line -...
86
 
-
 
87
    // }}}
-
 
88
    // {{{ constructor
-
 
89
 
77
 
90
    /**
78
    /**
91
     * PEAR_Command_Common constructor.
79
     * PEAR_Command_Common constructor.
92
     *
80
     *
93
     * @access public
81
     * @access public
94
     */
82
     */
95
    function PEAR_Command_Common(&$ui, &$config)
83
    function __construct(&$ui, &$config)
96
    {
84
    {
97
        parent::PEAR();
85
        parent::__construct();
98
        $this->config = &$config;
86
        $this->config = &$config;
99
        $this->ui = &$ui;
87
        $this->ui = &$ui;
Line 100... Line -...
100
    }
-
 
101
 
-
 
102
    // }}}
-
 
103
 
-
 
104
    // {{{ getCommands()
88
    }
105
 
89
 
106
    /**
90
    /**
107
     * Return a list of all the commands defined by this class.
91
     * Return a list of all the commands defined by this class.
108
     * @return array list of commands
92
     * @return array list of commands
Line 112... Line 96...
112
    {
96
    {
113
        $ret = array();
97
        $ret = array();
114
        foreach (array_keys($this->commands) as $command) {
98
        foreach (array_keys($this->commands) as $command) {
115
            $ret[$command] = $this->commands[$command]['summary'];
99
            $ret[$command] = $this->commands[$command]['summary'];
116
        }
100
        }
-
 
101
 
117
        return $ret;
102
        return $ret;
118
    }
103
    }
Line 119... Line -...
119
 
-
 
120
    // }}}
-
 
121
    // {{{ getShortcuts()
-
 
122
 
104
 
123
    /**
105
    /**
124
     * Return a list of all the command shortcuts defined by this class.
106
     * Return a list of all the command shortcuts defined by this class.
125
     * @return array shortcut => command
107
     * @return array shortcut => command
126
     * @access public
108
     * @access public
Line 131... Line 113...
131
        foreach (array_keys($this->commands) as $command) {
113
        foreach (array_keys($this->commands) as $command) {
132
            if (isset($this->commands[$command]['shortcut'])) {
114
            if (isset($this->commands[$command]['shortcut'])) {
133
                $ret[$this->commands[$command]['shortcut']] = $command;
115
                $ret[$this->commands[$command]['shortcut']] = $command;
134
            }
116
            }
135
        }
117
        }
-
 
118
 
136
        return $ret;
119
        return $ret;
137
    }
120
    }
Line 138... Line -...
138
 
-
 
139
    // }}}
-
 
140
    // {{{ getOptions()
-
 
141
 
121
 
142
    function getOptions($command)
122
    function getOptions($command)
143
    {
123
    {
144
        $shortcuts = $this->getShortcuts();
124
        $shortcuts = $this->getShortcuts();
145
        if (isset($shortcuts[$command])) {
125
        if (isset($shortcuts[$command])) {
146
            $command = $shortcuts[$command];
126
            $command = $shortcuts[$command];
-
 
127
        }
147
        }
128
 
148
        if (isset($this->commands[$command]) &&
129
        if (isset($this->commands[$command]) &&
149
              isset($this->commands[$command]['options'])) {
130
              isset($this->commands[$command]['options'])) {
150
            return $this->commands[$command]['options'];
-
 
151
        } else {
-
 
152
            return null;
131
            return $this->commands[$command]['options'];
153
        }
-
 
Line -... Line 132...
-
 
132
        }
154
    }
133
 
155
 
-
 
Line 156... Line 134...
156
    // }}}
134
        return null;
157
    // {{{ getGetoptArgs()
135
    }
158
 
136
 
159
    function getGetoptArgs($command, &$short_args, &$long_args)
137
    function getGetoptArgs($command, &$short_args, &$long_args)
160
    {
138
    {
161
        $short_args = "";
139
        $short_args = '';
162
        $long_args = array();
140
        $long_args = array();
-
 
141
        if (empty($this->commands[$command]) || empty($this->commands[$command]['options'])) {
163
        if (empty($this->commands[$command]) || empty($this->commands[$command]['options'])) {
142
            return;
164
            return;
143
        }
165
        }
144
 
166
        reset($this->commands[$command]['options']);
145
        reset($this->commands[$command]['options']);
167
        while (list($option, $info) = each($this->commands[$command]['options'])) {
146
        while (list($option, $info) = each($this->commands[$command]['options'])) {
Line 175... Line 154...
175
                    $larg = '=';
154
                    $larg = '=';
176
                    $sarg = ':';
155
                    $sarg = ':';
177
                    $arg = $info['arg'];
156
                    $arg = $info['arg'];
178
                }
157
                }
179
            }
158
            }
-
 
159
 
180
            if (isset($info['shortopt'])) {
160
            if (isset($info['shortopt'])) {
181
                $short_args .= $info['shortopt'] . $sarg;
161
                $short_args .= $info['shortopt'] . $sarg;
182
            }
162
            }
-
 
163
 
183
            $long_args[] = $option . $larg;
164
            $long_args[] = $option . $larg;
184
        }
165
        }
185
    }
166
    }
Line 186... Line -...
186
 
-
 
187
    // }}}
-
 
188
    // {{{ getHelp()
167
 
189
    /**
168
    /**
190
    * Returns the help message for the given command
169
    * Returns the help message for the given command
191
    *
170
    *
192
    * @param string $command The command
171
    * @param string $command The command
Line 198... Line 177...
198
    {
177
    {
199
        $config = &PEAR_Config::singleton();
178
        $config = &PEAR_Config::singleton();
200
        if (!isset($this->commands[$command])) {
179
        if (!isset($this->commands[$command])) {
201
            return "No such command \"$command\"";
180
            return "No such command \"$command\"";
202
        }
181
        }
-
 
182
 
203
        $help = null;
183
        $help = null;
204
        if (isset($this->commands[$command]['doc'])) {
184
        if (isset($this->commands[$command]['doc'])) {
205
            $help = $this->commands[$command]['doc'];
185
            $help = $this->commands[$command]['doc'];
206
        }
186
        }
-
 
187
 
207
        if (empty($help)) {
188
        if (empty($help)) {
208
            // XXX (cox) Fallback to summary if there is no doc (show both?)
189
            // XXX (cox) Fallback to summary if there is no doc (show both?)
209
            if (!isset($this->commands[$command]['summary'])) {
190
            if (!isset($this->commands[$command]['summary'])) {
210
                return "No help for command \"$command\"";
191
                return "No help for command \"$command\"";
211
            }
192
            }
212
            $help = $this->commands[$command]['summary'];
193
            $help = $this->commands[$command]['summary'];
213
        }
194
        }
-
 
195
 
214
        if (preg_match_all('/{config\s+([^\}]+)}/e', $help, $matches)) {
196
        if (preg_match_all('/{config\s+([^\}]+)}/e', $help, $matches)) {
215
            foreach($matches[0] as $k => $v) {
197
            foreach($matches[0] as $k => $v) {
216
                $help = preg_replace("/$v/", $config->get($matches[1][$k]), $help);
198
                $help = preg_replace("/$v/", $config->get($matches[1][$k]), $help);
217
            }
199
            }
218
        }
200
        }
-
 
201
 
219
        return array($help, $this->getHelpArgs($command));
202
        return array($help, $this->getHelpArgs($command));
220
    }
203
    }
Line 221... Line -...
221
 
-
 
222
    // }}}
-
 
223
    // {{{ getHelpArgs()
204
 
224
    /**
205
    /**
225
    * Returns the help for the accepted arguments of a command
206
     * Returns the help for the accepted arguments of a command
226
    *
207
     *
227
    * @param  string $command
208
     * @param  string $command
228
    * @return string The help string
209
     * @return string The help string
229
    */
210
     */
230
    function getHelpArgs($command)
211
    function getHelpArgs($command)
231
    {
212
    {
232
        if (isset($this->commands[$command]['options']) &&
213
        if (isset($this->commands[$command]['options']) &&
233
            count($this->commands[$command]['options']))
214
            count($this->commands[$command]['options']))
Line 244... Line 225...
244
                        $lapp = "=$v[arg]";
225
                        $lapp = "=$v[arg]";
245
                    }
226
                    }
246
                } else {
227
                } else {
247
                    $sapp = $lapp = "";
228
                    $sapp = $lapp = "";
248
                }
229
                }
-
 
230
 
249
                if (isset($v['shortopt'])) {
231
                if (isset($v['shortopt'])) {
250
                    $s = $v['shortopt'];
232
                    $s = $v['shortopt'];
251
                    $help .= "  -$s$sapp, --$k$lapp\n";
233
                    $help .= "  -$s$sapp, --$k$lapp\n";
252
                } else {
234
                } else {
253
                    $help .= "  --$k$lapp\n";
235
                    $help .= "  --$k$lapp\n";
254
                }
236
                }
-
 
237
 
255
                $p = "        ";
238
                $p = "        ";
256
                $doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
239
                $doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
257
                $help .= "        $doc\n";
240
                $help .= "        $doc\n";
258
            }
241
            }
-
 
242
 
259
            return $help;
243
            return $help;
260
        }
244
        }
-
 
245
 
261
        return null;
246
        return null;
262
    }
247
    }
Line 263... Line -...
263
 
-
 
264
    // }}}
-
 
265
    // {{{ run()
-
 
266
 
248
 
267
    function run($command, $options, $params)
249
    function run($command, $options, $params)
268
    {
250
    {
269
        if (empty($this->commands[$command]['function'])) {
251
        if (empty($this->commands[$command]['function'])) {
270
            // look for shortcuts
252
            // look for shortcuts
Line 274... Line 256...
274
                        return $this->raiseError("unknown command `$command'");
256
                        return $this->raiseError("unknown command `$command'");
275
                    } else {
257
                    } else {
276
                        $func = $this->commands[$cmd]['function'];
258
                        $func = $this->commands[$cmd]['function'];
277
                    }
259
                    }
278
                    $command = $cmd;
260
                    $command = $cmd;
-
 
261
 
-
 
262
                    //$command = $this->commands[$cmd]['function'];
279
                    break;
263
                    break;
280
                }
264
                }
281
            }
265
            }
282
        } else {
266
        } else {
283
            $func = $this->commands[$command]['function'];
267
            $func = $this->commands[$command]['function'];
284
        }
268
        }
-
 
269
 
285
        return $this->$func($command, $options, $params);
270
        return $this->$func($command, $options, $params);
286
    }
271
    }
287
 
-
 
288
    // }}}
-
 
289
}
272
}
290
 
-
 
291
?>
-