buffer = new \Threaded; $opts = getopt("", ["disable-readline"]); $this->readline = (extension_loaded("readline") and !isset($opts["disable-readline"])); $this->start(); } public function shutdown(){ $this->shutdown = true; } private function readLine(){ if(!$this->readline){ global $stdin; if(!is_resource($stdin)){ return ""; } return trim(fgets($stdin)); }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->shift(); } return null; } public function run(){ if(!$this->readline){ global $stdin; $stdin = fopen("php://stdin", "r"); stream_set_blocking($stdin, 0); } $lastLine = microtime(true); while(!$this->shutdown){ if(($line = $this->readLine()) !== ""){ $this->buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line); }elseif(!$this->shutdown and (microtime(true) - $lastLine) <= 0.1){ //Non blocking! Sleep to save CPU $this->synchronized(function(){ $this->wait(10000); }); } $lastLine = microtime(true); } } public function getThreadName(){ return "Console"; } }