Редакция 69 | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
69 | alex-w | 1 | <?php |
2 | /** |
||
3 | * PEAR_Command_Config (config-show, config-get, config-set, config-help, config-create commands) |
||
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: Config.php,v 1.56 2008/01/03 20:26:36 cellog Exp $ |
||
20 | * @link http://pear.php.net/package/PEAR |
||
21 | * @since File available since Release 0.1 |
||
22 | */ |
||
23 | |||
24 | /** |
||
25 | * base class |
||
26 | */ |
||
27 | require_once 'PEAR/Command/Common.php'; |
||
28 | |||
29 | /** |
||
30 | * PEAR commands for managing configuration data. |
||
31 | * |
||
32 | * @category pear |
||
33 | * @package PEAR |
||
34 | * @author Stig Bakken <ssb@php.net> |
||
35 | * @author Greg Beaver <cellog@php.net> |
||
36 | * @copyright 1997-2008 The PHP Group |
||
37 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
||
38 | * @version Release: 1.7.2 |
||
39 | * @link http://pear.php.net/package/PEAR |
||
40 | * @since Class available since Release 0.1 |
||
41 | */ |
||
42 | class PEAR_Command_Config extends PEAR_Command_Common |
||
43 | { |
||
44 | // {{{ properties |
||
45 | |||
46 | var $commands = array( |
||
47 | 'config-show' => array( |
||
48 | 'summary' => 'Show All Settings', |
||
49 | 'function' => 'doConfigShow', |
||
50 | 'shortcut' => 'csh', |
||
51 | 'options' => array( |
||
52 | 'channel' => array( |
||
53 | 'shortopt' => 'c', |
||
54 | 'doc' => 'show configuration variables for another channel', |
||
55 | 'arg' => 'CHAN', |
||
56 | ), |
||
57 | ), |
||
58 | 'doc' => '[layer] |
||
59 | Displays all configuration values. An optional argument |
||
60 | may be used to tell which configuration layer to display. Valid |
||
61 | configuration layers are "user", "system" and "default". To display |
||
62 | configurations for different channels, set the default_channel |
||
63 | configuration variable and run config-show again. |
||
64 | ', |
||
65 | ), |
||
66 | 'config-get' => array( |
||
67 | 'summary' => 'Show One Setting', |
||
68 | 'function' => 'doConfigGet', |
||
69 | 'shortcut' => 'cg', |
||
70 | 'options' => array( |
||
71 | 'channel' => array( |
||
72 | 'shortopt' => 'c', |
||
73 | 'doc' => 'show configuration variables for another channel', |
||
74 | 'arg' => 'CHAN', |
||
75 | ), |
||
76 | ), |
||
77 | 'doc' => '<parameter> [layer] |
||
78 | Displays the value of one configuration parameter. The |
||
79 | first argument is the name of the parameter, an optional second argument |
||
80 | may be used to tell which configuration layer to look in. Valid configuration |
||
81 | layers are "user", "system" and "default". If no layer is specified, a value |
||
82 | will be picked from the first layer that defines the parameter, in the order |
||
83 | just specified. The configuration value will be retrieved for the channel |
||
84 | specified by the default_channel configuration variable. |
||
85 | ', |
||
86 | ), |
||
87 | 'config-set' => array( |
||
88 | 'summary' => 'Change Setting', |
||
89 | 'function' => 'doConfigSet', |
||
90 | 'shortcut' => 'cs', |
||
91 | 'options' => array( |
||
92 | 'channel' => array( |
||
93 | 'shortopt' => 'c', |
||
94 | 'doc' => 'show configuration variables for another channel', |
||
95 | 'arg' => 'CHAN', |
||
96 | ), |
||
97 | ), |
||
98 | 'doc' => '<parameter> <value> [layer] |
||
99 | Sets the value of one configuration parameter. The first argument is |
||
100 | the name of the parameter, the second argument is the new value. Some |
||
101 | parameters are subject to validation, and the command will fail with |
||
102 | an error message if the new value does not make sense. An optional |
||
103 | third argument may be used to specify in which layer to set the |
||
104 | configuration parameter. The default layer is "user". The |
||
105 | configuration value will be set for the current channel, which |
||
106 | is controlled by the default_channel configuration variable. |
||
107 | ', |
||
108 | ), |
||
109 | 'config-help' => array( |
||
110 | 'summary' => 'Show Information About Setting', |
||
111 | 'function' => 'doConfigHelp', |
||
112 | 'shortcut' => 'ch', |
||
113 | 'options' => array(), |
||
114 | 'doc' => '[parameter] |
||
115 | Displays help for a configuration parameter. Without arguments it |
||
116 | displays help for all configuration parameters. |
||
117 | ', |
||
118 | ), |
||
119 | 'config-create' => array( |
||
120 | 'summary' => 'Create a Default configuration file', |
||
121 | 'function' => 'doConfigCreate', |
||
122 | 'shortcut' => 'coc', |
||
123 | 'options' => array( |
||
124 | 'windows' => array( |
||
125 | 'shortopt' => 'w', |
||
126 | 'doc' => 'create a config file for a windows install', |
||
127 | ), |
||
128 | ), |
||
129 | 'doc' => '<root path> <filename> |
||
130 | Create a default configuration file with all directory configuration |
||
131 | variables set to subdirectories of <root path>, and save it as <filename>. |
||
132 | This is useful especially for creating a configuration file for a remote |
||
133 | PEAR installation (using the --remoteconfig option of install, upgrade, |
||
134 | and uninstall). |
||
135 | ', |
||
136 | ), |
||
137 | ); |
||
138 | |||
139 | // }}} |
||
140 | // {{{ constructor |
||
141 | |||
142 | /** |
||
143 | * PEAR_Command_Config constructor. |
||
144 | * |
||
145 | * @access public |
||
146 | */ |
||
147 | function PEAR_Command_Config(&$ui, &$config) |
||
148 | { |
||
149 | parent::PEAR_Command_Common($ui, $config); |
||
150 | } |
||
151 | |||
152 | // }}} |
||
153 | |||
154 | // {{{ doConfigShow() |
||
155 | |||
156 | function doConfigShow($command, $options, $params) |
||
157 | { |
||
158 | if (is_array($params)) { |
||
159 | $layer = isset($params[0]) ? $params[0] : NULL; |
||
160 | } else { |
||
161 | $layer = NULL; |
||
162 | } |
||
163 | |||
164 | // $params[0] -> the layer |
||
165 | if ($error = $this->_checkLayer($layer)) { |
||
166 | return $this->raiseError("config-show:$error"); |
||
167 | } |
||
168 | $keys = $this->config->getKeys(); |
||
169 | sort($keys); |
||
170 | $channel = isset($options['channel']) ? $options['channel'] : |
||
171 | $this->config->get('default_channel'); |
||
172 | $reg = &$this->config->getRegistry(); |
||
173 | if (!$reg->channelExists($channel)) { |
||
174 | return $this->raiseError('Channel "' . $channel . '" does not exist'); |
||
175 | } |
||
176 | $data = array('caption' => 'Configuration (channel ' . $channel . '):'); |
||
177 | foreach ($keys as $key) { |
||
178 | $type = $this->config->getType($key); |
||
179 | $value = $this->config->get($key, $layer, $channel); |
||
180 | if ($type == 'password' && $value) { |
||
181 | $value = '********'; |
||
182 | } |
||
183 | if ($value === false) { |
||
184 | $value = 'false'; |
||
185 | } elseif ($value === true) { |
||
186 | $value = 'true'; |
||
187 | } |
||
188 | $data['data'][$this->config->getGroup($key)][] = array($this->config->getPrompt($key) , $key, $value); |
||
189 | } |
||
190 | foreach ($this->config->getLayers() as $layer) { |
||
191 | $data['data']['Config Files'][] = array(ucfirst($layer) . ' Configuration File', 'Filename' , $this->config->getConfFile($layer)); |
||
192 | } |
||
193 | |||
194 | $this->ui->outputData($data, $command); |
||
195 | return true; |
||
196 | } |
||
197 | |||
198 | // }}} |
||
199 | // {{{ doConfigGet() |
||
200 | |||
201 | function doConfigGet($command, $options, $params) |
||
202 | { |
||
203 | if (!is_array($params)) { |
||
204 | $args_cnt = 0; |
||
205 | } else { |
||
206 | $args_cnt = count($params); |
||
207 | } |
||
208 | |||
209 | switch ($args_cnt) { |
||
210 | case 1: |
||
211 | $config_key = $params[0]; |
||
212 | $layer = NULL; |
||
213 | break; |
||
214 | case 2: |
||
215 | $config_key = $params[0]; |
||
216 | $layer = $params[1]; |
||
217 | if ($error = $this->_checkLayer($layer)) { |
||
218 | return $this->raiseError("config-get:$error"); |
||
219 | } |
||
220 | break; |
||
221 | case 0: |
||
222 | default: |
||
223 | return $this->raiseError("config-get expects 1 or 2 parameters"); |
||
224 | } |
||
225 | |||
226 | $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel'); |
||
227 | $reg = &$this->config->getRegistry(); |
||
228 | |||
229 | if (!$reg->channelExists($channel)) { |
||
230 | return $this->raiseError('Channel "' . $channel . '" does not exist'); |
||
231 | } |
||
232 | |||
233 | $this->ui->outputData($this->config->get($config_key, $layer, $channel), $command); |
||
234 | |||
235 | return true; |
||
236 | } |
||
237 | |||
238 | // }}} |
||
239 | // {{{ doConfigSet() |
||
240 | |||
241 | function doConfigSet($command, $options, $params) |
||
242 | { |
||
243 | // $param[0] -> a parameter to set |
||
244 | // $param[1] -> the value for the parameter |
||
245 | // $param[2] -> the layer |
||
246 | $failmsg = ''; |
||
247 | if (sizeof($params) < 2 || sizeof($params) > 3) { |
||
248 | $failmsg .= "config-set expects 2 or 3 parameters"; |
||
249 | return PEAR::raiseError($failmsg); |
||
250 | } |
||
251 | if (isset($params[2]) && ($error = $this->_checkLayer($params[2]))) { |
||
252 | $failmsg .= $error; |
||
253 | return PEAR::raiseError("config-set:$failmsg"); |
||
254 | } |
||
255 | $channel = isset($options['channel']) ? $options['channel'] : |
||
256 | $this->config->get('default_channel'); |
||
257 | $reg = &$this->config->getRegistry(); |
||
258 | if (!$reg->channelExists($channel)) { |
||
259 | return $this->raiseError('Channel "' . $channel . '" does not exist'); |
||
260 | } |
||
261 | if ($params[0] == 'default_channel') { |
||
262 | if (!$reg->channelExists($params[1])) { |
||
263 | return $this->raiseError('Channel "' . $params[1] . '" does not exist'); |
||
264 | } |
||
265 | } |
||
266 | if (count($params) == 2) { |
||
267 | array_push($params, 'user'); |
||
268 | $layer = 'user'; |
||
269 | } else { |
||
270 | $layer = $params[2]; |
||
271 | } |
||
272 | array_push($params, $channel); |
||
273 | if (!call_user_func_array(array(&$this->config, 'set'), $params)) |
||
274 | { |
||
275 | array_pop($params); |
||
276 | $failmsg = "config-set (" . implode(", ", $params) . ") failed, channel $channel"; |
||
277 | } else { |
||
278 | $this->config->store($layer); |
||
279 | } |
||
280 | if ($failmsg) { |
||
281 | return $this->raiseError($failmsg); |
||
282 | } |
||
283 | $this->ui->outputData('config-set succeeded', $command); |
||
284 | return true; |
||
285 | } |
||
286 | |||
287 | // }}} |
||
288 | // {{{ doConfigHelp() |
||
289 | |||
290 | function doConfigHelp($command, $options, $params) |
||
291 | { |
||
292 | if (empty($params)) { |
||
293 | $params = $this->config->getKeys(); |
||
294 | } |
||
295 | $data['caption'] = "Config help" . ((count($params) == 1) ? " for $params[0]" : ''); |
||
296 | $data['headline'] = array('Name', 'Type', 'Description'); |
||
297 | $data['border'] = true; |
||
298 | foreach ($params as $name) { |
||
299 | $type = $this->config->getType($name); |
||
300 | $docs = $this->config->getDocs($name); |
||
301 | if ($type == 'set') { |
||
302 | $docs = rtrim($docs) . "\nValid set: " . |
||
303 | implode(' ', $this->config->getSetValues($name)); |
||
304 | } |
||
305 | $data['data'][] = array($name, $type, $docs); |
||
306 | } |
||
307 | $this->ui->outputData($data, $command); |
||
308 | } |
||
309 | |||
310 | // }}} |
||
311 | // {{{ doConfigCreate() |
||
312 | |||
313 | function doConfigCreate($command, $options, $params) |
||
314 | { |
||
315 | if (count($params) != 2) { |
||
316 | return PEAR::raiseError('config-create: must have 2 parameters, root path and ' . |
||
317 | 'filename to save as'); |
||
318 | } |
||
319 | $root = $params[0]; |
||
320 | // Clean up the DIRECTORY_SEPARATOR mess |
||
321 | $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR; |
||
322 | $root = preg_replace(array('!\\\\+!', '!/+!', "!$ds2+!"), |
||
323 | array('/', '/', '/'), |
||
324 | $root); |
||
325 | if ($root{0} != '/') { |
||
326 | if (isset($options['windows'])) { |
||
327 | if (!preg_match('/^[A-Za-z]:/', $root)) { |
||
328 | return PEAR::raiseError('Root directory must be an absolute path beginning ' . |
||
329 | 'with "\\" or "C:\\", was: "' . $root . '"'); |
||
330 | } |
||
331 | } else { |
||
332 | return PEAR::raiseError('Root directory must be an absolute path beginning ' . |
||
333 | 'with "/", was: "' . $root . '"'); |
||
334 | } |
||
335 | } |
||
336 | $windows = isset($options['windows']); |
||
337 | if ($windows) { |
||
338 | $root = str_replace('/', '\\', $root); |
||
339 | } |
||
340 | if (!file_exists($params[1])) { |
||
341 | if (!@touch($params[1])) { |
||
342 | return PEAR::raiseError('Could not create "' . $params[1] . '"'); |
||
343 | } |
||
344 | } |
||
345 | $params[1] = realpath($params[1]); |
||
346 | $config = &new PEAR_Config($params[1], '#no#system#config#', false, false); |
||
347 | if ($root{strlen($root) - 1} == '/') { |
||
348 | $root = substr($root, 0, strlen($root) - 1); |
||
349 | } |
||
350 | $config->noRegistry(); |
||
351 | $config->set('php_dir', $windows ? "$root\\pear\\php" : "$root/pear/php", 'user'); |
||
352 | $config->set('data_dir', $windows ? "$root\\pear\\data" : "$root/pear/data"); |
||
353 | $config->set('www_dir', $windows ? "$root\\pear\\www" : "$root/pear/www"); |
||
354 | $config->set('cfg_dir', $windows ? "$root\\pear\\cfg" : "$root/pear/cfg"); |
||
355 | $config->set('ext_dir', $windows ? "$root\\pear\\ext" : "$root/pear/ext"); |
||
356 | $config->set('doc_dir', $windows ? "$root\\pear\\docs" : "$root/pear/docs"); |
||
357 | $config->set('test_dir', $windows ? "$root\\pear\\tests" : "$root/pear/tests"); |
||
358 | $config->set('cache_dir', $windows ? "$root\\pear\\cache" : "$root/pear/cache"); |
||
359 | $config->set('download_dir', $windows ? "$root\\pear\\download" : "$root/pear/download"); |
||
360 | $config->set('temp_dir', $windows ? "$root\\pear\\temp" : "$root/pear/temp"); |
||
361 | $config->set('bin_dir', $windows ? "$root\\pear" : "$root/pear"); |
||
362 | $config->writeConfigFile(); |
||
363 | $this->_showConfig($config); |
||
364 | $this->ui->outputData('Successfully created default configuration file "' . $params[1] . '"', |
||
365 | $command); |
||
366 | } |
||
367 | |||
368 | // }}} |
||
369 | |||
370 | function _showConfig(&$config) |
||
371 | { |
||
372 | $params = array('user'); |
||
373 | $keys = $config->getKeys(); |
||
374 | sort($keys); |
||
375 | $channel = 'pear.php.net'; |
||
376 | $data = array('caption' => 'Configuration (channel ' . $channel . '):'); |
||
377 | foreach ($keys as $key) { |
||
378 | $type = $config->getType($key); |
||
379 | $value = $config->get($key, 'user', $channel); |
||
380 | if ($type == 'password' && $value) { |
||
381 | $value = '********'; |
||
382 | } |
||
383 | if ($value === false) { |
||
384 | $value = 'false'; |
||
385 | } elseif ($value === true) { |
||
386 | $value = 'true'; |
||
387 | } |
||
388 | $data['data'][$config->getGroup($key)][] = |
||
389 | array($config->getPrompt($key) , $key, $value); |
||
390 | } |
||
391 | foreach ($config->getLayers() as $layer) { |
||
392 | $data['data']['Config Files'][] = |
||
393 | array(ucfirst($layer) . ' Configuration File', 'Filename' , |
||
394 | $config->getConfFile($layer)); |
||
395 | } |
||
396 | |||
397 | $this->ui->outputData($data, 'config-show'); |
||
398 | return true; |
||
399 | } |
||
400 | // {{{ _checkLayer() |
||
401 | |||
402 | /** |
||
403 | * Checks if a layer is defined or not |
||
404 | * |
||
405 | * @param string $layer The layer to search for |
||
406 | * @return mixed False on no error or the error message |
||
407 | */ |
||
408 | function _checkLayer($layer = null) |
||
409 | { |
||
410 | if (!empty($layer) && $layer != 'default') { |
||
411 | $layers = $this->config->getLayers(); |
||
412 | if (!in_array($layer, $layers)) { |
||
413 | return " only the layers: \"" . implode('" or "', $layers) . "\" are supported"; |
||
414 | } |
||
415 | } |
||
416 | return false; |
||
417 | } |
||
418 | |||
419 | // }}} |
||
420 | } |
||
421 | |||
422 | ?> |