public function start()
{
$port = mt_rand(1025, 65535);
$this->address = 'localhost:' . $port;
$command = sprintf('php -S %s -t %s %s >> /dev/null 2>&1 & echo $!', $this->address, $this->docroot, $this->router);
// execute the command and store the process ID
$output = array();
exec($command, $output);
$this->pid = (int) $output[0];
// wait until it's listening for connections to avoid race conditions
$start = microtime(true);
while (($sock = @fsockopen('localhost', $port, $errno, $errstr, 10)) === false) {
// set a 5 secs timeout waiting for the server to start
if (microtime(true) > $start + 5) {
$this->pid = false;
// signal failure
$this->address = false;
break;
}
}
if ($sock !== false) {
fclose($sock);
}
return $this->address;
}