PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PEAR/Command.php

https://bitbucket.org/ywarnier/chamilo-dev
PHP | 450 lines | 340 code | 10 blank | 100 comment | 5 complexity | cc7624244e679a146d90b53c2dae1f0b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  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 286494 2009-07-29 06:57: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.1
  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. {
  113. PEAR_Command :: registerCommands();
  114. }
  115. if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command]))
  116. {
  117. $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
  118. }
  119. if (! isset($GLOBALS['_PEAR_Command_commandlist'][$command]))
  120. {
  121. $a = PEAR :: raiseError("unknown command `$command'");
  122. return $a;
  123. }
  124. $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
  125. if (! class_exists($class))
  126. {
  127. require_once $GLOBALS['_PEAR_Command_objects'][$class];
  128. }
  129. if (! class_exists($class))
  130. {
  131. $a = PEAR :: raiseError("unknown command `$command'");
  132. return $a;
  133. }
  134. $ui = & PEAR_Command :: getFrontendObject();
  135. $obj = &new $class($ui, $config);
  136. return $obj;
  137. }
  138. // }}}
  139. // {{{ & getObject()
  140. function &getObject($command)
  141. {
  142. $class = $GLOBALS['_PEAR_Command_commandlist'][$command];
  143. if (! class_exists($class))
  144. {
  145. require_once $GLOBALS['_PEAR_Command_objects'][$class];
  146. }
  147. if (! class_exists($class))
  148. {
  149. return PEAR :: raiseError("unknown command `$command'");
  150. }
  151. $ui = & PEAR_Command :: getFrontendObject();
  152. $config = &PEAR_Config :: singleton();
  153. $obj = &new $class($ui, $config);
  154. return $obj;
  155. }
  156. // }}}
  157. // {{{ & getFrontendObject()
  158. /**
  159. * Get instance of frontend object.
  160. *
  161. * @return object|PEAR_Error
  162. * @static
  163. */
  164. function &getFrontendObject()
  165. {
  166. $a = &PEAR_Frontend :: singleton();
  167. return $a;
  168. }
  169. // }}}
  170. // {{{ & setFrontendClass()
  171. /**
  172. * Load current frontend class.
  173. *
  174. * @param string $uiclass Name of class implementing the frontend
  175. *
  176. * @return object the frontend object, or a PEAR error
  177. * @static
  178. */
  179. function &setFrontendClass($uiclass)
  180. {
  181. $a = &PEAR_Frontend :: setFrontendClass($uiclass);
  182. return $a;
  183. }
  184. // }}}
  185. // {{{ setFrontendType()
  186. /**
  187. * Set current frontend.
  188. *
  189. * @param string $uitype Name of the frontend type (for example "CLI")
  190. *
  191. * @return object the frontend object, or a PEAR error
  192. * @static
  193. */
  194. function setFrontendType($uitype)
  195. {
  196. $uiclass = 'PEAR_Frontend_' . $uitype;
  197. return PEAR_Command :: setFrontendClass($uiclass);
  198. }
  199. // }}}
  200. // {{{ registerCommands()
  201. /**
  202. * Scan through the Command directory looking for classes
  203. * and see what commands they implement.
  204. *
  205. * @param bool (optional) if FALSE (default), the new list of
  206. * commands should replace the current one. If TRUE,
  207. * new entries will be merged with old.
  208. *
  209. * @param string (optional) where (what directory) to look for
  210. * classes, defaults to the Command subdirectory of
  211. * the directory from where this file (__FILE__) is
  212. * included.
  213. *
  214. * @return bool TRUE on success, a PEAR error on failure
  215. *
  216. * @access public
  217. * @static
  218. */
  219. function registerCommands($merge = false, $dir = null)
  220. {
  221. $parser = new PEAR_XMLParser();
  222. if ($dir === null)
  223. {
  224. $dir = dirname(__FILE__) . '/Command';
  225. }
  226. if (! is_dir($dir))
  227. {
  228. return PEAR :: raiseError("registerCommands: opendir($dir) '$dir' does not exist or is not a directory");
  229. }
  230. $dp = @opendir($dir);
  231. if (empty($dp))
  232. {
  233. return PEAR :: raiseError("registerCommands: opendir($dir) failed");
  234. }
  235. if (! $merge)
  236. {
  237. $GLOBALS['_PEAR_Command_commandlist'] = array();
  238. }
  239. while ($file = readdir($dp))
  240. {
  241. if ($file{0} == '.' || substr($file, - 4) != '.xml')
  242. {
  243. continue;
  244. }
  245. $f = substr($file, 0, - 4);
  246. $class = "PEAR_Command_" . $f;
  247. // List of commands
  248. if (empty($GLOBALS['_PEAR_Command_objects'][$class]))
  249. {
  250. $GLOBALS['_PEAR_Command_objects'][$class] = "$dir/" . $f . '.php';
  251. }
  252. $parser->parse(file_get_contents("$dir/$file"));
  253. $implements = $parser->getData();
  254. foreach ($implements as $command => $desc)
  255. {
  256. if ($command == 'attribs')
  257. {
  258. continue;
  259. }
  260. if (isset($GLOBALS['_PEAR_Command_commandlist'][$command]))
  261. {
  262. return PEAR :: raiseError('Command "' . $command . '" already registered in ' . 'class "' . $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
  263. }
  264. $GLOBALS['_PEAR_Command_commandlist'][$command] = $class;
  265. $GLOBALS['_PEAR_Command_commanddesc'][$command] = $desc['summary'];
  266. if (isset($desc['shortcut']))
  267. {
  268. $shortcut = $desc['shortcut'];
  269. if (isset($GLOBALS['_PEAR_Command_shortcuts'][$shortcut]))
  270. {
  271. return PEAR :: raiseError('Command shortcut "' . $shortcut . '" already ' . 'registered to command "' . $command . '" in class "' . $GLOBALS['_PEAR_Command_commandlist'][$command] . '"');
  272. }
  273. $GLOBALS['_PEAR_Command_shortcuts'][$shortcut] = $command;
  274. }
  275. if (isset($desc['options']) && $desc['options'])
  276. {
  277. foreach ($desc['options'] as $oname => $option)
  278. {
  279. if (isset($option['shortopt']) && strlen($option['shortopt']) > 1)
  280. {
  281. return PEAR :: raiseError('Option "' . $oname . '" short option "' . $option['shortopt'] . '" must be ' . 'only 1 character in Command "' . $command . '" in class "' . $class . '"');
  282. }
  283. }
  284. }
  285. }
  286. }
  287. ksort($GLOBALS['_PEAR_Command_shortcuts']);
  288. ksort($GLOBALS['_PEAR_Command_commandlist']);
  289. @closedir($dp);
  290. return true;
  291. }
  292. // }}}
  293. // {{{ getCommands()
  294. /**
  295. * Get the list of currently supported commands, and what
  296. * classes implement them.
  297. *
  298. * @return array command => implementing class
  299. *
  300. * @access public
  301. * @static
  302. */
  303. function getCommands()
  304. {
  305. if (empty($GLOBALS['_PEAR_Command_commandlist']))
  306. {
  307. PEAR_Command :: registerCommands();
  308. }
  309. return $GLOBALS['_PEAR_Command_commandlist'];
  310. }
  311. // }}}
  312. // {{{ getShortcuts()
  313. /**
  314. * Get the list of command shortcuts.
  315. *
  316. * @return array shortcut => command
  317. *
  318. * @access public
  319. * @static
  320. */
  321. function getShortcuts()
  322. {
  323. if (empty($GLOBALS['_PEAR_Command_shortcuts']))
  324. {
  325. PEAR_Command :: registerCommands();
  326. }
  327. return $GLOBALS['_PEAR_Command_shortcuts'];
  328. }
  329. // }}}
  330. // {{{ getGetoptArgs()
  331. /**
  332. * Compiles arguments for getopt.
  333. *
  334. * @param string $command command to get optstring for
  335. * @param string $short_args (reference) short getopt format
  336. * @param array $long_args (reference) long getopt format
  337. *
  338. * @return void
  339. *
  340. * @access public
  341. * @static
  342. */
  343. function getGetoptArgs($command, &$short_args, &$long_args)
  344. {
  345. if (empty($GLOBALS['_PEAR_Command_commandlist']))
  346. {
  347. PEAR_Command :: registerCommands();
  348. }
  349. if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command]))
  350. {
  351. $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
  352. }
  353. if (! isset($GLOBALS['_PEAR_Command_commandlist'][$command]))
  354. {
  355. return null;
  356. }
  357. $obj = &PEAR_Command :: getObject($command);
  358. return $obj->getGetoptArgs($command, $short_args, $long_args);
  359. }
  360. // }}}
  361. // {{{ getDescription()
  362. /**
  363. * Get description for a command.
  364. *
  365. * @param string $command Name of the command
  366. *
  367. * @return string command description
  368. *
  369. * @access public
  370. * @static
  371. */
  372. function getDescription($command)
  373. {
  374. if (! isset($GLOBALS['_PEAR_Command_commanddesc'][$command]))
  375. {
  376. return null;
  377. }
  378. return $GLOBALS['_PEAR_Command_commanddesc'][$command];
  379. }
  380. // }}}
  381. // {{{ getHelp()
  382. /**
  383. * Get help for command.
  384. *
  385. * @param string $command Name of the command to return help for
  386. *
  387. * @access public
  388. * @static
  389. */
  390. function getHelp($command)
  391. {
  392. $cmds = PEAR_Command :: getCommands();
  393. if (isset($GLOBALS['_PEAR_Command_shortcuts'][$command]))
  394. {
  395. $command = $GLOBALS['_PEAR_Command_shortcuts'][$command];
  396. }
  397. if (isset($cmds[$command]))
  398. {
  399. $obj = &PEAR_Command :: getObject($command);
  400. return $obj->getHelp($command);
  401. }
  402. return false;
  403. }
  404. // }}}
  405. }