Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 69e6a02

Browse filesBrowse files
committed
[WebServerBundle] added support for port auto-detection
1 parent b59b823 commit 69e6a02
Copy full SHA for 69e6a02

File tree

Expand file treeCollapse file tree

3 files changed

+22
-3
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+22
-3
lines changed

‎src/Symfony/Bundle/WebServerBundle/Command/ServerRunCommand.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/WebServerBundle/Command/ServerRunCommand.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function configure()
3434
{
3535
$this
3636
->setDefinition(array(
37-
new InputArgument('addressport', InputArgument::OPTIONAL, 'The address to listen to (can be address:port, address, or port)', '127.0.0.1:8000'),
37+
new InputArgument('addressport', InputArgument::OPTIONAL, 'The address to listen to (can be address:port, address, or port)', null),
3838
new InputOption('docroot', 'd', InputOption::VALUE_REQUIRED, 'Document root', null),
3939
new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script', null),
4040
))

‎src/Symfony/Bundle/WebServerBundle/Command/ServerStartCommand.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/WebServerBundle/Command/ServerStartCommand.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function configure()
3434
$this
3535
->setName('server:start')
3636
->setDefinition(array(
37-
new InputArgument('addressport', InputArgument::OPTIONAL, 'The address to listen to (can be address:port, address, or port)', '127.0.0.1:8000'),
37+
new InputArgument('addressport', InputArgument::OPTIONAL, 'The address to listen to (can be address:port, address, or port)', null),
3838
new InputOption('docroot', 'd', InputOption::VALUE_REQUIRED, 'Document root', null),
3939
new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script'),
4040
new InputOption('pidfile', '', InputOption::VALUE_REQUIRED, 'PID file', null),

‎src/Symfony/Bundle/WebServerBundle/WebServerConfig.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/WebServerBundle/WebServerConfig.php
+20-1Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class WebServerConfig
2727
private $env;
2828
private $router;
2929

30-
public function __construct($documentRoot, $env, $address = '127.0.0.1:8000', $router = null)
30+
public function __construct($documentRoot, $env, $address = null, $router = null)
3131
{
3232
if (!is_dir($documentRoot)) {
3333
throw new \InvalidArgumentException(sprintf('The document root directory "%s" does not exist.', $documentRoot));
@@ -43,6 +43,12 @@ public function __construct($documentRoot, $env, $address = '127.0.0.1:8000', $r
4343
$this->env = $env;
4444
$this->router = $router ?: __DIR__.'/Resources/router.php';
4545

46+
if (null === $address) {
47+
$this->address = '127.0.0.1';
48+
$this->port = $this->findBestPort();
49+
$address = $this->address.':'.$this->port;
50+
}
51+
4652
if (false !== $pos = strrpos($address, ':')) {
4753
$this->hostname = substr($address, 0, $pos);
4854
$this->port = substr($address, $pos + 1);
@@ -99,4 +105,17 @@ private function guessFrontController($documentRoot, $env)
99105
}
100106
}
101107
}
108+
109+
private function findBestPort()
110+
{
111+
$port = 8000;
112+
while (false !== $fp = @fsockopen('127.0.0.1', $port, $errno, $errstr, 1)) {
113+
fclose($fp);
114+
if ($port++ >= 8100) {
115+
throw new \RuntimeException('Unable to find a port available to run the web server.');
116+
}
117+
}
118+
119+
return $port;
120+
}
102121
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.