Skip to content

Commit

Permalink
bug #14436 Show a better error when the port is in use (dosten)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3 branch.

Discussion
----------

Show a better error when the port is in use

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #14275
| License       | MIT
| Doc PR        | -

This is a backport of #14063

Commits
-------

34e000e Show a better error when the port is in use
  • Loading branch information
fabpot committed Apr 27, 2015
2 parents 3be6492 + 34e000e commit 7f24883
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php
Expand Up @@ -95,16 +95,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$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>');
}

if (null === $builder = $this->createPhpProcessBuilder($input, $output, $env)) {
if (null === $builder = $this->createPhpProcessBuilder($output, $address, $input->getOption('router'), $env)) {
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.');

$builder->setWorkingDirectory($documentRoot);
Expand All @@ -127,9 +140,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $process->getExitCode();
}

private function createPhpProcessBuilder(InputInterface $input, OutputInterface $output, $env)
private function isOtherServerProcessRunning($address)
{
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 = $input->getOption('router') ?: $this
$router = $router ?: $this
->getContainer()
->get('kernel')
->locateResource(sprintf('@FrameworkBundle/Resources/config/router_%s.php', $env))
Expand All @@ -150,6 +178,6 @@ private function createPhpProcessBuilder(InputInterface $input, OutputInterface
return;
}

return new ProcessBuilder(array($binary, '-S', $input->getArgument('address'), $router));
return new ProcessBuilder(array($binary, '-S', $address, $router));
}
}

0 comments on commit 7f24883

Please sign in to comment.