Config.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 Bakken <[email protected]> |
  17. // | Tomas V.V.Cox <[email protected]> |
  18. // | |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Config.php,v 1.27 2004/06/15 16:48:49 pajoye Exp $
  22. require_once "PEAR/Command/Common.php";
  23. require_once "PEAR/Config.php";
  24. /**
  25. * PEAR commands for managing configuration data.
  26. *
  27. */
  28. class PEAR_Command_Config extends PEAR_Command_Common
  29. {
  30. // {{{ properties
  31. var $commands = array(
  32. 'config-show' => array(
  33. 'summary' => 'Show All Settings',
  34. 'function' => 'doConfigShow',
  35. 'shortcut' => 'csh',
  36. 'options' => array(),
  37. 'doc' => '
  38. Displays all configuration values. An optional argument
  39. may be used to tell which configuration layer to display. Valid
  40. configuration layers are "user", "system" and "default".
  41. ',
  42. ),
  43. 'config-get' => array(
  44. 'summary' => 'Show One Setting',
  45. 'function' => 'doConfigGet',
  46. 'shortcut' => 'cg',
  47. 'options' => array(),
  48. 'doc' => '<parameter> [layer]
  49. Displays the value of one configuration parameter. The
  50. first argument is the name of the parameter, an optional second argument
  51. may be used to tell which configuration layer to look in. Valid configuration
  52. layers are "user", "system" and "default". If no layer is specified, a value
  53. will be picked from the first layer that defines the parameter, in the order
  54. just specified.
  55. ',
  56. ),
  57. 'config-set' => array(
  58. 'summary' => 'Change Setting',
  59. 'function' => 'doConfigSet',
  60. 'shortcut' => 'cs',
  61. 'options' => array(),
  62. 'doc' => '<parameter> <value> [layer]
  63. Sets the value of one configuration parameter. The first argument is
  64. the name of the parameter, the second argument is the new value. Some
  65. parameters are subject to validation, and the command will fail with
  66. an error message if the new value does not make sense. An optional
  67. third argument may be used to specify in which layer to set the
  68. configuration parameter. The default layer is "user".
  69. ',
  70. ),
  71. 'config-help' => array(
  72. 'summary' => 'Show Information About Setting',
  73. 'function' => 'doConfigHelp',
  74. 'shortcut' => 'ch',
  75. 'options' => array(),
  76. 'doc' => '[parameter]
  77. Displays help for a configuration parameter. Without arguments it
  78. displays help for all configuration parameters.
  79. ',
  80. ),
  81. );
  82. // }}}
  83. // {{{ constructor
  84. /**
  85. * PEAR_Command_Config constructor.
  86. *
  87. * @access public
  88. */
  89. function PEAR_Command_Config(&$ui, &$config)
  90. {
  91. parent::PEAR_Command_Common($ui, $config);
  92. }
  93. // }}}
  94. // {{{ doConfigShow()
  95. function doConfigShow($command, $options, $params)
  96. {
  97. // $params[0] -> the layer
  98. if ($error = $this->_checkLayer(@$params[0])) {
  99. return $this->raiseError($error);
  100. }
  101. $keys = $this->config->getKeys();
  102. sort($keys);
  103. $data = array('caption' => 'Configuration:');
  104. foreach ($keys as $key) {
  105. $type = $this->config->getType($key);
  106. $value = $this->config->get($key, @$params[0]);
  107. if ($type == 'password' && $value) {
  108. $value = '********';
  109. }
  110. if ($value === false) {
  111. $value = 'false';
  112. } elseif ($value === true) {
  113. $value = 'true';
  114. }
  115. $data['data'][$this->config->getGroup($key)][] = array($this->config->getPrompt($key) , $key, $value);
  116. }
  117. $this->ui->outputData($data, $command);
  118. return true;
  119. }
  120. // }}}
  121. // {{{ doConfigGet()
  122. function doConfigGet($command, $options, $params)
  123. {
  124. // $params[0] -> the parameter
  125. // $params[1] -> the layer
  126. if ($error = $this->_checkLayer(@$params[1])) {
  127. return $this->raiseError($error);
  128. }
  129. if (sizeof($params) < 1 || sizeof($params) > 2) {
  130. return $this->raiseError("config-get expects 1 or 2 parameters");
  131. } elseif (sizeof($params) == 1) {
  132. $this->ui->outputData($this->config->get($params[0]), $command);
  133. } else {
  134. $data = $this->config->get($params[0], $params[1]);
  135. $this->ui->outputData($data, $command);
  136. }
  137. return true;
  138. }
  139. // }}}
  140. // {{{ doConfigSet()
  141. function doConfigSet($command, $options, $params)
  142. {
  143. // $param[0] -> a parameter to set
  144. // $param[1] -> the value for the parameter
  145. // $param[2] -> the layer
  146. $failmsg = '';
  147. if (sizeof($params) < 2 || sizeof($params) > 3) {
  148. $failmsg .= "config-set expects 2 or 3 parameters";
  149. return PEAR::raiseError($failmsg);
  150. }
  151. if ($error = $this->_checkLayer(@$params[2])) {
  152. $failmsg .= $error;
  153. return PEAR::raiseError($failmsg);
  154. }
  155. if (!call_user_func_array(array(&$this->config, 'set'), $params))
  156. {
  157. $failmsg = "config-set (" . implode(", ", $params) . ") failed";
  158. } else {
  159. $this->config->store();
  160. }
  161. if ($failmsg) {
  162. return $this->raiseError($failmsg);
  163. }
  164. return true;
  165. }
  166. // }}}
  167. // {{{ doConfigHelp()
  168. function doConfigHelp($command, $options, $params)
  169. {
  170. if (empty($params)) {
  171. $params = $this->config->getKeys();
  172. }
  173. $data['caption'] = "Config help" . ((count($params) == 1) ? " for $params[0]" : '');
  174. $data['headline'] = array('Name', 'Type', 'Description');
  175. $data['border'] = true;
  176. foreach ($params as $name) {
  177. $type = $this->config->getType($name);
  178. $docs = $this->config->getDocs($name);
  179. if ($type == 'set') {
  180. $docs = rtrim($docs) . "\nValid set: " .
  181. implode(' ', $this->config->getSetValues($name));
  182. }
  183. $data['data'][] = array($name, $type, $docs);
  184. }
  185. $this->ui->outputData($data, $command);
  186. }
  187. // }}}
  188. // {{{ _checkLayer()
  189. /**
  190. * Checks if a layer is defined or not
  191. *
  192. * @param string $layer The layer to search for
  193. * @return mixed False on no error or the error message
  194. */
  195. function _checkLayer($layer = null)
  196. {
  197. if (!empty($layer) && $layer != 'default') {
  198. $layers = $this->config->getLayers();
  199. if (!in_array($layer, $layers)) {
  200. return " only the layers: \"" . implode('" or "', $layers) . "\" are supported";
  201. }
  202. }
  203. return false;
  204. }
  205. // }}}
  206. }
  207. ?>