7 const OP_START =
"\x00";
9 const OP_READY =
"\x02";
16 public function __construct($socket)
18 parent::__construct(
'\\fpoirotte\\push\\Manager');
19 $this->socket = $socket;
21 $this->scope = array();
29 list($file, $line) = $this->evaluate(
true);
32 $this->send(self::OP_READY,
"$file($line)");
40 protected function cancel()
42 if ($this->child === null) {
46 echo
"Cancelling...\n";
47 posix_kill($this->child, SIGKILL);
48 pcntl_signal_dispatch();
51 protected function handleCOMMAND($data)
54 $this->child = pcntl_fork();
56 if ($this->child < 0) {
57 throw new \RuntimeException(
'Could not run command');
60 if ($this->child == 0) {
64 pcntl_signal(SIGINT, SIG_DFL,
true);
67 $this->send(self::OP_START, (
string) getmypid());
71 ini_set(
'display_errors',
'0');
73 ini_set(
'display_errors',
'1');
74 $this->outputResult();
88 posix_kill($pid, SIGKILL);
89 pcntl_signal_dispatch();
90 $this->send(self::OP_END);
96 pcntl_signal(SIGINT, array($this,
'cancel'),
true);
101 $child = pcntl_waitpid(-1, $status);
105 if (pcntl_wifexited($child) && pcntl_wexitstatus($child) === 0) {
113 $this->send(self::OP_END);
118 protected function evaluate($magic =
false)
122 return array(__FILE__, __LINE__ + 4);
125 extract($this->scope, EXTR_OVERWRITE | EXTR_PREFIX_SAME,
'_');
126 $this->result = eval(
'return ' . $this->data);
127 $this->scope = get_defined_vars();
128 return $this->result;
131 protected function outputResult()
133 if ($this->result === null) {
137 fwrite(STDOUT, var_export($this->result,
true));
140 protected function handleSIGNAL($data)
142 $signo = (int) $data;
146 throw new \RuntimeException();
149 posix_kill(getmypid(), $signo);
150 if (function_exists(
'pcntl_signal_dispatch')) {
151 pcntl_signal_dispatch();