This repository was archived by the owner on Dec 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathWebServerConfig.php
152 lines (125 loc) · 4.16 KB
/
WebServerConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\WebServerBundle;
/**
* @author Fabien Potencier <[email protected]>
*
* @deprecated since Symfony 4.4, to be removed in 5.0; the new Symfony local server has more features, you can use it instead.
*/
class WebServerConfig
{
private $hostname;
private $port;
private $documentRoot;
private $env;
private $router;
public function __construct(string $documentRoot, string $env, string $address = null, string $router = null)
{
if (!is_dir($documentRoot)) {
throw new \InvalidArgumentException(sprintf('The document root directory "%s" does not exist.', $documentRoot));
}
if (null === $file = $this->findFrontController($documentRoot, $env)) {
throw new \InvalidArgumentException(sprintf('Unable to find the front controller under "%s" (none of these files exist: "%s").', $documentRoot, implode('", "', $this->getFrontControllerFileNames($env))));
}
$_ENV['APP_FRONT_CONTROLLER'] = $file;
$this->documentRoot = $documentRoot;
$this->env = $env;
if (null !== $router) {
$absoluteRouterPath = realpath($router);
if (false === $absoluteRouterPath) {
throw new \InvalidArgumentException(sprintf('Router script "%s" does not exist.', $router));
}
$this->router = $absoluteRouterPath;
} else {
$this->router = __DIR__.'/Resources/router.php';
}
if (null === $address) {
$this->hostname = '127.0.0.1';
$this->port = $this->findBestPort();
} elseif (false !== $pos = strrpos($address, ':')) {
$this->hostname = substr($address, 0, $pos);
if ('*' === $this->hostname) {
$this->hostname = '0.0.0.0';
}
$this->port = substr($address, $pos + 1);
} elseif (ctype_digit($address)) {
$this->hostname = '127.0.0.1';
$this->port = $address;
} else {
$this->hostname = $address;
$this->port = $this->findBestPort();
}
if (!ctype_digit($this->port)) {
throw new \InvalidArgumentException(sprintf('Port "%s" is not valid.', $this->port));
}
}
public function getDocumentRoot()
{
return $this->documentRoot;
}
public function getEnv()
{
return $this->env;
}
public function getRouter()
{
return $this->router;
}
public function getHostname()
{
return $this->hostname;
}
public function getPort()
{
return $this->port;
}
public function getAddress()
{
return $this->hostname.':'.$this->port;
}
/**
* @return string contains resolved hostname if available, empty string otherwise
*/
public function getDisplayAddress()
{
if ('0.0.0.0' !== $this->hostname) {
return '';
}
if (false === $localHostname = gethostname()) {
return '';
}
return gethostbyname($localHostname).':'.$this->port;
}
private function findFrontController(string $documentRoot, string $env): ?string
{
$fileNames = $this->getFrontControllerFileNames($env);
foreach ($fileNames as $fileName) {
if (file_exists($documentRoot.'/'.$fileName)) {
return $fileName;
}
}
return null;
}
private function getFrontControllerFileNames(string $env): array
{
return ['app_'.$env.'.php', 'app.php', 'index_'.$env.'.php', 'index.php'];
}
private function findBestPort(): int
{
$port = 8000;
while (false !== $fp = @fsockopen($this->hostname, $port, $errno, $errstr, 1)) {
fclose($fp);
if ($port++ >= 8100) {
throw new \RuntimeException('Unable to find a port available to run the web server.');
}
}
return $port;
}
}