12 $this->manager = $manager;
14 $this->prompt =
'>>> ';
17 protected function filter($data)
19 if (substr($data, -1) ===
"\n") {
20 $data = (string) substr($data, 0, -1);
24 $lines = explode(
"\n", $data);
25 $pattern = $this->manager->getEvalLocation();
26 $pattern =
" in $pattern : eval()'d code on line ";
27 $pLen = strlen($pattern);
28 foreach ($lines as $line) {
29 $pos = strpos($line, $pattern);
31 $matchEnd = strspn(substr($line, $pos + $pLen),
'1234567890');
32 if ($pos + $pLen + $matchEnd === strlen($line)) {
33 $output[] = (string) substr($line, 0, $pos);
41 return implode(
"\n", $output) .
"\n";
44 protected function signalHandler($signo)
50 if (!$this->manager->isWorking()) {
56 $this->manager->sendSignal($signo);
59 protected function lineHandler($line)
65 readline_callback_handler_remove();
67 if ($line === null || $line ==
"exit") {
77 readline_add_history($line);
78 $this->manager->sendCommands($line);
86 $stdout = $stderr = $control = null;
87 $this->manager->prepare($stdout, $stderr, $control);
127 foreach ($signals as $signal) {
128 if (!defined($signal)) {
132 if (!pcntl_signal(constant($signal), array($this,
'signalHandler'),
true)) {
133 throw new \RuntimeException(
'Unable to set up signal handler for ' . $signal);
139 readline_write_history();
144 $r = array(STDIN, $stdout, $stderr, $control);
147 $nb = @stream_select($r, $w, $e, null);
148 pcntl_signal_dispatch();
154 if (in_array(STDIN, $r)) {
155 if ($this->manager->isWorking()) {
158 readline_callback_read_char();
162 if (in_array($stdout, $r)) {
163 $read = fread($stdout, 8192);
164 if ($read !==
false) {
165 fwrite(STDOUT, $this->filter($read));
169 if (in_array($stderr, $r)) {
170 $read = fread($stderr, 8192);
171 if ($read !==
false) {
172 fwrite(STDERR, $this->filter($read));
176 if (in_array($control, $r)) {
178 $this->manager->runOnce();
179 }
catch (\RuntimeException $e) {
184 if (!$this->manager->isWorking()) {
185 $this->prompt =
'>>> ';
188 if (!readline_callback_handler_install($this->prompt, array($this,
'lineHandler'))) {
189 throw new \RuntimeException();