[ Index ] |
|
Code source de SPIP Agora 1.4 |
1 <?php 2 // 3 // +----------------------------------------------------------------------+ 4 // | PHP Version 5 | 5 // +----------------------------------------------------------------------+ 6 // | Copyright (c) 1997-2004 The PHP Group | 7 // +----------------------------------------------------------------------+ 8 // | This source file is subject to version 3.0 of the PHP license, | 9 // | that is bundled with this package in the file LICENSE, and is | 10 // | available through the world-wide-web at the following url: | 11 // | http://www.php.net/license/3_0.txt. | 12 // | If you did not receive a copy of the PHP license and are unable to | 13 // | obtain it through the world-wide-web, please send a note to | 14 // | license@php.net so we can mail you a copy immediately. | 15 // +----------------------------------------------------------------------+ 16 // | Author: Stig Bakken <ssb@php.net> | 17 // +----------------------------------------------------------------------+ 18 // 19 // $Id: Command.php,v 1.24 2004/05/16 15:43:30 pajoye Exp $ 20 21 22 require_once "PEAR.php"; 23 24 /** 25 * List of commands and what classes they are implemented in. 26 * @var array command => implementing class 27 */ 28 $GLOBALS['_PEAR_Command_commandlist'] = array(); 29 30 /** 31 * List of shortcuts to common commands. 32 * @var array shortcut => command 33 */ 34 $GLOBALS['_PEAR_Command_shortcuts'] = array(); 35 36 /** 37 * Array of command objects 38 * @var array class => object 39 */ 40 $GLOBALS['_PEAR_Command_objects'] = array(); 41 42 /** 43 * Which user interface class is being used. 44 * @var string class name 45 */ 46 $GLOBALS['_PEAR_Command_uiclass'] = 'PEAR_Frontend_CLI'; 47 48 /** 49 * Instance of $_PEAR_Command_uiclass. 50 * @var object 51 */ 52 $GLOBALS['_PEAR_Command_uiobject'] = null; 53 54 /** 55 * PEAR command class, a simple factory class for administrative 56 * commands. 57 * 58 * How to implement command classes: 59 * 60 * - The class must be called PEAR_Command_Nnn, installed in the 61 * "PEAR/Common" subdir, with a method called getCommands() that 62 * returns an array of the commands implemented by the class (see 63 * PEAR/Command/Install.php for an example). 64 * 65 * - The class must implement a run() function that is called with three 66 * params: 67 * 68 * (string) command name 69 * (array) assoc array with options, freely defined by each 70 * command, for example: 71 * array('force' => true) 72 * (array) list of the other parameters 73 * 74 * The run() function returns a PEAR_CommandResponse object. Use 75 * these methods to get information: 76 * 77 * int getStatus() Returns PEAR_COMMAND_(SUCCESS|FAILURE|PARTIAL) 78 * *_PARTIAL means that you need to issue at least 79 * one more command to complete the operation 80 * (used for example for validation steps). 81 * 82 * string getMessage() Returns a message for the user. Remember, 83 * no HTML or other interface-specific markup. 84 * 85 * If something unexpected happens, run() returns a PEAR error. 86 * 87 * - DON'T OUTPUT ANYTHING! Return text for output instead. 88 * 89 * - DON'T USE HTML! The text you return will be used from both Gtk, 90 * web and command-line interfaces, so for now, keep everything to 91 * plain text. 92 * 93 * - DON'T USE EXIT OR DIE! Always use pear errors. From static 94 * classes do PEAR::raiseError(), from other classes do 95 * $this->raiseError(). 96 */ 97 class PEAR_Command 98 { 99 // {{{ factory() 100 101 /** 102 * Get the right object for executing a command. 103 * 104 * @param string $command The name of the command 105 * @param object $config Instance of PEAR_Config object 106 * 107 * @return object the command object or a PEAR error 108 * 109 * @access public 110 * @static 111 */ 112 function factory($command, &$config) 113 { 114 if (empty($GLOBALS['_PEAR_Command_commandlist'])) { 115 PEAR_Command::registerCommands(); 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 return PEAR::raiseError("unknown command `$command'"); 122 } 123 $class = $GLOBALS['_PEAR_Command_commandlist'][$command]; 124 if (!class_exists($class)) { 125 return PEAR::raiseError("unknown command `$command'"); 126 } 127 $ui =& PEAR_Command::getFrontendObject(); 128 $obj = &new $class($ui, $config); 129 return $obj; 130 } 131 132 // }}} 133 // {{{ & getFrontendObject() 134 135 /** 136 * Get instance of frontend object. 137 * 138 * @return object 139 * @static 140 */ 141 function &getFrontendObject() 142 { 143 if (empty($GLOBALS['_PEAR_Command_uiobject'])) { 144 $GLOBALS['_PEAR_Command_uiobject'] = &new $GLOBALS['_PEAR_Command_uiclass']; 145 } 146 return $GLOBALS['_PEAR_Command_uiobject']; 147 } 148 149 // }}} 150 // {{{ & setFrontendClass() 151 152 /** 153 * Load current frontend class. 154 * 155 * @param string $uiclass Name of class implementing the frontend 156 * 157 * @return object the frontend object, or a PEAR error 158 * @static 159 */ 160 function &setFrontendClass($uiclass) 161 { 162 if (is_object($GLOBALS['_PEAR_Command_uiobject']) && 163 is_a($GLOBALS['_PEAR_Command_uiobject'], $uiclass)) { 164 return $GLOBALS['_PEAR_Command_uiobject']; 165 } 166 if (!class_exists($uiclass)) { 167 $file = str_replace('_', '/', $uiclass) . '.php'; 168 if (PEAR_Command::isIncludeable($file)) { 169 include_once $file; 170 } 171 } 172 if (class_exists($uiclass)) { 173 $obj = &new $uiclass; 174 // quick test to see if this class implements a few of the most 175 // important frontend methods 176 if (method_exists($obj, 'userConfirm')) { 177 $GLOBALS['_PEAR_Command_uiobject'] = &$obj; 178 $GLOBALS['_PEAR_Command_uiclass'] = $uiclass; 179 return $obj; 180 } else { 181 $err = PEAR::raiseError("not a frontend class: $uiclass"); 182 return $err; 183 } 184 } 185 $err = PEAR::raiseError("no such class: $uiclass"); 186 return $err; 187 } 188 189 // }}} 190 // {{{ setFrontendType() 191 192 // }}} 193 // {{{ isIncludeable() 194 195 /** 196 * @param string $path relative or absolute include path 197 * @return boolean 198 * @static 199 */ 200 function isIncludeable($path) 201 { 202 if (file_exists($path) && is_readable($path)) { 203 return true; 204 } 205 $ipath = explode(PATH_SEPARATOR, ini_get('include_path')); 206 foreach ($ipath as $include) { 207 $test = realpath($include . DIRECTORY_SEPARATOR . $path); 208 if (file_exists($test) && is_readable($test)) { 209 return true; 210 } 211 } 212 return false; 213 } 214 215 /** 216 * Set current frontend. 217 * 218 * @param string $uitype Name of the frontend type (for example "CLI") 219 * 220 * @return object the frontend object, or a PEAR error 221 * @static 222 */ 223 function setFrontendType($uitype) 224 { 225 $uiclass = 'PEAR_Frontend_' . $uitype; 226 return PEAR_Command::setFrontendClass($uiclass); 227 } 228 229 // }}} 230 // {{{ registerCommands() 231 232 /** 233 * Scan through the Command directory looking for classes 234 * and see what commands they implement. 235 * 236 * @param bool (optional) if FALSE (default), the new list of 237 * commands should replace the current one. If TRUE, 238 * new entries will be merged with old. 239 * 240 * @param string (optional) where (what directory) to look for 241 * classes, defaults to the Command subdirectory of 242 * the directory from where this file (__FILE__) is 243 * included. 244 * 245 * @return bool TRUE on success, a PEAR error on failure 246 * 247 * @access public 248 * @static 249 */ 250 function registerCommands($merge = false, $dir = null) 251 { 252 if ($dir === null) { 253 $dir = dirname(__FILE__) . '/Command'; 254 } 255 $dp = @opendir($dir); 256 if (empty($dp)) { 257 return PEAR::raiseError("registerCommands: opendir($dir) failed"); 258 } 259 if (!$merge) { 260 $GLOBALS['_PEAR_Command_commandlist'] = array(); 261 } 262 while ($entry = readdir($dp)) { 263 if ($entry{0} == '.' || substr($entry, -4) != '.php' || $entry == 'Common.php') { 264 continue; 265 } 266 $class = "PEAR_Command_".substr($entry, 0, -4); 267 $file = "$dir/$entry"; 268 include_once $file; 269 // List of commands 270 if (empty($GLOBALS['_PEAR_Command_objects'][$class])) { 271 $GLOBALS['_PEAR_Command_objects'][$class] = &new $class($ui, $config); 272 } 273 $implements = $GLOBALS['_PEAR_Command_objects'][$class]->getCommands(); 274 foreach ($implements as $command => $desc) { 275 $GLOBALS['_PEAR_Command_commandlist'][$command] = $class; 276 $GLOBALS['_PEAR_Command_commanddesc'][$command] = $desc; 277 } 278 $shortcuts = $GLOBALS['_PEAR_Command_objects'][$class]->getShortcuts(); 279 foreach ($shortcuts as $shortcut => $command) { 280 $GLOBALS['_PEAR_Command_shortcuts'][$shortcut] = $command; 281 } 282 } 283 @closedir($dp); 284 return true; 285 } 286 287 // }}} 288 // {{{ getCommands() 289 290 /** 291 * Get the list of currently supported commands, and what 292 * classes implement them. 293 * 294 * @return array command => implementing class 295 * 296 * @access public 297 * @static 298 */ 299 function getCommands() 300 { 301 if (empty($GLOBALS['_PEAR_Command_commandlist'])) { 302 PEAR_Command::registerCommands(); 303 } 304 return $GLOBALS['_PEAR_Command_commandlist']; 305 } 306 307 // }}} 308 // {{{ getShortcuts() 309 310 /** 311 * Get the list of command shortcuts. 312 * 313 * @return array shortcut => command 314 * 315 * @access public 316 * @static 317 */ 318 function getShortcuts() 319 { 320 if (empty($GLOBALS['_PEAR_Command_shortcuts'])) { 321 PEAR_Command::registerCommands(); 322 } 323 return $GLOBALS['_PEAR_Command_shortcuts']; 324 } 325 326 // }}} 327 // {{{ getGetoptArgs() 328 329 /** 330 * Compiles arguments for getopt. 331 * 332 * @param string $command command to get optstring for 333 * @param string $short_args (reference) short getopt format 334 * @param array $long_args (reference) long getopt format 335 * 336 * @return void 337 * 338 * @access public 339 * @static 340 */ 341 function getGetoptArgs($command, &$short_args, &$long_args) 342 { 343 if (empty($GLOBALS['_PEAR_Command_commandlist'])) { 344 PEAR_Command::registerCommands(); 345 } 346 if (!isset($GLOBALS['_PEAR_Command_commandlist'][$command])) { 347 return null; 348 } 349 $class = $GLOBALS['_PEAR_Command_commandlist'][$command]; 350 $obj = &$GLOBALS['_PEAR_Command_objects'][$class]; 351 return $obj->getGetoptArgs($command, $short_args, $long_args); 352 } 353 354 // }}} 355 // {{{ getDescription() 356 357 /** 358 * Get description for a command. 359 * 360 * @param string $command Name of the command 361 * 362 * @return string command description 363 * 364 * @access public 365 * @static 366 */ 367 function getDescription($command) 368 { 369 if (!isset($GLOBALS['_PEAR_Command_commanddesc'][$command])) { 370 return null; 371 } 372 return $GLOBALS['_PEAR_Command_commanddesc'][$command]; 373 } 374 375 // }}} 376 // {{{ getHelp() 377 378 /** 379 * Get help for command. 380 * 381 * @param string $command Name of the command to return help for 382 * 383 * @access public 384 * @static 385 */ 386 function getHelp($command) 387 { 388 $cmds = PEAR_Command::getCommands(); 389 if (isset($cmds[$command])) { 390 $class = $cmds[$command]; 391 return $GLOBALS['_PEAR_Command_objects'][$class]->getHelp($command); 392 } 393 return false; 394 } 395 // }}} 396 } 397 398 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 14:40:03 2007 | par Balluche grâce à PHPXref 0.7 |