PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/manager/pear/PEAR/Command.php

http://punchcms.googlecode.com/
PHP | 412 lines | 303 code | 7 blank | 102 comment | 5 complexity | 2ef0505718a51a73b9a241110071eedd MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * PEAR_Command, command pattern class
  4. *
  5. * PHP versions 4 and 5
  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
  14. * @package PEAR
  15. * @author Stig Bakken <ssb@php.net>
  16. * @author Greg Beaver <cellog@php.net>
  17. * @copyright 1997-2006 The PHP Group
  18. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  19. * @version CVS: $Id: Command.php,v 1.36.2.1 2006/06/16 13:01:59 pajoye Exp $
  20. * @link http://pear.php.net/package/PEAR
  21. * @since File available since Release 0.1
  22. */
  23. /**
  24. * Needed for error handling
  25. */
  26. require_once 'PEAR.php';
  27. require_once 'PEAR/Frontend.php';
  28. require_once 'PEAR/XMLParser.php';
  29. /**
  30. * List of commands and what classes they are implemented in.
  31. * @var array command => implementing class
  32. */
  33. $GLOBALS['_PEAR_Command_commandlist'] = array();
  34. /**
  35. * List of shortcuts to common commands.
  36. * @var array shortcut => command
  37. */
  38. $GLOBALS['_PEAR_Command_shortcuts'] = array();
  39. /**
  40. * Array of command objects
  41. * @var array class => object
  42. */
  43. $GLOBALS['_PEAR_Command_objects'] = array();
  44. /**
  45. * PEAR command class, a simple factory class for administrative
  46. * commands.
  47. *
  48. * How to implement command classes:
  49. *
  50. * - The class must be called PEAR_Command_Nnn, installed in the
  51. * "PEAR/Common" subdir, with a method called getCommands() that
  52. * returns an array of the commands implemented by the class (see
  53. * PEAR/Command/Install.php for an example).
  54. *
  55. * - The class must implement a run() function that is called with three
  56. * params:
  57. *
  58. * (string) command name
  59. * (array) assoc array with options, freely defined by each
  60. * command, for example:
  61. * array('force' => true)
  62. * (array) list of the other parameters
  63. *
  64. * The run() function returns a PEAR_CommandResponse object. Use
  65. * these methods to get information:
  66. *
  67. * int getStatus() Returns PEAR_COMMAND_(SUCCESS|FAILURE|PARTIAL)
  68. * *_PARTIAL means that you need to issue at least
  69. * one more command to complete the operation
  70. * (used for example for validation steps).
  71. *
  72. * string getMessage() Returns a message for the user. Remember,
  73. * no HTML or other interface-specific markup.
  74. *
  75. * If something unexpected happens, run() returns a PEAR error.
  76. *
  77. * - DON'T OUTPUT ANYTHING! Return text for output instead.
  78. *
  79. * - DON'T USE HTML! The text you return will be used from both Gtk,
  80. * web and command-line interfaces, so for now, keep everything to
  81. * plain text.
  82. *
  83. * - DON'T USE EXIT OR DIE! Always use pear errors. From static
  84. * classes do PEAR::raiseError(), from other classes do
  85. * $this->raiseError().
  86. * @category pear
  87. * @package PEAR
  88. * @author Stig Bakken <ssb@php.net>
  89. * @author Greg Beaver <cellog@php.net>
  90. * @copyright 1997-2006 The PHP Group
  91. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  92. * @version Release: 1.4.10
  93. * @link http://pear.php.net/package/PEAR
  94. * @since Class available since Release 0.1
  95. */
  96. class PEAR_Command
  97. {
  98. // {{{ factory()
  99. /**
  100. * Get the right object for executing a command.
  101. *
  102. * @param string $command The name of the command
  103. * @param object $config Instance of PEAR_Config object
  104. *
  105. * @return object the command object or a PEAR error
  106. *
  107. * @access public
  108. * @static
  109. */
  110. function &factory($command, &$config)
  111. {
  112. if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
  113. PEAR_Command::registerCommands();
  114. }
  115. if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
  116. $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
  117. }
  118. if (!isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
  119. $a = PEAR::raiseError("unknown command `$command'");
  120. return $a;
  121. }
  122. $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
  123. if (!class_exists($class)) {
  124. require_once $GLOBALS['_PEAR_Command_objects'][$class];
  125. }
  126. if (!class_exists($class)) {
  127. $a = PEAR::raiseError("unknown command `$command'");
  128. return $a;
  129. }
  130. $ui =& PEAR_Command::getFrontendObject();
  131. $obj = &new $class($ui, $config);
  132. return $obj;
  133. }
  134. // }}}
  135. // {{{ & getObject()
  136. function &getObject($command)
  137. {
  138. $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
  139. if (!class_exists($class)) {
  140. require_once $GLOBALS['_PEAR_Command_objects'][$class];
  141. }
  142. if (!class_exists($class)) {
  143. return PEAR::raiseError("unknown command `$command'");
  144. }
  145. $ui =& PEAR_Command::getFrontendObject();
  146. $config = &PEAR_Config::singleton();
  147. $obj = &new $class($ui, $config);
  148. return $obj;
  149. }
  150. // }}}
  151. // {{{ & getFrontendObject()
  152. /**
  153. * Get instance of frontend object.
  154. *
  155. * @return object|PEAR_Error
  156. * @static
  157. */
  158. function &getFrontendObject()
  159. {
  160. $a = &PEAR_Frontend::singleton();
  161. return $a;
  162. }
  163. // }}}
  164. // {{{ & setFrontendClass()
  165. /**
  166. * Load current frontend class.
  167. *
  168. * @param string $uiclass Name of class implementing the frontend
  169. *
  170. * @return object the frontend object, or a PEAR error
  171. * @static
  172. */
  173. function &setFrontendClass($uiclass)
  174. {
  175. $a = &PEAR_Frontend::setFrontendClass($uiclass);
  176. return $a;
  177. }
  178. // }}}
  179. // {{{ setFrontendType()
  180. /**
  181. * Set current frontend.
  182. *
  183. * @param string $uitype Name of the frontend type (for example "CLI")
  184. *
  185. * @return object the frontend object, or a PEAR error
  186. * @static
  187. */
  188. function setFrontendType($uitype)
  189. {
  190. $uiclass = 'PEAR_Frontend_' . $uitype;
  191. return PEAR_Command::setFrontendClass($uiclass);
  192. }
  193. // }}}
  194. // {{{ registerCommands()
  195. /**
  196. * Scan through the Command directory looking for classes
  197. * and see what commands they implement.
  198. *
  199. * @param bool (optional) if FALSE (default), the new list of
  200. * commands should replace the current one. If TRUE,
  201. * new entries will be merged with old.
  202. *
  203. * @param string (optional) where (what directory) to look for
  204. * classes, defaults to the Command subdirectory of
  205. * the directory from where this file (__FILE__) is
  206. * included.
  207. *
  208. * @return bool TRUE on success, a PEAR error on failure
  209. *
  210. * @access public
  211. * @static
  212. */
  213. function registerCommands($merge = false, $dir = null)
  214. {
  215. $parser = new PEAR_XMLParser;
  216. if ($dir === null) {
  217. $dir = dirname(__FILE__) . '/Command';
  218. }
  219. if (!@is_dir($dir)) {
  220. return PEAR::raiseError("registerCommands: opendir($dir) '$dir' does not exist or is not a directory");
  221. }
  222. $dp = @opendir($dir);
  223. if (empty($dp)) {
  224. return PEAR::raiseError("registerCommands: opendir($dir) failed");
  225. }
  226. if (!$merge) {
  227. $GLOBALS['_PEAR_Command_commandlist'] = array();
  228. }
  229. while ($entry = readdir($dp)) {
  230. if ($entry{0} == '.' || substr($entry, -4) != '.xml') {
  231. continue;
  232. }
  233. $class = "PEAR_Command_".substr($entry, 0, -4);
  234. $file = "$dir/$entry";
  235. $parser->parse(file_get_contents($file));
  236. $implements = $parser->getData();
  237. // List of commands
  238. if (empty($GLOBALS['_PEAR_Command_objects'][$class])) {
  239. $GLOBALS['_PEAR_Command_objects'][$class] = "$dir/" . substr($entry, 0, -4) .
  240. '.php';
  241. }
  242. foreach ($implements as $command => $desc) {
  243. if ($command == 'attribs') {
  244. continue;
  245. }
  246. if (isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
  247. return PEAR::raiseError('Command "' . $command . '" already registered in ' .
  248. 'class "' . $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
  249. }
  250. $GLOBALS['_PEAR_Command_commandlist'][$command] = $class;
  251. $GLOBALS['_PEAR_Command_commanddesc'][$command] = $desc['summary'];
  252. if (isset($desc['shortcut'])) {
  253. $shortcut = $desc['shortcut'];
  254. if (isset($GLOBALS['_PEAR_Command_shortcuts'][$shortcut])) {
  255. return PEAR::raiseError('Command shortcut "' . $shortcut . '" already ' .
  256. 'registered to command "' . $command . '" in class "' .
  257. $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
  258. }
  259. $GLOBALS['_PEAR_Command_shortcuts'][$shortcut] = $command;
  260. }
  261. if (isset($desc['options']) && $desc['options']) {
  262. foreach ($desc['options'] as $oname => $option) {
  263. if (isset($option['shortopt']) && strlen($option['shortopt']) > 1) {
  264. return PEAR::raiseError('Option "' . $oname . '" short option "' .
  265. $option['shortopt'] . '" must be ' .
  266. 'only 1 character in Command "' . $command . '" in class "' .
  267. $class . '"');
  268. }
  269. }
  270. }
  271. }
  272. }
  273. ksort($GLOBALS['_PEAR_Command_shortcuts']);
  274. ksort($GLOBALS['_PEAR_Command_commandlist']);
  275. @closedir($dp);
  276. return true;
  277. }
  278. // }}}
  279. // {{{ getCommands()
  280. /**
  281. * Get the list of currently supported commands, and what
  282. * classes implement them.
  283. *
  284. * @return array command => implementing class
  285. *
  286. * @access public
  287. * @static
  288. */
  289. function getCommands()
  290. {
  291. if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
  292. PEAR_Command::registerCommands();
  293. }
  294. return $GLOBALS['_PEAR_Command_commandlist'];
  295. }
  296. // }}}
  297. // {{{ getShortcuts()
  298. /**
  299. * Get the list of command shortcuts.
  300. *
  301. * @return array shortcut => command
  302. *
  303. * @access public
  304. * @static
  305. */
  306. function getShortcuts()
  307. {
  308. if (empty($GLOBALS['_PEAR_Command_shortcuts'])) {
  309. PEAR_Command::registerCommands();
  310. }
  311. return $GLOBALS['_PEAR_Command_shortcuts'];
  312. }
  313. // }}}
  314. // {{{ getGetoptArgs()
  315. /**
  316. * Compiles arguments for getopt.
  317. *
  318. * @param string $command command to get optstring for
  319. * @param string $short_args (reference) short getopt format
  320. * @param array $long_args (reference) long getopt format
  321. *
  322. * @return void
  323. *
  324. * @access public
  325. * @static
  326. */
  327. function getGetoptArgs($command, &$short_args, &$long_args)
  328. {
  329. if (empty($GLOBALS['_PEAR_Command_commandlist'])) {
  330. PEAR_Command::registerCommands();
  331. }
  332. if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
  333. $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
  334. }
  335. if (!isset($GLOBALS['_PEAR_Command_commandlist'][$command])) {
  336. return null;
  337. }
  338. $obj = &PEAR_Command::getObject($command);
  339. return $obj->getGetoptArgs($command, $short_args, $long_args);
  340. }
  341. // }}}
  342. // {{{ getDescription()
  343. /**
  344. * Get description for a command.
  345. *
  346. * @param string $command Name of the command
  347. *
  348. * @return string command description
  349. *
  350. * @access public
  351. * @static
  352. */
  353. function getDescription($command)
  354. {
  355. if (!isset($GLOBALS['_PEAR_Command_commanddesc'][$command])) {
  356. return null;
  357. }
  358. return $GLOBALS['_PEAR_Command_commanddesc'][$command];
  359. }
  360. // }}}
  361. // {{{ getHelp()
  362. /**
  363. * Get help for command.
  364. *
  365. * @param string $command Name of the command to return help for
  366. *
  367. * @access public
  368. * @static
  369. */
  370. function getHelp($command)
  371. {
  372. $cmds = PEAR_Command::getCommands();
  373. if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command])) {
  374. $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
  375. }
  376. if (isset($cmds[$command])) {
  377. $obj = &PEAR_Command::getObject($command);
  378. return $obj->getHelp($command);
  379. }
  380. return false;
  381. }
  382. // }}}
  383. }
  384. ?>