1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Elgg\Cli; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\Question; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Adds interaction to a console command |
14
|
|
|
*/ |
15
|
|
|
trait ConsoleInteractions { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var InputInterface |
19
|
|
|
*/ |
20
|
|
|
protected $input; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var OutputInterface |
24
|
|
|
*/ |
25
|
|
|
protected $output; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Ask a question |
29
|
|
|
* |
30
|
|
|
* @param string $question Question to ask |
31
|
|
|
* @param mixed $default Default value |
32
|
|
|
* @param bool $hidden Hide response |
33
|
|
|
* @param bool $required User input is required |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function ask($question, $default = null, $hidden = false, $required = true) { |
38
|
|
|
|
39
|
|
|
$helper = $this->getHelper('question'); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$q = new Question($question, $default); |
42
|
|
|
|
43
|
|
|
if ($hidden) { |
44
|
|
|
$q->setHidden(true); |
45
|
|
|
$q->setHiddenFallback(false); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($required) { |
49
|
|
|
$q->setValidator([ |
50
|
|
|
$this, |
51
|
|
|
'assertNotEmpty' |
52
|
|
|
]); |
53
|
|
|
$q->setMaxAttempts(2); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $helper->ask($this->input, $this->output, $q); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Write messages to output buffer |
61
|
|
|
* |
62
|
|
|
* @param string|array $messages Messages |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function write($messages) { |
67
|
|
|
$this->output->writeln($messages); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns option value |
72
|
|
|
* |
73
|
|
|
* @param string $name Option name |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function option($name) { |
78
|
|
|
return $this->input->getOption($name); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns argument value |
83
|
|
|
* |
84
|
|
|
* @param string $name Argument name |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function argument($name) { |
89
|
|
|
return $this->input->getArgument($name); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Question validator for required user response |
94
|
|
|
* |
95
|
|
|
* @param mixed $answer User answer |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function assertNotEmpty($answer) { |
100
|
|
|
if (empty($answer)) { |
101
|
|
|
throw new \RuntimeException('Please enter a required answer'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $answer; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Dump and output system and error messages |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
protected function dumpRegisters() { |
112
|
|
|
$registers = _elgg_services()->systemMessages->loadRegisters(); |
113
|
|
|
|
114
|
|
|
foreach ($registers as $prop => $values) { |
115
|
|
|
if (!empty($values)) { |
116
|
|
|
foreach ($values as $msg) { |
117
|
|
|
$tag = $prop == 'error' ? 'error' : 'info'; |
118
|
|
|
$this->write(elgg_format_element($tag, [], $msg)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |