-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.php
More file actions
executable file
·206 lines (176 loc) · 5.21 KB
/
Copy pathShell.php
File metadata and controls
executable file
·206 lines (176 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace Nitso\SqlConsole;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
/**
* A modified version of Symfony\Component\Console\Shell v2.8
* https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Console/Shell.php
*/
class Shell
{
/**
* @var Application
*/
protected $application;
/**
* @var ConsoleOutput
*/
protected $output;
/**
* @var string
*/
private $history;
/**
* @var bool
*/
private $hasReadline;
/**
* @var bool
*/
private $defaultIsInteractive;
/**
* @var int
*/
private $defaultVerbosity;
/**
* Shell constructor.
* @param Application $application
*/
public function __construct(Application $application)
{
$this->application = $application;
$this->output = new ConsoleOutput();
$this->hasReadline = function_exists('readline');
$this->history = getenv('HOME') . '/.history_' . $application->getName();
}
/**
* Runs the shell.
*/
public function run()
{
$this->application->setAutoExit(false);
$this->application->setCatchExceptions(true);
if ($this->hasReadline) {
readline_read_history($this->history);
readline_completion_function(array($this, 'autocompleter'));
}
$this->output->writeln($this->getHeader());
$this->saveDefaultIOBehavior();
while (true) {
$command = $this->readline();
if (false === $command) {
$this->output->writeln("\n");
break;
}
if ($this->hasReadline) {
readline_add_history($command);
readline_write_history($this->history);
}
$input = new StringInput($command);
$input->setInteractive($this->defaultIsInteractive);
$ret = $this->application->run($input, $this->output);
$this->output->setVerbosity($this->defaultVerbosity);
if (0 !== $ret) {
$this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret));
}
}
}
/**
* Saving options from shell start command so you can set verbosity
* Restoring these options later before/after executing every command
* @return void
*/
private function saveDefaultIOBehavior()
{
$shellInput = new ArgvInput();
$this->application->preConfigureIO($shellInput, $this->output);
$this->defaultIsInteractive = $shellInput->isInteractive();
$this->defaultVerbosity = $this->output->getVerbosity();
}
/**
* Reads a single line from standard input.
*
* @return string The single line from standard input
*/
private function readline()
{
if ($this->hasReadline) {
$line = readline($this->getPrompt());
} else {
$this->output->write($this->getPrompt());
$line = fgets(STDIN, 1024);
$line = (false === $line || '' === $line) ? false : rtrim($line);
}
return $line;
}
/**
* Renders a prompt.
*
* @return string
*/
protected function getPrompt()
{
// using the formatter here is required when using readline
return $this->output->getFormatter()->format($this->application->getPrompt() . ' > ');
}
/**
* @return ConsoleOutput
*/
protected function getOutput()
{
return $this->output;
}
/**
* @return Application
*/
protected function getApplication()
{
return $this->application;
}
/**
* Shell header.
* @return string
*/
protected function getHeader()
{
return <<<EOF
Welcome to the <info>{$this->application->getName()}</info> shell.
Type <comment>help</comment> for some help, <comment>list</comment> to get a list of available commands, <comment>exit</comment> to exit the shell.
EOF;
}
/**
* Tries to return autocompletion for the current entered text.
*
* @param string $text The last segment of the entered text
*
* @return array A list of guessed strings
*/
private function autocompleter($text)
{
$info = readline_info();
// try to overcome some buggy windows libraries
if (!isset($info['end']) && PHP_OS == 'WINNT') {
$info['end'] = $info['point'];
}
$text = substr($info['line_buffer'], 0, $info['end']);
if ($info['point'] !== $info['end']) {
return array();
}
// task name?
if (false === strpos($text, ' ') || !$text) {
return array_keys($this->application->all());
}
// options and arguments?
try {
$command = $this->application->find(substr($text, 0, strpos($text, ' ')));
} catch (\Exception $e) {
return array();
}
$list = array('--help');
foreach ($command->getDefinition()->getOptions() as $option) {
$list[] = '--' . $option->getName();
}
return $list;
}
}