1: <?php
2: /**
3: * Wrapper for Horde_Cli that adds functionality specific to Horde
4: * applications.
5: *
6: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (LGPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
10: *
11: * @todo Extend Horde_Cli when we can use LSB of PHP 5.3 in Horde_Cli::init().
12: *
13: * @author Jan Schneider <jan@horde.org>
14: * @package Core
15: */
16: class Horde_Core_Cli
17: {
18: /**
19: * The Horde_Cli object we are wrapping.
20: *
21: * @var Horde_Cli
22: */
23: protected $_cli;
24:
25: /**
26: * Constructor.
27: */
28: public function __construct()
29: {
30: $this->_cli = Horde_Cli::init();
31: }
32:
33: /**
34: * Proxy method.
35: */
36: public function __call($method, $args)
37: {
38: return call_user_func_array(array($this->_cli, $method), $args);
39: }
40:
41: /**
42: * Shows a prompt for a single configuration setting.
43: *
44: * @param Horde_Variables $vars This is going to be populated with the
45: * answers.
46: * @param string $prefix The current prefix for $name.
47: * @param string $name The name of the configuration setting.
48: * @param array $field A part of the parsed configuration tree as
49: * returned from Horde_Config.
50: */
51: public function question($vars, $prefix, $name, $field)
52: {
53: if (!isset($field['desc'])) {
54: // This is a <configsection>.
55: foreach ($field as $sub => $sub_field) {
56: $this->question($vars, $prefix . '__' . $name, $sub, $sub_field);
57: }
58: return;
59: }
60:
61: $question = $field['desc'];
62: $default = $field['default'];
63: $values = null;
64: if (isset($field['switch'])) {
65: $values = array();
66: foreach ($field['switch'] as $case => $case_field) {
67: $values[$case] = $case_field['desc'];
68: }
69: } else {
70: switch ($field['_type']) {
71: case 'boolean':
72: $values = array(true => 'Yes', false => 'No');
73: $default = (int)$default;
74: break;
75: case 'enum':
76: $values = $field['values'];
77: break;
78: }
79: if (!empty($field['required'])) {
80: $question .= $this->red('*');
81: }
82: }
83:
84: while (true) {
85: if ($name == 'password') {
86: $value = $this->passwordPrompt($question);
87: } else {
88: $value = $this->prompt($question, $values, $default);
89: }
90: if (empty($field['required']) || $value !== '') {
91: break;
92: } else {
93: $this->writeln($this->red('This field is required.'));
94: }
95: }
96:
97: if (isset($field['switch']) &&
98: !empty($field['switch'][$value]['fields'])) {
99: foreach ($field['switch'][$value]['fields'] as $sub => $sub_field) {
100: $this->question($vars, $prefix, $sub, $sub_field);
101: }
102: }
103:
104: $vars->set($prefix . '__' . $name, $value);
105: }
106: }
107: