PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/pear/PEAR/Command.php

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