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 * @link http://pear.php.net/package/PEAR
14 * @since File available since Release 0.1
20 require_once 'PEAR/Command/Common.php';
23 * PEAR commands for managing configuration data.
27 * @author Stig Bakken <ssb@php.net>
28 * @author Greg Beaver <cellog@php.net>
29 * @copyright 1997-2009 The Authors
30 * @license http://opensource.org/licenses/bsd-license.php New BSD License
31 * @version Release: 1.10.5
32 * @link http://pear.php.net/package/PEAR
33 * @since Class available since Release 0.1
35 class PEAR_Command_Config extends PEAR_Command_Common
37 var $commands = array(
38 'config-show' => array(
39 'summary' => 'Show All Settings',
40 'function' => 'doConfigShow',
45 'doc' => 'show configuration variables for another channel',
50 Displays all configuration values. An optional argument
51 may be used to tell which configuration layer to display. Valid
52 configuration layers are "user", "system" and "default". To display
53 configurations for different channels, set the default_channel
54 configuration variable and run config-show again.
57 'config-get' => array(
58 'summary' => 'Show One Setting',
59 'function' => 'doConfigGet',
64 'doc' => 'show configuration variables for another channel',
68 'doc' => '<parameter> [layer]
69 Displays the value of one configuration parameter. The
70 first argument is the name of the parameter, an optional second argument
71 may be used to tell which configuration layer to look in. Valid configuration
72 layers are "user", "system" and "default". If no layer is specified, a value
73 will be picked from the first layer that defines the parameter, in the order
74 just specified. The configuration value will be retrieved for the channel
75 specified by the default_channel configuration variable.
78 'config-set' => array(
79 'summary' => 'Change Setting',
80 'function' => 'doConfigSet',
85 'doc' => 'show configuration variables for another channel',
89 'doc' => '<parameter> <value> [layer]
90 Sets the value of one configuration parameter. The first argument is
91 the name of the parameter, the second argument is the new value. Some
92 parameters are subject to validation, and the command will fail with
93 an error message if the new value does not make sense. An optional
94 third argument may be used to specify in which layer to set the
95 configuration parameter. The default layer is "user". The
96 configuration value will be set for the current channel, which
97 is controlled by the default_channel configuration variable.
100 'config-help' => array(
101 'summary' => 'Show Information About Setting',
102 'function' => 'doConfigHelp',
104 'options' => array(),
105 'doc' => '[parameter]
106 Displays help for a configuration parameter. Without arguments it
107 displays help for all configuration parameters.
110 'config-create' => array(
111 'summary' => 'Create a Default configuration file',
112 'function' => 'doConfigCreate',
117 'doc' => 'create a config file for a windows install',
120 'doc' => '<root path> <filename>
121 Create a default configuration file with all directory configuration
122 variables set to subdirectories of <root path>, and save it as <filename>.
123 This is useful especially for creating a configuration file for a remote
124 PEAR installation (using the --remoteconfig option of install, upgrade,
131 * PEAR_Command_Config constructor.
135 function __construct(&$ui, &$config)
137 parent::__construct($ui, $config);
140 function doConfigShow($command, $options, $params)
143 if (is_array($params)) {
144 $layer = isset($params[0]) ? $params[0] : null;
147 // $params[0] -> the layer
148 if ($error = $this->_checkLayer($layer)) {
149 return $this->raiseError("config-show:$error");
152 $keys = $this->config->getKeys();
154 $channel = isset($options['channel']) ? $options['channel'] :
155 $this->config->get('default_channel');
156 $reg = &$this->config->getRegistry();
157 if (!$reg->channelExists($channel)) {
158 return $this->raiseError('Channel "' . $channel . '" does not exist');
161 $channel = $reg->channelName($channel);
162 $data = array('caption' => 'Configuration (channel ' . $channel . '):');
163 foreach ($keys as $key) {
164 $type = $this->config->getType($key);
165 $value = $this->config->get($key, $layer, $channel);
166 if ($type == 'password' && $value) {
170 if ($value === false) {
172 } elseif ($value === true) {
176 $data['data'][$this->config->getGroup($key)][] = array($this->config->getPrompt($key) , $key, $value);
179 foreach ($this->config->getLayers() as $layer) {
180 $data['data']['Config Files'][] = array(ucfirst($layer) . ' Configuration File', 'Filename' , $this->config->getConfFile($layer));
183 $this->ui->outputData($data, $command);
187 function doConfigGet($command, $options, $params)
189 $args_cnt = is_array($params) ? count($params) : 0;
192 $config_key = $params[0];
196 $config_key = $params[0];
198 if ($error = $this->_checkLayer($layer)) {
199 return $this->raiseError("config-get:$error");
204 return $this->raiseError("config-get expects 1 or 2 parameters");
207 $reg = &$this->config->getRegistry();
208 $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel');
209 if (!$reg->channelExists($channel)) {
210 return $this->raiseError('Channel "' . $channel . '" does not exist');
213 $channel = $reg->channelName($channel);
214 $this->ui->outputData($this->config->get($config_key, $layer, $channel), $command);
218 function doConfigSet($command, $options, $params)
220 // $param[0] -> a parameter to set
221 // $param[1] -> the value for the parameter
222 // $param[2] -> the layer
224 if (count($params) < 2 || count($params) > 3) {
225 $failmsg .= "config-set expects 2 or 3 parameters";
226 return PEAR::raiseError($failmsg);
229 if (isset($params[2]) && ($error = $this->_checkLayer($params[2]))) {
231 return PEAR::raiseError("config-set:$failmsg");
234 $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel');
235 $reg = &$this->config->getRegistry();
236 if (!$reg->channelExists($channel)) {
237 return $this->raiseError('Channel "' . $channel . '" does not exist');
240 $channel = $reg->channelName($channel);
241 if ($params[0] == 'default_channel' && !$reg->channelExists($params[1])) {
242 return $this->raiseError('Channel "' . $params[1] . '" does not exist');
245 if ($params[0] == 'preferred_mirror'
247 !$reg->mirrorExists($channel, $params[1]) &&
248 (!$reg->channelExists($params[1]) || $channel != $params[1])
251 $msg = 'Channel Mirror "' . $params[1] . '" does not exist';
252 $msg .= ' in your registry for channel "' . $channel . '".';
253 $msg .= "\n" . 'Attempt to run "pear channel-update ' . $channel .'"';
254 $msg .= ' if you believe this mirror should exist as you may';
255 $msg .= ' have outdated channel information.';
256 return $this->raiseError($msg);
259 if (count($params) == 2) {
260 array_push($params, 'user');
266 array_push($params, $channel);
267 if (!call_user_func_array(array(&$this->config, 'set'), $params)) {
269 $failmsg = "config-set (" . implode(", ", $params) . ") failed, channel $channel";
271 $this->config->store($layer);
275 return $this->raiseError($failmsg);
278 $this->ui->outputData('config-set succeeded', $command);
282 function doConfigHelp($command, $options, $params)
284 if (empty($params)) {
285 $params = $this->config->getKeys();
288 $data['caption'] = "Config help" . ((count($params) == 1) ? " for $params[0]" : '');
289 $data['headline'] = array('Name', 'Type', 'Description');
290 $data['border'] = true;
291 foreach ($params as $name) {
292 $type = $this->config->getType($name);
293 $docs = $this->config->getDocs($name);
294 if ($type == 'set') {
295 $docs = rtrim($docs) . "\nValid set: " .
296 implode(' ', $this->config->getSetValues($name));
299 $data['data'][] = array($name, $type, $docs);
302 $this->ui->outputData($data, $command);
305 function doConfigCreate($command, $options, $params)
307 if (count($params) != 2) {
308 return PEAR::raiseError('config-create: must have 2 parameters, root path and ' .
309 'filename to save as');
313 // Clean up the DIRECTORY_SEPARATOR mess
314 $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR;
315 $root = preg_replace(array('!\\\\+!', '!/+!', "!$ds2+!"),
316 array('/', '/', '/'),
318 if ($root{0} != '/') {
319 if (!isset($options['windows'])) {
320 return PEAR::raiseError('Root directory must be an absolute path beginning ' .
321 'with "/", was: "' . $root . '"');
324 if (!preg_match('/^[A-Za-z]:/', $root)) {
325 return PEAR::raiseError('Root directory must be an absolute path beginning ' .
326 'with "\\" or "C:\\", was: "' . $root . '"');
330 $windows = isset($options['windows']);
332 $root = str_replace('/', '\\', $root);
335 if (!file_exists($params[1]) && !@touch($params[1])) {
336 return PEAR::raiseError('Could not create "' . $params[1] . '"');
339 $params[1] = realpath($params[1]);
340 $config = new PEAR_Config($params[1], '#no#system#config#', false, false);
341 if ($root{strlen($root) - 1} == '/') {
342 $root = substr($root, 0, strlen($root) - 1);
345 $config->noRegistry();
346 $config->set('php_dir', $windows ? "$root\\pear\\php" : "$root/pear/php", 'user');
347 $config->set('data_dir', $windows ? "$root\\pear\\data" : "$root/pear/data");
348 $config->set('www_dir', $windows ? "$root\\pear\\www" : "$root/pear/www");
349 $config->set('cfg_dir', $windows ? "$root\\pear\\cfg" : "$root/pear/cfg");
350 $config->set('ext_dir', $windows ? "$root\\pear\\ext" : "$root/pear/ext");
351 $config->set('doc_dir', $windows ? "$root\\pear\\docs" : "$root/pear/docs");
352 $config->set('test_dir', $windows ? "$root\\pear\\tests" : "$root/pear/tests");
353 $config->set('cache_dir', $windows ? "$root\\pear\\cache" : "$root/pear/cache");
354 $config->set('download_dir', $windows ? "$root\\pear\\download" : "$root/pear/download");
355 $config->set('temp_dir', $windows ? "$root\\pear\\temp" : "$root/pear/temp");
356 $config->set('bin_dir', $windows ? "$root\\pear" : "$root/pear");
357 $config->set('man_dir', $windows ? "$root\\pear\\man" : "$root/pear/man");
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";