Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 126f4d9

Browse files
committed
[WebServerBundle] added support for port auto-detection
1 parent 6f689d6 commit 126f4d9

File tree

6 files changed

+26
-13
lines changed

6 files changed

+26
-13
lines changed

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

Lines 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)'),
3838
new InputOption('docroot', 'd', InputOption::VALUE_REQUIRED, 'Document root'),
3939
new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script'),
4040
))

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

Lines 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)'),
3838
new InputOption('docroot', 'd', InputOption::VALUE_REQUIRED, 'Document root'),
3939
new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script'),
4040
new InputOption('pidfile', null, InputOption::VALUE_REQUIRED, 'PID file'),

src/Symfony/Bundle/WebServerBundle/Command/ServerStatusCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bundle\WebServerBundle\Command;
1313

1414
use Symfony\Bundle\WebServerBundle\WebServer;
15-
use Symfony\Component\Console\Input\InputArgument;
1615
use Symfony\Component\Console\Input\InputInterface;
1716
use Symfony\Component\Console\Input\InputOption;
1817
use Symfony\Component\Console\Output\OutputInterface;

src/Symfony/Bundle/WebServerBundle/Command/ServerStopCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bundle\WebServerBundle\Command;
1313

1414
use Symfony\Bundle\WebServerBundle\WebServer;
15-
use Symfony\Component\Console\Input\InputArgument;
1615
use Symfony\Component\Console\Input\InputInterface;
1716
use Symfony\Component\Console\Output\OutputInterface;
1817
use Symfony\Component\Console\Input\InputOption;

src/Symfony/Bundle/WebServerBundle/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2004-2016 Fabien Potencier
1+
Copyright (c) 2004-2017 Fabien Potencier
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

src/Symfony/Bundle/WebServerBundle/WebServerConfig.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111

1212
namespace Symfony\Bundle\WebServerBundle;
1313

14-
use Symfony\Component\Process\PhpExecutableFinder;
15-
use Symfony\Component\Process\Process;
16-
use Symfony\Component\Process\ProcessBuilder;
17-
use Symfony\Component\Process\Exception\RuntimeException;
18-
1914
/**
2015
* @author Fabien Potencier <[email protected]>
2116
*/
@@ -27,7 +22,7 @@ class WebServerConfig
2722
private $env;
2823
private $router;
2924

30-
public function __construct($documentRoot, $env, $address = '127.0.0.1:8000', $router = null)
25+
public function __construct($documentRoot, $env, $address = null, $router = null)
3126
{
3227
if (!is_dir($documentRoot)) {
3328
throw new \InvalidArgumentException(sprintf('The document root directory "%s" does not exist.', $documentRoot));
@@ -43,15 +38,22 @@ public function __construct($documentRoot, $env, $address = '127.0.0.1:8000', $r
4338
$this->env = $env;
4439
$this->router = $router ?: __DIR__.'/Resources/router.php';
4540

46-
if (false !== $pos = strrpos($address, ':')) {
41+
if (null === $address) {
42+
$this->hostname = '127.0.0.1';
43+
$this->port = $this->findBestPort();
44+
} elseif (false !== $pos = strrpos($address, ':')) {
4745
$this->hostname = substr($address, 0, $pos);
4846
$this->port = substr($address, $pos + 1);
4947
} elseif (ctype_digit($address)) {
5048
$this->hostname = '127.0.0.1';
5149
$this->port = $address;
5250
} else {
5351
$this->hostname = $address;
54-
$this->port = 80;
52+
$this->port = $this->findBestPort();
53+
}
54+
55+
if (!ctype_digit($this->port)) {
56+
throw new \InvalidArgumentException(sprintf('Port "%s" is not valid.', $this->port));
5557
}
5658
}
5759

@@ -99,4 +101,17 @@ private function guessFrontController($documentRoot, $env)
99101
}
100102
}
101103
}
104+
105+
private function findBestPort()
106+
{
107+
$port = 8000;
108+
while (false !== $fp = @fsockopen('127.0.0.1', $port, $errno, $errstr, 1)) {
109+
fclose($fp);
110+
if ($port++ >= 8100) {
111+
throw new \RuntimeException('Unable to find a port available to run the web server.');
112+
}
113+
}
114+
115+
return $port;
116+
}
102117
}

0 commit comments

Comments
 (0)