stream = $stream; $this->start(PTHREADS_INHERIT_ALL & ~PTHREADS_INHERIT_CLASSES); } private function readLine(){ if(!$this->readline){ $line = trim(fgets($this->fp)); }else{ $line = trim(readline("> ")); if($line != ""){ readline_add_history($line); } } return $line; } /** * Reads a line from console, if available. Returns null if not available * * @return string|null */ public function getLine(){ if($this->buffer->count() !== 0){ return $this->buffer->synchronized(function (){ return $this->buffer->shift(); }); } return null; } public function run(){ $this->buffer = new \Threaded; if(extension_loaded("readline") and $this->stream === "php://stdin"){ $this->readline = true; }else{ $this->readline = false; $this->fp = fopen($this->stream, "r"); stream_set_blocking($this->fp, 1); //Non-blocking STDIN won't work on Windows } $lastLine = microtime(true); while(true){ if(($line = $this->readLine()) !== ""){ $this->buffer->synchronized(function (\Threaded $buffer, $line){ $buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line); }, $this->buffer, $line); $lastLine = microtime(true); }elseif((microtime(true) - $lastLine) <= 0.1){ //Non blocking! Sleep to save CPU usleep(40000); } } } }