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

/script/lib/PEAR/Command/Common.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 293 lines | 189 code | 28 blank | 76 comment | 28 complexity | b10ce0fe26802715474fbd25b8634f65 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0
  1. <?php
  2. /**
  3. * PEAR_Command_Common base 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: Common.php 276383 2009-02-24 23:39:37Z dufuz $
  14. * @link http://pear.php.net/package/PEAR
  15. * @since File available since Release 0.1
  16. */
  17. /**
  18. * base class
  19. */
  20. require_once 'PEAR.php';
  21. /**
  22. * PEAR commands base class
  23. *
  24. * @category pear
  25. * @package PEAR
  26. * @author Stig Bakken <ssb@php.net>
  27. * @author Greg Beaver <cellog@php.net>
  28. * @copyright 1997-2009 The Authors
  29. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  30. * @version Release: 1.9.1
  31. * @link http://pear.php.net/package/PEAR
  32. * @since Class available since Release 0.1
  33. */
  34. class PEAR_Command_Common extends PEAR
  35. {
  36. /**
  37. * PEAR_Config object used to pass user system and configuration
  38. * on when executing commands
  39. *
  40. * @var PEAR_Config
  41. */
  42. var $config;
  43. /**
  44. * @var PEAR_Registry
  45. * @access protected
  46. */
  47. var $_registry;
  48. /**
  49. * User Interface object, for all interaction with the user.
  50. * @var object
  51. */
  52. var $ui;
  53. var $_deps_rel_trans = array('lt' => '<', 'le' => '<=', 'eq' => '=', 'ne' => '!=', 'gt' => '>', 'ge' => '>=',
  54. 'has' => '==');
  55. var $_deps_type_trans = array('pkg' => 'package', 'ext' => 'extension', 'php' => 'PHP',
  56. 'prog' => 'external program', 'ldlib' => 'external library for linking',
  57. 'rtlib' => 'external runtime library', 'os' => 'operating system', 'websrv' => 'web server',
  58. 'sapi' => 'SAPI backend');
  59. /**
  60. * PEAR_Command_Common constructor.
  61. *
  62. * @access public
  63. */
  64. function PEAR_Command_Common(&$ui, &$config)
  65. {
  66. parent :: PEAR();
  67. $this->config = &$config;
  68. $this->ui = &$ui;
  69. }
  70. /**
  71. * Return a list of all the commands defined by this class.
  72. * @return array list of commands
  73. * @access public
  74. */
  75. function getCommands()
  76. {
  77. $ret = array();
  78. foreach (array_keys($this->commands) as $command)
  79. {
  80. $ret[$command] = $this->commands[$command]['summary'];
  81. }
  82. return $ret;
  83. }
  84. /**
  85. * Return a list of all the command shortcuts defined by this class.
  86. * @return array shortcut => command
  87. * @access public
  88. */
  89. function getShortcuts()
  90. {
  91. $ret = array();
  92. foreach (array_keys($this->commands) as $command)
  93. {
  94. if (isset($this->commands[$command]['shortcut']))
  95. {
  96. $ret[$this->commands[$command]['shortcut']] = $command;
  97. }
  98. }
  99. return $ret;
  100. }
  101. function getOptions($command)
  102. {
  103. $shortcuts = $this->getShortcuts();
  104. if (isset($shortcuts[$command]))
  105. {
  106. $command = $shortcuts[$command];
  107. }
  108. if (isset($this->commands[$command]) && isset($this->commands[$command]['options']))
  109. {
  110. return $this->commands[$command]['options'];
  111. }
  112. return null;
  113. }
  114. function getGetoptArgs($command, &$short_args, &$long_args)
  115. {
  116. $short_args = '';
  117. $long_args = array();
  118. if (empty($this->commands[$command]) || empty($this->commands[$command]['options']))
  119. {
  120. return;
  121. }
  122. reset($this->commands[$command]['options']);
  123. while (list($option, $info) = each($this->commands[$command]['options']))
  124. {
  125. $larg = $sarg = '';
  126. if (isset($info['arg']))
  127. {
  128. if ($info['arg']{0} == '(')
  129. {
  130. $larg = '==';
  131. $sarg = '::';
  132. $arg = substr($info['arg'], 1, - 1);
  133. }
  134. else
  135. {
  136. $larg = '=';
  137. $sarg = ':';
  138. $arg = $info['arg'];
  139. }
  140. }
  141. if (isset($info['shortopt']))
  142. {
  143. $short_args .= $info['shortopt'] . $sarg;
  144. }
  145. $long_args[] = $option . $larg;
  146. }
  147. }
  148. /**
  149. * Returns the help message for the given command
  150. *
  151. * @param string $command The command
  152. * @return mixed A fail string if the command does not have help or
  153. * a two elements array containing [0]=>help string,
  154. * [1]=> help string for the accepted cmd args
  155. */
  156. function getHelp($command)
  157. {
  158. $config = &PEAR_Config :: singleton();
  159. if (! isset($this->commands[$command]))
  160. {
  161. return "No such command \"$command\"";
  162. }
  163. $help = null;
  164. if (isset($this->commands[$command]['doc']))
  165. {
  166. $help = $this->commands[$command]['doc'];
  167. }
  168. if (empty($help))
  169. {
  170. // XXX (cox) Fallback to summary if there is no doc (show both?)
  171. if (! isset($this->commands[$command]['summary']))
  172. {
  173. return "No help for command \"$command\"";
  174. }
  175. $help = $this->commands[$command]['summary'];
  176. }
  177. if (preg_match_all('/{config\s+([^\}]+)}/e', $help, $matches))
  178. {
  179. foreach ($matches[0] as $k => $v)
  180. {
  181. $help = preg_replace("/$v/", $config->get($matches[1][$k]), $help);
  182. }
  183. }
  184. return array($help, $this->getHelpArgs($command));
  185. }
  186. /**
  187. * Returns the help for the accepted arguments of a command
  188. *
  189. * @param string $command
  190. * @return string The help string
  191. */
  192. function getHelpArgs($command)
  193. {
  194. if (isset($this->commands[$command]['options']) && count($this->commands[$command]['options']))
  195. {
  196. $help = "Options:\n";
  197. foreach ($this->commands[$command]['options'] as $k => $v)
  198. {
  199. if (isset($v['arg']))
  200. {
  201. if ($v['arg'][0] == '(')
  202. {
  203. $arg = substr($v['arg'], 1, - 1);
  204. $sapp = " [$arg]";
  205. $lapp = "[=$arg]";
  206. }
  207. else
  208. {
  209. $sapp = " $v[arg]";
  210. $lapp = "=$v[arg]";
  211. }
  212. }
  213. else
  214. {
  215. $sapp = $lapp = "";
  216. }
  217. if (isset($v['shortopt']))
  218. {
  219. $s = $v['shortopt'];
  220. $help .= " -$s$sapp, --$k$lapp\n";
  221. }
  222. else
  223. {
  224. $help .= " --$k$lapp\n";
  225. }
  226. $p = " ";
  227. $doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
  228. $help .= " $doc\n";
  229. }
  230. return $help;
  231. }
  232. return null;
  233. }
  234. function run($command, $options, $params)
  235. {
  236. if (empty($this->commands[$command]['function']))
  237. {
  238. // look for shortcuts
  239. foreach (array_keys($this->commands) as $cmd)
  240. {
  241. if (isset($this->commands[$cmd]['shortcut']) && $this->commands[$cmd]['shortcut'] == $command)
  242. {
  243. if (empty($this->commands[$cmd]['function']))
  244. {
  245. return $this->raiseError("unknown command `$command'");
  246. }
  247. else
  248. {
  249. $func = $this->commands[$cmd]['function'];
  250. }
  251. $command = $cmd;
  252. //$command = $this->commands[$cmd]['function'];
  253. break;
  254. }
  255. }
  256. }
  257. else
  258. {
  259. $func = $this->commands[$command]['function'];
  260. }
  261. return $this->$func($command, $options, $params);
  262. }
  263. }