Common.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | [email protected] so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Sæther Bakken <[email protected]> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Common.php,v 1.24 2004/01/08 17:33:13 sniper Exp $
  20. require_once "PEAR.php";
  21. class PEAR_Command_Common extends PEAR
  22. {
  23. // {{{ properties
  24. /**
  25. * PEAR_Config object used to pass user system and configuration
  26. * on when executing commands
  27. *
  28. * @var object
  29. */
  30. var $config;
  31. /**
  32. * User Interface object, for all interaction with the user.
  33. * @var object
  34. */
  35. var $ui;
  36. var $_deps_rel_trans = array(
  37. 'lt' => '<',
  38. 'le' => '<=',
  39. 'eq' => '=',
  40. 'ne' => '!=',
  41. 'gt' => '>',
  42. 'ge' => '>=',
  43. 'has' => '=='
  44. );
  45. var $_deps_type_trans = array(
  46. 'pkg' => 'package',
  47. 'extension' => 'extension',
  48. 'php' => 'PHP',
  49. 'prog' => 'external program',
  50. 'ldlib' => 'external library for linking',
  51. 'rtlib' => 'external runtime library',
  52. 'os' => 'operating system',
  53. 'websrv' => 'web server',
  54. 'sapi' => 'SAPI backend'
  55. );
  56. // }}}
  57. // {{{ constructor
  58. /**
  59. * PEAR_Command_Common constructor.
  60. *
  61. * @access public
  62. */
  63. function PEAR_Command_Common(&$ui, &$config)
  64. {
  65. parent::PEAR();
  66. $this->config = &$config;
  67. $this->ui = &$ui;
  68. }
  69. // }}}
  70. // {{{ getCommands()
  71. /**
  72. * Return a list of all the commands defined by this class.
  73. * @return array list of commands
  74. * @access public
  75. */
  76. function getCommands()
  77. {
  78. $ret = array();
  79. foreach (array_keys($this->commands) as $command) {
  80. $ret[$command] = $this->commands[$command]['summary'];
  81. }
  82. return $ret;
  83. }
  84. // }}}
  85. // {{{ getShortcuts()
  86. /**
  87. * Return a list of all the command shortcuts defined by this class.
  88. * @return array shortcut => command
  89. * @access public
  90. */
  91. function getShortcuts()
  92. {
  93. $ret = array();
  94. foreach (array_keys($this->commands) as $command) {
  95. if (isset($this->commands[$command]['shortcut'])) {
  96. $ret[$this->commands[$command]['shortcut']] = $command;
  97. }
  98. }
  99. return $ret;
  100. }
  101. // }}}
  102. // {{{ getOptions()
  103. function getOptions($command)
  104. {
  105. return @$this->commands[$command]['options'];
  106. }
  107. // }}}
  108. // {{{ getGetoptArgs()
  109. function getGetoptArgs($command, &$short_args, &$long_args)
  110. {
  111. $short_args = "";
  112. $long_args = array();
  113. if (empty($this->commands[$command])) {
  114. return;
  115. }
  116. reset($this->commands[$command]);
  117. while (list($option, $info) = each($this->commands[$command]['options'])) {
  118. $larg = $sarg = '';
  119. if (isset($info['arg'])) {
  120. if ($info['arg']{0} == '(') {
  121. $larg = '==';
  122. $sarg = '::';
  123. $arg = substr($info['arg'], 1, -1);
  124. } else {
  125. $larg = '=';
  126. $sarg = ':';
  127. $arg = $info['arg'];
  128. }
  129. }
  130. if (isset($info['shortopt'])) {
  131. $short_args .= $info['shortopt'] . $sarg;
  132. }
  133. $long_args[] = $option . $larg;
  134. }
  135. }
  136. // }}}
  137. // {{{ getHelp()
  138. /**
  139. * Returns the help message for the given command
  140. *
  141. * @param string $command The command
  142. * @return mixed A fail string if the command does not have help or
  143. * a two elements array containing [0]=>help string,
  144. * [1]=> help string for the accepted cmd args
  145. */
  146. function getHelp($command)
  147. {
  148. $config = &PEAR_Config::singleton();
  149. $help = @$this->commands[$command]['doc'];
  150. if (empty($help)) {
  151. // XXX (cox) Fallback to summary if there is no doc (show both?)
  152. if (!$help = @$this->commands[$command]['summary']) {
  153. return "No help for command \"$command\"";
  154. }
  155. }
  156. if (preg_match_all('/{config\s+([^\}]+)}/e', $help, $matches)) {
  157. foreach($matches[0] as $k => $v) {
  158. $help = preg_replace("/$v/", $config->get($matches[1][$k]), $help);
  159. }
  160. }
  161. return array($help, $this->getHelpArgs($command));
  162. }
  163. // }}}
  164. // {{{ getHelpArgs()
  165. /**
  166. * Returns the help for the accepted arguments of a command
  167. *
  168. * @param string $command
  169. * @return string The help string
  170. */
  171. function getHelpArgs($command)
  172. {
  173. if (isset($this->commands[$command]['options']) &&
  174. count($this->commands[$command]['options']))
  175. {
  176. $help = "Options:\n";
  177. foreach ($this->commands[$command]['options'] as $k => $v) {
  178. if (isset($v['arg'])) {
  179. if ($v['arg']{0} == '(') {
  180. $arg = substr($v['arg'], 1, -1);
  181. $sapp = " [$arg]";
  182. $lapp = "[=$arg]";
  183. } else {
  184. $sapp = " $v[arg]";
  185. $lapp = "=$v[arg]";
  186. }
  187. } else {
  188. $sapp = $lapp = "";
  189. }
  190. if (isset($v['shortopt'])) {
  191. $s = $v['shortopt'];
  192. @$help .= " -$s$sapp, --$k$lapp\n";
  193. } else {
  194. @$help .= " --$k$lapp\n";
  195. }
  196. $p = " ";
  197. $doc = rtrim(str_replace("\n", "\n$p", $v['doc']));
  198. $help .= " $doc\n";
  199. }
  200. return $help;
  201. }
  202. return null;
  203. }
  204. // }}}
  205. // {{{ run()
  206. function run($command, $options, $params)
  207. {
  208. $func = @$this->commands[$command]['function'];
  209. if (empty($func)) {
  210. // look for shortcuts
  211. foreach (array_keys($this->commands) as $cmd) {
  212. if (@$this->commands[$cmd]['shortcut'] == $command) {
  213. $command = $cmd;
  214. $func = @$this->commands[$command]['function'];
  215. if (empty($func)) {
  216. return $this->raiseError("unknown command `$command'");
  217. }
  218. break;
  219. }
  220. }
  221. }
  222. return $this->$func($command, $options, $params);
  223. }
  224. // }}}
  225. }
  226. ?>