PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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