PageRenderTime 65ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/php/PEAR/Command.php

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