print error message if server couldn't be started

This commit is contained in:
Christian Flothmann 2014-10-19 21:30:31 +02:00 committed by Fabien Potencier
parent 820b973dab
commit 6b2537b902
1 changed files with 34 additions and 2 deletions

View File

@ -91,6 +91,19 @@ EOF
}
$env = $this->getContainer()->getParameter('kernel.environment');
$address = $input->getArgument('address');
if (false === strpos($address, ':')) {
$output->writeln('The address has to be of the form <comment>bind-address:port</comment>.');
return 1;
}
if ($this->isOtherServerProcessRunning($address)) {
$output->writeln(sprintf('<error>A process is already listening on http://%s.</error>', $address));
return 1;
}
if ('prod' === $env) {
$output->writeln('<error>Running PHP built-in server in production environment is NOT recommended!</error>');
@ -104,8 +117,6 @@ EOF
return 1;
}
$address = $input->getArgument('address');
if ($pid > 0) {
$output->writeln(sprintf('<info>Web server listening on http://%s</info>', $address));
@ -144,6 +155,27 @@ EOF
}
}
private function isOtherServerProcessRunning($address)
{
$lockFile = $this->getLockFile($address);
if (file_exists($lockFile)) {
return true;
}
list($hostname, $port) = explode(':', $address);
$fp = @fsockopen($hostname, $port, $errno, $errstr, 5);
if (false !== $fp) {
fclose($fp);
return true;
}
return false;
}
/**
* Creates a process to start PHP's built-in web server.
*