3 * PEAR_Command_Config (config-show, config-get, config-set, config-help, config-create commands)
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: Config.php 313024 2011-07-06 19:51:24Z dufuz $
14 * @link http://pear.php.net/package/PEAR
15 * @since File available since Release 0.1
21 require_once 'PEAR/Command/Common.php';
24 * PEAR commands for managing configuration data.
28 * @author Stig Bakken <ssb@php.net>
29 * @author Greg Beaver <cellog@php.net>
30 * @copyright 1997-2009 The Authors
31 * @license http://opensource.org/licenses/bsd-license.php New BSD License
32 * @version Release: 1.9.4
33 * @link http://pear.php.net/package/PEAR
34 * @since Class available since Release 0.1
36 class PEAR_Command_Config extends PEAR_Command_Common
38 var $commands = array(
39 'config-show' => array(
40 'summary' => 'Show All Settings',
41 'function' => 'doConfigShow',
46 'doc' => 'show configuration variables for another channel',
51 Displays all configuration values. An optional argument
52 may be used to tell which configuration layer to display. Valid
53 configuration layers are "user", "system" and "default". To display
54 configurations for different channels, set the default_channel
55 configuration variable and run config-show again.
58 'config-get' => array(
59 'summary' => 'Show One Setting',
60 'function' => 'doConfigGet',
65 'doc' => 'show configuration variables for another channel',
69 'doc' => '<parameter> [layer]
70 Displays the value of one configuration parameter. The
71 first argument is the name of the parameter, an optional second argument
72 may be used to tell which configuration layer to look in. Valid configuration
73 layers are "user", "system" and "default". If no layer is specified, a value
74 will be picked from the first layer that defines the parameter, in the order
75 just specified. The configuration value will be retrieved for the channel
76 specified by the default_channel configuration variable.
79 'config-set' => array(
80 'summary' => 'Change Setting',
81 'function' => 'doConfigSet',
86 'doc' => 'show configuration variables for another channel',
90 'doc' => '<parameter> <value> [layer]
91 Sets the value of one configuration parameter. The first argument is
92 the name of the parameter, the second argument is the new value. Some
93 parameters are subject to validation, and the command will fail with
94 an error message if the new value does not make sense. An optional
95 third argument may be used to specify in which layer to set the
96 configuration parameter. The default layer is "user". The
97 configuration value will be set for the current channel, which
98 is controlled by the default_channel configuration variable.
101 'config-help' => array(
102 'summary' => 'Show Information About Setting',
103 'function' => 'doConfigHelp',
105 'options' => array(),
106 'doc' => '[parameter]
107 Displays help for a configuration parameter. Without arguments it
108 displays help for all configuration parameters.
111 'config-create' => array(
112 'summary' => 'Create a Default configuration file',
113 'function' => 'doConfigCreate',
118 'doc' => 'create a config file for a windows install',
121 'doc' => '<root path> <filename>
122 Create a default configuration file with all directory configuration
123 variables set to subdirectories of <root path>, and save it as <filename>.
124 This is useful especially for creating a configuration file for a remote
125 PEAR installation (using the --remoteconfig option of install, upgrade,
132 * PEAR_Command_Config constructor.
136 function PEAR_Command_Config(&$ui, &$config)
138 parent::PEAR_Command_Common($ui, $config);
141 function doConfigShow($command, $options, $params)
144 if (is_array($params)) {
145 $layer = isset($params[0]) ? $params[0] : null;
148 // $params[0] -> the layer
149 if ($error = $this->_checkLayer($layer)) {
150 return $this->raiseError("config-show:$error");
153 $keys = $this->config->getKeys();
155 $channel = isset($options['channel']) ? $options['channel'] :
156 $this->config->get('default_channel');
157 $reg = &$this->config->getRegistry();
158 if (!$reg->channelExists($channel)) {
159 return $this->raiseError('Channel "' . $channel . '" does not exist');
162 $channel = $reg->channelName($channel);
163 $data = array('caption' => 'Configuration (channel ' . $channel . '):');
164 foreach ($keys as $key) {
165 $type = $this->config->getType($key);
166 $value = $this->config->get($key, $layer, $channel);
167 if ($type == 'password' && $value) {
171 if ($value === false) {
173 } elseif ($value === true) {
177 $data['data'][$this->config->getGroup($key)][] = array($this->config->getPrompt($key) , $key, $value);
180 foreach ($this->config->getLayers() as $layer) {
181 $data['data']['Config Files'][] = array(ucfirst($layer) . ' Configuration File', 'Filename' , $this->config->getConfFile($layer));
184 $this->ui->outputData($data, $command);
188 function doConfigGet($command, $options, $params)
190 $args_cnt = is_array($params) ? count($params) : 0;
193 $config_key = $params[0];
197 $config_key = $params[0];
199 if ($error = $this->_checkLayer($layer)) {
200 return $this->raiseError("config-get:$error");
205 return $this->raiseError("config-get expects 1 or 2 parameters");
208 $reg = &$this->config->getRegistry();
209 $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel');
210 if (!$reg->channelExists($channel)) {
211 return $this->raiseError('Channel "' . $channel . '" does not exist');
214 $channel = $reg->channelName($channel);
215 $this->ui->outputData($this->config->get($config_key, $layer, $channel), $command);
219 function doConfigSet($command, $options, $params)
221 // $param[0] -> a parameter to set
222 // $param[1] -> the value for the parameter
223 // $param[2] -> the layer
225 if (count($params) < 2 || count($params) > 3) {
226 $failmsg .= "config-set expects 2 or 3 parameters";
227 return PEAR::raiseError($failmsg);
230 if (isset($params[2]) && ($error = $this->_checkLayer($params[2]))) {
232 return PEAR::raiseError("config-set:$failmsg");
235 $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel');
236 $reg = &$this->config->getRegistry();
237 if (!$reg->channelExists($channel)) {
238 return $this->raiseError('Channel "' . $channel . '" does not exist');
241 $channel = $reg->channelName($channel);
242 if ($params[0] == 'default_channel' && !$reg->channelExists($params[1])) {
243 return $this->raiseError('Channel "' . $params[1] . '" does not exist');
246 if ($params[0] == 'preferred_mirror'
248 !$reg->mirrorExists($channel, $params[1]) &&
249 (!$reg->channelExists($params[1]) || $channel != $params[1])
252 $msg = 'Channel Mirror "' . $params[1] . '" does not exist';
253 $msg .= ' in your registry for channel "' . $channel . '".';
254 $msg .= "\n" . 'Attempt to run "pear channel-update ' . $channel .'"';
255 $msg .= ' if you believe this mirror should exist as you may';
256 $msg .= ' have outdated channel information.';
257 return $this->raiseError($msg);
260 if (count($params) == 2) {
261 array_push($params, 'user');
267 array_push($params, $channel);
268 if (!call_user_func_array(array(&$this->config, 'set'), $params)) {
270 $failmsg = "config-set (" . implode(", ", $params) . ") failed, channel $channel";
272 $this->config->store($layer);
276 return $this->raiseError($failmsg);
279 $this->ui->outputData('config-set succeeded', $command);
283 function doConfigHelp($command, $options, $params)
285 if (empty($params)) {
286 $params = $this->config->getKeys();
289 $data['caption'] = "Config help" . ((count($params) == 1) ? " for $params[0]" : '');
290 $data['headline'] = array('Name', 'Type', 'Description');
291 $data['border'] = true;
292 foreach ($params as $name) {
293 $type = $this->config->getType($name);
294 $docs = $this->config->getDocs($name);
295 if ($type == 'set') {
296 $docs = rtrim($docs) . "\nValid set: " .
297 implode(' ', $this->config->getSetValues($name));
300 $data['data'][] = array($name, $type, $docs);
303 $this->ui->outputData($data, $command);
306 function doConfigCreate($command, $options, $params)
308 if (count($params) != 2) {
309 return PEAR::raiseError('config-create: must have 2 parameters, root path and ' .
310 'filename to save as');
314 // Clean up the DIRECTORY_SEPARATOR mess
315 $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR;
316 $root = preg_replace(array('!\\\\+!', '!/+!', "!$ds2+!"),
317 array('/', '/', '/'),
319 if ($root{0} != '/') {
320 if (!isset($options['windows'])) {
321 return PEAR::raiseError('Root directory must be an absolute path beginning ' .
322 'with "/", was: "' . $root . '"');
325 if (!preg_match('/^[A-Za-z]:/', $root)) {
326 return PEAR::raiseError('Root directory must be an absolute path beginning ' .
327 'with "\\" or "C:\\", was: "' . $root . '"');
331 $windows = isset($options['windows']);
333 $root = str_replace('/', '\\', $root);
336 if (!file_exists($params[1]) && !@touch($params[1])) {
337 return PEAR::raiseError('Could not create "' . $params[1] . '"');
340 $params[1] = realpath($params[1]);
341 $config = &new PEAR_Config($params[1], '#no#system#config#', false, false);
342 if ($root{strlen($root) - 1} == '/') {
343 $root = substr($root, 0, strlen($root) - 1);
346 $config->noRegistry();
347 $config->set('php_dir', $windows ? "$root\\pear\\php" : "$root/pear/php", 'user');
348 $config->set('data_dir', $windows ? "$root\\pear\\data" : "$root/pear/data");
349 $config->set('www_dir', $windows ? "$root\\pear\\www" : "$root/pear/www");
350 $config->set('cfg_dir', $windows ? "$root\\pear\\cfg" : "$root/pear/cfg");
351 $config->set('ext_dir', $windows ? "$root\\pear\\ext" : "$root/pear/ext");
352 $config->set('doc_dir', $windows ? "$root\\pear\\docs" : "$root/pear/docs");
353 $config->set('test_dir', $windows ? "$root\\pear\\tests" : "$root/pear/tests");
354 $config->set('cache_dir', $windows ? "$root\\pear\\cache" : "$root/pear/cache");
355 $config->set('download_dir', $windows ? "$root\\pear\\download" : "$root/pear/download");
356 $config->set('temp_dir', $windows ? "$root\\pear\\temp" : "$root/pear/temp");
357 $config->set('bin_dir', $windows ? "$root\\pear" : "$root/pear");
358 $config->writeConfigFile();
359 $this->_showConfig($config);
360 $this->ui->outputData('Successfully created default configuration file "' . $params[1] . '"',
364 function _showConfig(&$config)
366 $params = array('user');
367 $keys = $config->getKeys();
369 $channel = 'pear.php.net';
370 $data = array('caption' => 'Configuration (channel ' . $channel . '):');
371 foreach ($keys as $key) {
372 $type = $config->getType($key);
373 $value = $config->get($key, 'user', $channel);
374 if ($type == 'password' && $value) {
378 if ($value === false) {
380 } elseif ($value === true) {
383 $data['data'][$config->getGroup($key)][] =
384 array($config->getPrompt($key) , $key, $value);
387 foreach ($config->getLayers() as $layer) {
388 $data['data']['Config Files'][] =
389 array(ucfirst($layer) . ' Configuration File', 'Filename' ,
390 $config->getConfFile($layer));
393 $this->ui->outputData($data, 'config-show');
398 * Checks if a layer is defined or not
400 * @param string $layer The layer to search for
401 * @return mixed False on no error or the error message
403 function _checkLayer($layer = null)
405 if (!empty($layer) && $layer != 'default') {
406 $layers = $this->config->getLayers();
407 if (!in_array($layer, $layers)) {
408 return " only the layers: \"" . implode('" or "', $layers) . "\" are supported";