Show a better error when the port is in use
This commit is contained in:
parent
db243627e5
commit
34e000e59a
|
@ -95,16 +95,29 @@ EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
$env = $this->getContainer()->getParameter('kernel.environment');
|
$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) {
|
if ('prod' === $env) {
|
||||||
$output->writeln('<error>Running PHP built-in server in production environment is NOT recommended!</error>');
|
$output->writeln('<error>Running PHP built-in server in production environment is NOT recommended!</error>');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === $builder = $this->createPhpProcessBuilder($input, $output, $env)) {
|
if (null === $builder = $this->createPhpProcessBuilder($output, $address, $input->getOption('router'), $env)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
$output->writeln(sprintf("Server running on <info>http://%s</info>\n", $input->getArgument('address')));
|
$output->writeln(sprintf("Server running on <info>http://%s</info>\n", $address));
|
||||||
$output->writeln('Quit the server with CONTROL-C.');
|
$output->writeln('Quit the server with CONTROL-C.');
|
||||||
|
|
||||||
$builder->setWorkingDirectory($documentRoot);
|
$builder->setWorkingDirectory($documentRoot);
|
||||||
|
@ -127,9 +140,24 @@ EOF
|
||||||
return $process->getExitCode();
|
return $process->getExitCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createPhpProcessBuilder(InputInterface $input, OutputInterface $output, $env)
|
private function isOtherServerProcessRunning($address)
|
||||||
{
|
{
|
||||||
$router = $input->getOption('router') ?: $this
|
list($hostname, $port) = explode(':', $address);
|
||||||
|
|
||||||
|
$fp = @fsockopen($hostname, $port, $errno, $errstr, 5);
|
||||||
|
|
||||||
|
if (false !== $fp) {
|
||||||
|
fclose($fp);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createPhpProcessBuilder(OutputInterface $output, $address, $router, $env)
|
||||||
|
{
|
||||||
|
$router = $router ?: $this
|
||||||
->getContainer()
|
->getContainer()
|
||||||
->get('kernel')
|
->get('kernel')
|
||||||
->locateResource(sprintf('@FrameworkBundle/Resources/config/router_%s.php', $env))
|
->locateResource(sprintf('@FrameworkBundle/Resources/config/router_%s.php', $env))
|
||||||
|
@ -150,6 +178,6 @@ EOF
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ProcessBuilder(array($binary, '-S', $input->getArgument('address'), $router));
|
return new ProcessBuilder(array($binary, '-S', $address, $router));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue