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, command pattern class
3
 * PEAR_Command, command pattern 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: Command.php,v 1.38 2006/10/31 02:54:40 cellog 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 96... Line 89...
96
 *   $this->raiseError().
89
 *   $this->raiseError().
97
 * @category   pear
90
 * @category   pear
98
 * @package    PEAR
91
 * @package    PEAR
99
 * @author     Stig Bakken <ssb@php.net>
92
 * @author     Stig Bakken <ssb@php.net>
100
 * @author     Greg Beaver <cellog@php.net>
93
 * @author     Greg Beaver <cellog@php.net>
101
 * @copyright  1997-2006 The PHP Group
94
 * @copyright  1997-2009 The Authors
102
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
95
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
103
 * @version    Release: 1.5.1
96
 * @version    Release: 1.10.1
104
 * @link       http://pear.php.net/package/PEAR
97
 * @link       http://pear.php.net/package/PEAR
105
 * @since      Class available since Release 0.1
98
 * @since      Class available since Release 0.1
106
 */
99
 */
107
class PEAR_Command
100
class PEAR_Command
108
{
101
{
Line 113... Line 106...
113
     *
106
     *
114
     * @param string $command The name of the command
107
     * @param string $command The name of the command
115
     * @param object $config  Instance of PEAR_Config object
108
     * @param object $config  Instance of PEAR_Config object
116
     *
109
     *
117
     * @return object the command object or a PEAR error
110
     * @return object the command object or a PEAR error
118
     *
-
 
119
     * @access public
-
 
120
     * @static
-
 
121
     */
111
     */
122
    function &factory($command, &$config)
112
    public static function &factory($command, &$config)
123
    {
113
    {
124
        if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
114
        if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
125
            PEAR_Command::registerCommands();
115
            PEAR_Command::registerCommands();
126
        }
116
        }
127
        if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
117
        if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
Line 138... Line 128...
138
        if (!class_exists($class)) {
128
        if (!class_exists($class)) {
139
            $a = PEAR::raiseError("unknown command `$command'");
129
            $a = PEAR::raiseError("unknown command `$command'");
140
            return $a;
130
            return $a;
141
        }
131
        }
142
        $ui =& PEAR_Command::getFrontendObject();
132
        $ui =& PEAR_Command::getFrontendObject();
143
        $obj = &new $class($ui, $config);
133
        $obj = new $class($ui, $config);
144
        return $obj;
134
        return $obj;
145
    }
135
    }
Line 146... Line 136...
146
 
136
 
147
    // }}}
137
    // }}}
148
    // {{{ & getObject()
138
    // {{{ & getObject()
149
    function &getObject($command)
139
    public static function &getObject($command)
150
    {
140
    {
151
        $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
141
        $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
152
        if (!class_exists($class)) {
142
        if (!class_exists($class)) {
153
            require_once $GLOBALS['_PEAR_Command_objects'][$class];
143
            require_once $GLOBALS['_PEAR_Command_objects'][$class];
154
        }
144
        }
155
        if (!class_exists($class)) {
145
        if (!class_exists($class)) {
156
            return PEAR::raiseError("unknown command `$command'");
146
            return PEAR::raiseError("unknown command `$command'");
157
        }
147
        }
158
        $ui =& PEAR_Command::getFrontendObject();
148
        $ui =& PEAR_Command::getFrontendObject();
159
        $config = &PEAR_Config::singleton();
149
        $config = &PEAR_Config::singleton();
160
        $obj = &new $class($ui, $config);
150
        $obj = new $class($ui, $config);
161
        return $obj;
151
        return $obj;
Line 162... Line 152...
162
    }
152
    }
163
 
153
 
Line 164... Line 154...
164
    // }}}
154
    // }}}
165
    // {{{ & getFrontendObject()
155
    // {{{ & getFrontendObject()
166
 
156
 
167
    /**
157
    /**
168
     * Get instance of frontend object.
-
 
169
     *
158
     * Get instance of frontend object.
170
     * @return object|PEAR_Error
159
     *
171
     * @static
160
     * @return object|PEAR_Error
172
     */
161
     */
173
    function &getFrontendObject()
162
    public static function &getFrontendObject()
174
    {
163
    {
Line 183... Line 172...
183
     * Load current frontend class.
172
     * Load current frontend class.
184
     *
173
     *
185
     * @param string $uiclass Name of class implementing the frontend
174
     * @param string $uiclass Name of class implementing the frontend
186
     *
175
     *
187
     * @return object the frontend object, or a PEAR error
176
     * @return object the frontend object, or a PEAR error
188
     * @static
-
 
189
     */
177
     */
190
    function &setFrontendClass($uiclass)
178
    public static function &setFrontendClass($uiclass)
191
    {
179
    {
192
        $a = &PEAR_Frontend::setFrontendClass($uiclass);
180
        $a = &PEAR_Frontend::setFrontendClass($uiclass);
193
        return $a;
181
        return $a;
194
    }
182
    }
Line 200... Line 188...
200
     * Set current frontend.
188
     * Set current frontend.
201
     *
189
     *
202
     * @param string $uitype Name of the frontend type (for example "CLI")
190
     * @param string $uitype Name of the frontend type (for example "CLI")
203
     *
191
     *
204
     * @return object the frontend object, or a PEAR error
192
     * @return object the frontend object, or a PEAR error
205
     * @static
-
 
206
     */
193
     */
207
    function setFrontendType($uitype)
194
    public static function setFrontendType($uitype)
208
    {
195
    {
209
        $uiclass = 'PEAR_Frontend_' . $uitype;
196
        $uiclass = 'PEAR_Frontend_' . $uitype;
210
        return PEAR_Command::setFrontendClass($uiclass);
197
        return PEAR_Command::setFrontendClass($uiclass);
211
    }
198
    }
Line 225... Line 212...
225
     *               classes, defaults to the Command subdirectory of
212
     *               classes, defaults to the Command subdirectory of
226
     *               the directory from where this file (__FILE__) is
213
     *               the directory from where this file (__FILE__) is
227
     *               included.
214
     *               included.
228
     *
215
     *
229
     * @return bool TRUE on success, a PEAR error on failure
216
     * @return bool TRUE on success, a PEAR error on failure
230
     *
-
 
231
     * @access public
-
 
232
     * @static
-
 
233
     */
217
     */
234
    function registerCommands($merge = false, $dir = null)
218
    public static function registerCommands($merge = false, $dir = null)
235
    {
219
    {
236
        $parser = new PEAR_XMLParser;
220
        $parser = new PEAR_XMLParser;
237
        if ($dir === null) {
221
        if ($dir === null) {
238
            $dir = dirname(__FILE__) . '/Command';
222
            $dir = dirname(__FILE__) . '/Command';
239
        }
223
        }
Line 245... Line 229...
245
            return PEAR::raiseError("registerCommands: opendir($dir) failed");
229
            return PEAR::raiseError("registerCommands: opendir($dir) failed");
246
        }
230
        }
247
        if (!$merge) {
231
        if (!$merge) {
248
            $GLOBALS['_PEAR_Command_commandlist'] = array();
232
            $GLOBALS['_PEAR_Command_commandlist'] = array();
249
        }
233
        }
-
 
234
 
250
        while ($entry = readdir($dp)) {
235
        while ($file = readdir($dp)) {
251
            if ($entry{0} == '.' || substr($entry, -4) != '.xml') {
236
            if ($file{0} == '.' || substr($file, -4) != '.xml') {
252
                continue;
237
                continue;
253
            }
238
            }
-
 
239
 
254
            $class = "PEAR_Command_".substr($entry, 0, -4);
240
            $f = substr($file, 0, -4);
255
            $file = "$dir/$entry";
241
            $class = "PEAR_Command_" . $f;
256
            $parser->parse(file_get_contents($file));
-
 
257
            $implements = $parser->getData();
-
 
258
            // List of commands
242
            // List of commands
259
            if (empty($GLOBALS['_PEAR_Command_objects'][$class])) {
243
            if (empty($GLOBALS['_PEAR_Command_objects'][$class])) {
260
                $GLOBALS['_PEAR_Command_objects'][$class] = "$dir/" . substr($entry, 0, -4) .
244
                $GLOBALS['_PEAR_Command_objects'][$class] = "$dir/" . $f . '.php';
261
                    '.php';
-
 
262
            }
245
            }
-
 
246
 
-
 
247
            $parser->parse(file_get_contents("$dir/$file"));
-
 
248
            $implements = $parser->getData();
263
            foreach ($implements as $command => $desc) {
249
            foreach ($implements as $command => $desc) {
264
                if ($command == 'attribs') {
250
                if ($command == 'attribs') {
265
                    continue;
251
                    continue;
266
                }
252
                }
-
 
253
 
267
                if (isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
254
                if (isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
268
                    return PEAR::raiseError('Command "' . $command . '" already registered in ' .
255
                    return PEAR::raiseError('Command "' . $command . '" already registered in ' .
269
                        'class "' . $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
256
                        'class "' . $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
270
                }
257
                }
-
 
258
 
271
                $GLOBALS['_PEAR_Command_commandlist'][$command] = $class;
259
                $GLOBALS['_PEAR_Command_commandlist'][$command] = $class;
272
                $GLOBALS['_PEAR_Command_commanddesc'][$command] = $desc['summary'];
260
                $GLOBALS['_PEAR_Command_commanddesc'][$command] = $desc['summary'];
273
                if (isset($desc['shortcut'])) {
261
                if (isset($desc['shortcut'])) {
274
                    $shortcut = $desc['shortcut'];
262
                    $shortcut = $desc['shortcut'];
275
                    if (isset($GLOBALS['_PEAR_Command_shortcuts'][$shortcut])) {
263
                    if (isset($GLOBALS['_PEAR_Command_shortcuts'][$shortcut])) {
Line 277... Line 265...
277
                            'registered to command "' . $command . '" in class "' .
265
                            'registered to command "' . $command . '" in class "' .
278
                            $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
266
                            $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
279
                    }
267
                    }
280
                    $GLOBALS['_PEAR_Command_shortcuts'][$shortcut] = $command;
268
                    $GLOBALS['_PEAR_Command_shortcuts'][$shortcut] = $command;
281
                }
269
                }
-
 
270
 
282
                if (isset($desc['options']) && $desc['options']) {
271
                if (isset($desc['options']) && $desc['options']) {
283
                    foreach ($desc['options'] as $oname => $option) {
272
                    foreach ($desc['options'] as $oname => $option) {
284
                        if (isset($option['shortopt']) && strlen($option['shortopt']) > 1) {
273
                        if (isset($option['shortopt']) && strlen($option['shortopt']) > 1) {
285
                            return PEAR::raiseError('Option "' . $oname . '" short option "' .
274
                            return PEAR::raiseError('Option "' . $oname . '" short option "' .
286
                                $option['shortopt'] . '" must be ' .
275
                                $option['shortopt'] . '" must be ' .
Line 289... Line 278...
289
                        }
278
                        }
290
                    }
279
                    }
291
                }
280
                }
292
            }
281
            }
293
        }
282
        }
-
 
283
 
294
        ksort($GLOBALS['_PEAR_Command_shortcuts']);
284
        ksort($GLOBALS['_PEAR_Command_shortcuts']);
295
        ksort($GLOBALS['_PEAR_Command_commandlist']);
285
        ksort($GLOBALS['_PEAR_Command_commandlist']);
296
        @closedir($dp);
286
        @closedir($dp);
297
        return true;
287
        return true;
298
    }
288
    }
Line 303... Line 293...
303
    /**
293
    /**
304
     * Get the list of currently supported commands, and what
294
     * Get the list of currently supported commands, and what
305
     * classes implement them.
295
     * classes implement them.
306
     *
296
     *
307
     * @return array command => implementing class
297
     * @return array command => implementing class
308
     *
-
 
309
     * @access public
-
 
310
     * @static
-
 
311
     */
298
     */
312
    function getCommands()
299
    public static function getCommands()
313
    {
300
    {
314
        if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
301
        if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
315
            PEAR_Command::registerCommands();
302
            PEAR_Command::registerCommands();
316
        }
303
        }
317
        return $GLOBALS['_PEAR_Command_commandlist'];
304
        return $GLOBALS['_PEAR_Command_commandlist'];
Line 322... Line 309...
322
 
309
 
323
    /**
310
    /**
324
     * Get the list of command shortcuts.
311
     * Get the list of command shortcuts.
325
     *
312
     *
326
     * @return array shortcut => command
-
 
327
     *
-
 
328
     * @access public
-
 
329
     * @static
313
     * @return array shortcut => command
330
     */
314
     */
331
    function getShortcuts()
315
    public static function getShortcuts()
332
    {
316
    {
333
        if (empty($GLOBALS['_PEAR_Command_shortcuts'])) {
317
        if (empty($GLOBALS['_PEAR_Command_shortcuts'])) {
334
            PEAR_Command::registerCommands();
318
            PEAR_Command::registerCommands();
335
        }
319
        }
Line 345... Line 329...
345
     * @param string $command     command to get optstring for
329
     * @param string $command     command to get optstring for
346
     * @param string $short_args  (reference) short getopt format
330
     * @param string $short_args  (reference) short getopt format
347
     * @param array  $long_args   (reference) long getopt format
331
     * @param array  $long_args   (reference) long getopt format
348
     *
332
     *
349
     * @return void
333
     * @return void
350
     *
-
 
351
     * @access public
-
 
352
     * @static
-
 
353
     */
334
     */
354
    function getGetoptArgs($command, &$short_args, &$long_args)
335
    public static function getGetoptArgs($command, &$short_args, &$long_args)
355
    {
336
    {
356
        if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
337
        if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
357
            PEAR_Command::registerCommands();
338
            PEAR_Command::registerCommands();
358
        }
339
        }
359
        if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
340
        if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
Line 373... Line 354...
373
     * Get description for a command.
354
     * Get description for a command.
374
     *
355
     *
375
     * @param  string $command Name of the command
356
     * @param  string $command Name of the command
376
     *
357
     *
377
     * @return string command description
358
     * @return string command description
378
     *
-
 
379
     * @access public
-
 
380
     * @static
-
 
381
     */
359
     */
382
    function getDescription($command)
360
    public static function getDescription($command)
383
    {
361
    {
384
        if (!isset($GLOBALS['_PEAR_Command_commanddesc'][$command])) {
362
        if (!isset($GLOBALS['_PEAR_Command_commanddesc'][$command])) {
385
            return null;
363
            return null;
386
        }
364
        }
387
        return $GLOBALS['_PEAR_Command_commanddesc'][$command];
365
        return $GLOBALS['_PEAR_Command_commanddesc'][$command];
Line 392... Line 370...
392
 
370
 
393
    /**
371
    /**
394
     * Get help for command.
372
     * Get help for command.
395
     *
373
     *
396
     * @param string $command Name of the command to return help for
-
 
397
     *
-
 
398
     * @access public
-
 
399
     * @static
374
     * @param string $command Name of the command to return help for
400
     */
375
     */
401
    function getHelp($command)
376
    public static function getHelp($command)
402
    {
377
    {
403
        $cmds = PEAR_Command::getCommands();
378
        $cmds = PEAR_Command::getCommands();
404
        if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
379
        if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
405
            $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
380
            $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
Line 409... Line 384...
409
            return $obj->getHelp($command);
384
            return $obj->getHelp($command);
410
        }
385
        }
411
        return false;
386
        return false;
412
    }
387
    }
413
    // }}}
388
    // }}}
414
}
389
}
415
 
-
 
416
?>
-
 
417
390