8000 bug #12258 [FrameworkBundle] print error message if server couldn't b… · symfony/symfony@fb1732a · GitHub
[go: up one dir, main page]

Skip to content

Commit fb1732a

Browse files
committed
bug #12258 [FrameworkBundle] print error message if server couldn't be started (xabbuh)
This PR was submitted for the master branch but it was merged into the 2.6 branch instead (closes #12258). Discussion ---------- [FrameworkBundle] print error message if server couldn't be started As @weaverryan noticed in symfony/symfony-docs#4005, the `server:run` command would provide you with a success message even if there were a web server already listening. Commits ------- 6b2537b print error message if server couldn't be started
2 parents 820b973 + 6b2537b commit fb1732a

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ServerStartCommand.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
9191
}
9292

9393
$env = $this->getContainer()->getParameter('kernel.environment');
94+
$address = $input->getArgument('address');
95+
96+
if (false === strpos($address, ':')) {
97+
$output->writeln('The address has to be of the form <comment>bind-address:port</comment>.');
98+
99+
return 1;
100+
}
101+
102+
if ($this->isOtherServerProcessRunning($address)) {
103+
$output->writeln(sprintf('<error>A process is already listening on http://%s.</error>', $address));
104+
105+
return 1;
106+
}
94107

95108
if ('prod' === $env) {
96109
$output->writeln('<error>Running PHP built-in server in production environment is NOT recommended!</error>');
@@ -104,8 +117,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
104117
return 1;
105118
}
106119

107-
$address = $input->getArgument('address');
108-
109120
if ($pid > 0) {
110121
$output->writeln(sprintf('<info>Web server listening on http://%s</info>', $address));
111122

@@ -144,6 +155,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
144155
}
145156
}
146157

158+
private function isOtherServerProcessRunning($address)
159+
{
160+
$lockFile = $this->getLockFile($address);
161+
162+
if (file_exists($lockFile)) {
163+
return true;
164+
}
165+
166+
list($hostname, $port) = explode(':', $address);
167+
168+
$fp = @fsockopen($hostname, $port, $errno, $errstr, 5);
169+
170+
if (false !== $fp) {
171+
fclose($fp);
172+
173+
return true;
174+
}
175+
176+
return false;
177+
}
178+
147179
/**
148180
* Creates a process to start PHP's built-in web server.
149181
*

0 commit comments

Comments
 (0)
0