/usr/share/php/Symfony/Bundle/WebServerBundle/WebServer.php is in php-symfony-web-server-bundle 3.4.6+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | <?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\WebServerBundle;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\RuntimeException;
/**
* Manages a local HTTP web server.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class WebServer
{
const STARTED = 0;
const STOPPED = 1;
public function run(WebServerConfig $config, $disableOutput = true, callable $callback = null)
{
if ($this->isRunning()) {
throw new \RuntimeException(sprintf('A process is already listening on http://%s.', $config->getAddress()));
}
$process = $this->createServerProcess($config);
if ($disableOutput) {
$process->disableOutput();
$callback = null;
} else {
try {
$process->setTty(true);
$callback = null;
} catch (RuntimeException $e) {
}
}
$process->run($callback);
if (!$process->isSuccessful()) {
$error = 'Server terminated unexpectedly.';
if ($process->isOutputDisabled()) {
$error .= ' Run the command again with -v option for more details.';
}
throw new \RuntimeException($error);
}
}
public function start(WebServerConfig $config, $pidFile = null)
{
$pidFile = $pidFile ?: $this->getDefaultPidFile();
if ($this->isRunning($pidFile)) {
throw new \RuntimeException(sprintf('A process is already listening on http://%s.', $config->getAddress()));
}
$pid = pcntl_fork();
if ($pid < 0) {
throw new \RuntimeException('Unable to start the server process.');
}
if ($pid > 0) {
return self::STARTED;
}
if (posix_setsid() < 0) {
throw new \RuntimeException('Unable to set the child process as session leader.');
}
$process = $this->createServerProcess($config);
$process->disableOutput();
$process->start();
if (!$process->isRunning()) {
throw new \RuntimeException('Unable to start the server process.');
}
file_put_contents($pidFile, $config->getAddress());
// stop the web server when the lock file is removed
while ($process->isRunning()) {
if (!file_exists($pidFile)) {
$process->stop();
}
sleep(1);
}
return self::STOPPED;
}
public function stop($pidFile = null)
{
$pidFile = $pidFile ?: $this->getDefaultPidFile();
if (!file_exists($pidFile)) {
throw new \RuntimeException('No web server is listening.');
}
unlink($pidFile);
}
public function getAddress($pidFile = null)
{
$pidFile = $pidFile ?: $this->getDefaultPidFile();
if (!file_exists($pidFile)) {
return false;
}
return file_get_contents($pidFile);
}
public function isRunning($pidFile = null)
{
$pidFile = $pidFile ?: $this->getDefaultPidFile();
if (!file_exists($pidFile)) {
return false;
}
$address = file_get_contents($pidFile);
$pos = strrpos($address, ':');
$hostname = substr($address, 0, $pos);
$port = substr($address, $pos + 1);
if (false !== $fp = @fsockopen($hostname, $port, $errno, $errstr, 1)) {
fclose($fp);
return true;
}
unlink($pidFile);
return false;
}
/**
* @return Process The process
*/
private function createServerProcess(WebServerConfig $config)
{
$finder = new PhpExecutableFinder();
if (false === $binary = $finder->find(false)) {
throw new \RuntimeException('Unable to find the PHP binary.');
}
$process = new Process(array_merge(array($binary), $finder->findArguments(), array('-dvariables_order=EGPCS', '-S', $config->getAddress(), $config->getRouter())));
$process->setWorkingDirectory($config->getDocumentRoot());
$process->setTimeout(null);
if (in_array('APP_ENV', explode(',', getenv('SYMFONY_DOTENV_VARS')))) {
$process->setEnv(array('APP_ENV' => false));
$process->inheritEnvironmentVariables();
}
return $process;
}
private function getDefaultPidFile()
{
return getcwd().'/.web-server-pid';
}
}
|