10 const PS1 =
"\001\033[1;32m\002>>>\001\033[0m\002 ";
14 $this->manager = $manager;
16 $this->prompt = static::PS1;
19 protected function filter($data)
21 if (substr($data, -1) ===
"\n") {
22 $data = (string) substr($data, 0, -1);
26 $lines = explode(
"\n", $data);
27 $pattern = $this->manager->getEvalLocation();
28 $pattern =
" in $pattern : eval()'d code on line ";
29 $pLen = strlen($pattern);
30 foreach ($lines as $line) {
31 $pos = strpos($line, $pattern);
33 $matchEnd = strspn(substr($line, $pos + $pLen),
'1234567890');
34 if ($pos + $pLen + $matchEnd === strlen($line)) {
35 $output[] = (string) substr($line, 0, $pos);
43 return implode(
"\n", $output) .
"\n";
46 protected function signalHandler($signo)
52 if (!$this->manager->isWorking()) {
58 $this->manager->sendSignal($signo);
61 protected function lineHandler($line)
67 readline_callback_handler_remove();
69 if ($line === null || $line ==
"exit") {
79 readline_add_history($line);
80 $this->manager->sendCommands($line);
88 $stdout = $stderr = $control = null;
89 $this->manager->prepare($stdout, $stderr, $control);
129 foreach ($signals as $signal) {
130 if (!defined($signal)) {
134 if (!pcntl_signal(constant($signal), array($this,
'signalHandler'),
true)) {
135 throw new \RuntimeException(
'Unable to set up signal handler for ' . $signal);
141 readline_write_history();
146 $r = array(STDIN, $stdout, $stderr, $control);
149 $nb = @stream_select($r, $w, $e, null);
150 pcntl_signal_dispatch();
156 if (in_array(STDIN, $r)) {
157 if ($this->manager->isWorking()) {
160 readline_callback_read_char();
164 if (in_array($stdout, $r)) {
165 $read = fread($stdout, 8192);
166 if ($read !==
false) {
167 fwrite(STDOUT, $this->filter($read));
171 if (in_array($stderr, $r)) {
172 $read = fread($stderr, 8192);
173 if ($read !==
false) {
174 fwrite(STDERR, $this->filter($read));
178 if (in_array($control, $r)) {
180 $this->manager->runOnce();
181 }
catch (\RuntimeException $e) {
186 if (!$this->manager->isWorking()) {
187 $this->prompt = static::PS1;
190 if (!readline_callback_handler_install($this->prompt, array($this,
'lineHandler'))) {
191 throw new \RuntimeException();