Registry.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. // /* vim: set expandtab tabstop=4 shiftwidth=4: */
  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 Bakken <[email protected]> |
  17. // | |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Registry.php,v 1.36 2004/01/08 17:33:13 sniper Exp $
  21. require_once 'PEAR/Command/Common.php';
  22. require_once 'PEAR/Registry.php';
  23. require_once 'PEAR/Config.php';
  24. class PEAR_Command_Registry extends PEAR_Command_Common
  25. {
  26. // {{{ properties
  27. var $commands = array(
  28. 'list' => array(
  29. 'summary' => 'List Installed Packages',
  30. 'function' => 'doList',
  31. 'shortcut' => 'l',
  32. 'options' => array(),
  33. 'doc' => '[package]
  34. If invoked without parameters, this command lists the PEAR packages
  35. installed in your php_dir ({config php_dir)). With a parameter, it
  36. lists the files in that package.
  37. ',
  38. ),
  39. 'shell-test' => array(
  40. 'summary' => 'Shell Script Test',
  41. 'function' => 'doShellTest',
  42. 'shortcut' => 'st',
  43. 'options' => array(),
  44. 'doc' => '<package> [[relation] version]
  45. Tests if a package is installed in the system. Will exit(1) if it is not.
  46. <relation> The version comparison operator. One of:
  47. <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne
  48. <version> The version to compare with
  49. '),
  50. 'info' => array(
  51. 'summary' => 'Display information about a package',
  52. 'function' => 'doInfo',
  53. 'shortcut' => 'in',
  54. 'options' => array(),
  55. 'doc' => '<package>
  56. Displays information about a package. The package argument may be a
  57. local package file, an URL to a package file, or the name of an
  58. installed package.'
  59. )
  60. );
  61. // }}}
  62. // {{{ constructor
  63. /**
  64. * PEAR_Command_Registry constructor.
  65. *
  66. * @access public
  67. */
  68. function PEAR_Command_Registry(&$ui, &$config)
  69. {
  70. parent::PEAR_Command_Common($ui, $config);
  71. }
  72. // }}}
  73. // {{{ doList()
  74. function _sortinfo($a, $b)
  75. {
  76. return strcmp($a['package'], $b['package']);
  77. }
  78. function doList($command, $options, $params)
  79. {
  80. $reg = new PEAR_Registry($this->config->get('php_dir'));
  81. if (sizeof($params) == 0) {
  82. $installed = $reg->packageInfo();
  83. usort($installed, array(&$this, '_sortinfo'));
  84. $i = $j = 0;
  85. $data = array(
  86. 'caption' => 'Installed packages:',
  87. 'border' => true,
  88. 'headline' => array('Package', 'Version', 'State')
  89. );
  90. foreach ($installed as $package) {
  91. $data['data'][] = array($package['package'],
  92. $package['version'],
  93. @$package['release_state']);
  94. }
  95. if (count($installed)==0) {
  96. $data = '(no packages installed)';
  97. }
  98. $this->ui->outputData($data, $command);
  99. } else {
  100. if (file_exists($params[0]) && !is_dir($params[0])) {
  101. include_once "PEAR/Common.php";
  102. $obj = &new PEAR_Common;
  103. $info = $obj->infoFromAny($params[0]);
  104. $headings = array('Package File', 'Install Path');
  105. $installed = false;
  106. } else {
  107. $info = $reg->packageInfo($params[0]);
  108. $headings = array('Type', 'Install Path');
  109. $installed = true;
  110. }
  111. if (PEAR::isError($info)) {
  112. return $this->raiseError($info);
  113. }
  114. if ($info === null) {
  115. return $this->raiseError("`$params[0]' not installed");
  116. }
  117. $list = $info['filelist'];
  118. if ($installed) {
  119. $caption = 'Installed Files For ' . $params[0];
  120. } else {
  121. $caption = 'Contents of ' . basename($params[0]);
  122. }
  123. $data = array(
  124. 'caption' => $caption,
  125. 'border' => true,
  126. 'headline' => $headings);
  127. foreach ($list as $file => $att) {
  128. if ($installed) {
  129. if (empty($att['installed_as'])) {
  130. continue;
  131. }
  132. $data['data'][] = array($att['role'], $att['installed_as']);
  133. } else {
  134. if (isset($att['baseinstalldir'])) {
  135. $dest = $att['baseinstalldir'] . DIRECTORY_SEPARATOR .
  136. $file;
  137. } else {
  138. $dest = $file;
  139. }
  140. switch ($att['role']) {
  141. case 'test':
  142. case 'data':
  143. if ($installed) {
  144. break 2;
  145. }
  146. $dest = '-- will not be installed --';
  147. break;
  148. case 'doc':
  149. $dest = $this->config->get('doc_dir') . DIRECTORY_SEPARATOR .
  150. $dest;
  151. break;
  152. case 'php':
  153. default:
  154. $dest = $this->config->get('php_dir') . DIRECTORY_SEPARATOR .
  155. $dest;
  156. }
  157. $dest = preg_replace('!/+!', '/', $dest);
  158. $file = preg_replace('!/+!', '/', $file);
  159. $data['data'][] = array($file, $dest);
  160. }
  161. }
  162. $this->ui->outputData($data, $command);
  163. }
  164. return true;
  165. }
  166. // }}}
  167. // {{{ doShellTest()
  168. function doShellTest($command, $options, $params)
  169. {
  170. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  171. $reg = &new PEAR_Registry($this->config->get('php_dir'));
  172. // "pear shell-test Foo"
  173. if (sizeof($params) == 1) {
  174. if (!$reg->packageExists($params[0])) {
  175. exit(1);
  176. }
  177. // "pear shell-test Foo 1.0"
  178. } elseif (sizeof($params) == 2) {
  179. $v = $reg->packageInfo($params[0], 'version');
  180. if (!$v || !version_compare("$v", "{$params[1]}", "ge")) {
  181. exit(1);
  182. }
  183. // "pear shell-test Foo ge 1.0"
  184. } elseif (sizeof($params) == 3) {
  185. $v = $reg->packageInfo($params[0], 'version');
  186. if (!$v || !version_compare("$v", "{$params[2]}", $params[1])) {
  187. exit(1);
  188. }
  189. } else {
  190. $this->popErrorHandling();
  191. $this->raiseError("$command: expects 1 to 3 parameters");
  192. exit(1);
  193. }
  194. }
  195. // }}}
  196. // {{{ doInfo
  197. function doInfo($command, $options, $params)
  198. {
  199. // $params[0] The package for showing info
  200. if (sizeof($params) != 1) {
  201. return $this->raiseError("This command only accepts one param: ".
  202. "the package you want information");
  203. }
  204. if (@is_file($params[0])) {
  205. $obj = &new PEAR_Common();
  206. $info = $obj->infoFromAny($params[0]);
  207. } else {
  208. $reg = &new PEAR_Registry($this->config->get('php_dir'));
  209. $info = $reg->packageInfo($params[0]);
  210. }
  211. if (PEAR::isError($info)) {
  212. return $info;
  213. }
  214. if (empty($info)) {
  215. $this->raiseError("Nothing found for `$params[0]'");
  216. return;
  217. }
  218. unset($info['filelist']);
  219. unset($info['changelog']);
  220. $keys = array_keys($info);
  221. $longtext = array('description', 'summary');
  222. foreach ($keys as $key) {
  223. if (is_array($info[$key])) {
  224. switch ($key) {
  225. case 'maintainers': {
  226. $i = 0;
  227. $mstr = '';
  228. foreach ($info[$key] as $m) {
  229. if ($i++ > 0) {
  230. $mstr .= "\n";
  231. }
  232. $mstr .= $m['name'] . " <";
  233. if (isset($m['email'])) {
  234. $mstr .= $m['email'];
  235. } else {
  236. $mstr .= $m['handle'] . '@php.net';
  237. }
  238. $mstr .= "> ($m[role])";
  239. }
  240. $info[$key] = $mstr;
  241. break;
  242. }
  243. case 'release_deps': {
  244. $i = 0;
  245. $dstr = '';
  246. foreach ($info[$key] as $d) {
  247. if (isset($this->_deps_rel_trans[$d['rel']])) {
  248. $rel = $this->_deps_rel_trans[$d['rel']];
  249. } else {
  250. $rel = $d['rel'];
  251. }
  252. if (isset($this->_deps_type_trans[$d['type']])) {
  253. $type = ucfirst($this->_deps_type_trans[$d['type']]);
  254. } else {
  255. $type = $d['type'];
  256. }
  257. if (isset($d['name'])) {
  258. $name = $d['name'] . ' ';
  259. } else {
  260. $name = '';
  261. }
  262. if (isset($d['version'])) {
  263. $version = $d['version'] . ' ';
  264. } else {
  265. $version = '';
  266. }
  267. $dstr .= "$type $name$rel $version\n";
  268. }
  269. $info[$key] = $dstr;
  270. break;
  271. }
  272. case 'provides' : {
  273. $debug = $this->config->get('verbose');
  274. if ($debug < 2) {
  275. $pstr = 'Classes: ';
  276. } else {
  277. $pstr = '';
  278. }
  279. $i = 0;
  280. foreach ($info[$key] as $p) {
  281. if ($debug < 2 && $p['type'] != "class") {
  282. continue;
  283. }
  284. // Only print classes when verbosity mode is < 2
  285. if ($debug < 2) {
  286. if ($i++ > 0) {
  287. $pstr .= ", ";
  288. }
  289. $pstr .= $p['name'];
  290. } else {
  291. if ($i++ > 0) {
  292. $pstr .= "\n";
  293. }
  294. $pstr .= ucfirst($p['type']) . " " . $p['name'];
  295. if (isset($p['explicit']) && $p['explicit'] == 1) {
  296. $pstr .= " (explicit)";
  297. }
  298. }
  299. }
  300. $info[$key] = $pstr;
  301. break;
  302. }
  303. default: {
  304. $info[$key] = implode(", ", $info[$key]);
  305. break;
  306. }
  307. }
  308. }
  309. if ($key == '_lastmodified') {
  310. $hdate = date('Y-m-d', $info[$key]);
  311. unset($info[$key]);
  312. $info['Last Modified'] = $hdate;
  313. } else {
  314. $info[$key] = trim($info[$key]);
  315. if (in_array($key, $longtext)) {
  316. $info[$key] = preg_replace('/ +/', ' ', $info[$key]);
  317. }
  318. }
  319. }
  320. $caption = 'About ' . $info['package'] . '-' . $info['version'];
  321. $data = array(
  322. 'caption' => $caption,
  323. 'border' => true);
  324. foreach ($info as $key => $value) {
  325. $key = ucwords(trim(str_replace('_', ' ', $key)));
  326. $data['data'][] = array($key, $value);
  327. }
  328. $data['raw'] = $info;
  329. $this->ui->outputData($data, 'package-info');
  330. }
  331. // }}}
  332. }
  333. ?>