Skip to content

[FrameworkBundle] print error message if server couldn't be started #12258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Command/ServerStartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ 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>');
Expand All @@ -104,8 +117,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

$address = $input->getArgument('address');

if ($pid > 0) {
$output->writeln(sprintf('<info>Web server listening on http://%s</info>', $address));

Expand Down Expand Up @@ -144,6 +155,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

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.
*
Expand Down