Bearsampp 2026.9.23
Loading...
Searching...
No Matches
Command.php
Go to the documentation of this file.
1<?php
16
20require_once 'PEAR.php';
21require_once 'PEAR/Frontend.php';
22require_once 'PEAR/XMLParser.php';
23
28$GLOBALS['_PEAR_Command_commandlist'] = array();
29
34$GLOBALS['_PEAR_Command_commanddesc'] = array();
35
40$GLOBALS['_PEAR_Command_shortcuts'] = array();
41
46$GLOBALS['_PEAR_Command_objects'] = array();
47
101{
102 // {{{ factory()
103
112 public static function &factory($command, &$config)
113 {
114 if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
116 }
117 if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
118 $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
119 }
120 if (!isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
121 $a = PEAR::raiseError("unknown command `$command'");
122 return $a;
123 }
124 $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
125 if (!class_exists($class)) {
126 require_once $GLOBALS['_PEAR_Command_objects'][$class];
127 }
128 if (!class_exists($class)) {
129 $a = PEAR::raiseError("unknown command `$command'");
130 return $a;
131 }
133 $obj = new $class($ui, $config);
134 return $obj;
135 }
136
137 // }}}
138 // {{{ & getObject()
139 public static function &getObject($command)
140 {
141 $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
142 if (!class_exists($class)) {
143 require_once $GLOBALS['_PEAR_Command_objects'][$class];
144 }
145 if (!class_exists($class)) {
146 return PEAR::raiseError("unknown command `$command'");
147 }
149 $config = &PEAR_Config::singleton();
150 $obj = new $class($ui, $config);
151 return $obj;
152 }
153
154 // }}}
155 // {{{ & getFrontendObject()
156
162 public static function &getFrontendObject()
163 {
165 return $a;
166 }
167
168 // }}}
169 // {{{ & setFrontendClass()
170
178 public static function &setFrontendClass($uiclass)
179 {
180 $a = &PEAR_Frontend::setFrontendClass($uiclass);
181 return $a;
182 }
183
184 // }}}
185 // {{{ setFrontendType()
186
194 public static function setFrontendType($uitype)
195 {
196 $uiclass = 'PEAR_Frontend_' . $uitype;
197 return PEAR_Command::setFrontendClass($uiclass);
198 }
199
200 // }}}
201 // {{{ registerCommands()
202
218 public static function registerCommands($merge = false, $dir = null)
219 {
220 $parser = new PEAR_XMLParser;
221 if ($dir === null) {
222 $dir = dirname(__FILE__) . '/Command';
223 }
224 if (!is_dir($dir)) {
225 return PEAR::raiseError("registerCommands: opendir($dir) '$dir' does not exist or is not a directory");
226 }
227 $dp = @opendir($dir);
228 if (empty($dp)) {
229 return PEAR::raiseError("registerCommands: opendir($dir) failed");
230 }
231 if (!$merge) {
232 $GLOBALS['_PEAR_Command_commandlist'] = array();
233 }
234
235 while ($file = readdir($dp)) {
236 if ($file[0] == '.' || substr($file, -4) != '.xml') {
237 continue;
238 }
239
240 $f = substr($file, 0, -4);
241 $class = "PEAR_Command_" . $f;
242 // List of commands
243 if (empty($GLOBALS['_PEAR_Command_objects'][$class])) {
244 $GLOBALS['_PEAR_Command_objects'][$class] = "$dir/" . $f . '.php';
245 }
246
247 $parser->parse(file_get_contents("$dir/$file"));
248 $implements = $parser->getData();
249 foreach ($implements as $command => $desc) {
250 if ($command == 'attribs') {
251 continue;
252 }
253
254 if (isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
255 return PEAR::raiseError('Command "' . $command . '" already registered in ' .
256 'class "' . $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
257 }
258
259 $GLOBALS['_PEAR_Command_commandlist'][$command] = $class;
260 $GLOBALS['_PEAR_Command_commanddesc'][$command] = $desc['summary'];
261 if (isset($desc['shortcut'])) {
262 $shortcut = $desc['shortcut'];
263 if (isset($GLOBALS['_PEAR_Command_shortcuts'][$shortcut])) {
264 return PEAR::raiseError('Command shortcut "' . $shortcut . '" already ' .
265 'registered to command "' . $command . '" in class "' .
266 $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
267 }
268 $GLOBALS['_PEAR_Command_shortcuts'][$shortcut] = $command;
269 }
270
271 if (isset($desc['options']) && $desc['options']) {
272 foreach ($desc['options'] as $oname => $option) {
273 if (isset($option['shortopt']) && strlen($option['shortopt']) > 1) {
274 return PEAR::raiseError('Option "' . $oname . '" short option "' .
275 $option['shortopt'] . '" must be ' .
276 'only 1 character in Command "' . $command . '" in class "' .
277 $class . '"');
278 }
279 }
280 }
281 }
282 }
283
284 ksort($GLOBALS['_PEAR_Command_shortcuts']);
285 ksort($GLOBALS['_PEAR_Command_commandlist']);
286 @closedir($dp);
287 return true;
288 }
289
290 // }}}
291 // {{{ getCommands()
292
299 public static function getCommands()
300 {
301 if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
303 }
304 return $GLOBALS['_PEAR_Command_commandlist'];
305 }
306
307 // }}}
308 // {{{ getShortcuts()
309
315 public static function getShortcuts()
316 {
317 if (empty($GLOBALS['_PEAR_Command_shortcuts'])) {
319 }
320 return $GLOBALS['_PEAR_Command_shortcuts'];
321 }
322
323 // }}}
324 // {{{ getGetoptArgs()
325
335 public static function getGetoptArgs($command, &$short_args, &$long_args)
336 {
337 if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
339 }
340 if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
341 $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
342 }
343 if (!isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
344 return null;
345 }
347 return $obj->getGetoptArgs($command, $short_args, $long_args);
348 }
349
350 // }}}
351 // {{{ getDescription()
352
360 public static function getDescription($command)
361 {
362 if (!isset($GLOBALS['_PEAR_Command_commanddesc'][$command])) {
363 return null;
364 }
365 return $GLOBALS['_PEAR_Command_commanddesc'][$command];
366 }
367
368 // }}}
369 // {{{ getHelp()
370
376 public static function getHelp($command)
377 {
379 if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
380 $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
381 }
382 if (isset($cmds[$command])) {
384 return $obj->getHelp($command);
385 }
386 return false;
387 }
388 // }}}
389}
$GLOBALS['_PEAR_Command_commandlist']
Definition Command.php:28
$GLOBALS['_PEAR_default_error_mode']
Definition PEAR.php:46
static & getObject($command)
Definition Command.php:139
static getGetoptArgs($command, &$short_args, &$long_args)
Definition Command.php:335
static registerCommands($merge=false, $dir=null)
Definition Command.php:218
static getShortcuts()
Definition Command.php:315
static & factory($command, &$config)
Definition Command.php:112
static getCommands()
Definition Command.php:299
static & getFrontendObject()
Definition Command.php:162
static & setFrontendClass($uiclass)
Definition Command.php:178
static getHelp($command)
Definition Command.php:376
static setFrontendType($uitype)
Definition Command.php:194
static getDescription($command)
Definition Command.php:360
static & setFrontendClass($uiclass)
Definition Frontend.php:73
static & singleton($type=null)
Definition Frontend.php:51
$config
Definition pearcmd.php:182
$ui
Definition pearcmd.php:181
if( $raw) foreach($opts as $opt) if( $store_system_config) if($store_user_config) $command
Definition pearcmd.php:322