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

Skip to content

Commit 66b1ee3

Browse files
committed
[WebServerBundle] moved most of the logic in a new class
1 parent 8329da6 commit 66b1ee3

File tree

7 files changed

+263
-297
lines changed

7 files changed

+263
-297
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"symfony/validator": "self.version",
7474
"symfony/var-dumper": "self.version",
7575
"symfony/web-profiler-bundle": "self.version",
76+
"symfony/web-server-bundle": "self.version",
7677
"symfony/workflow": "self.version",
7778
"symfony/yaml": "self.version"
7879
},

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

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -25,88 +25,6 @@ abstract class ServerCommand extends ContainerAwareCommand
2525
*/
2626
public function isEnabled()
2727
{
28-
if (defined('HHVM_VERSION')) {
29-
return false;
30-
}
31-
32-
return parent::isEnabled();
33-
}
34-
35-
/**
36-
* Determines the name of the lock file for a particular PHP web server process.
37-
*
38-
* @param string $address An address/port tuple
39-
*
40-
* @return string The filename
41-
*/
42-
protected function getLockFile($address)
43-
{
44-
return sys_get_temp_dir().'/'.strtr($address, '.:', '--').'.pid';
45-
}
46-
47-
protected function isOtherServerProcessRunning($address)
48-
{
49-
$lockFile = $this->getLockFile($address);
50-
if (file_exists($lockFile)) {
51-
return true;
52-
}
53-
54-
list($hostname, $port) = explode(':', $address);
55-
56-
$fp = @fsockopen($hostname, $port, $errno, $errstr, 5);
57-
58-
if (false !== $fp) {
59-
fclose($fp);
60-
61-
return true;
62-
}
63-
64-
return false;
65-
}
66-
67-
/**
68-
* Determine the absolute file path for the router script, using the environment to choose a standard script
69-
* if no custom router script is specified.
70-
*
71-
* @param string $documentRoot Document root
72-
* @param string|null $router File path of the custom router script, if set by the user; otherwise null
73-
* @param string $env The application environment
74-
*
75-
* @return string|bool The absolute file path of the router script, or false on failure
76-
*/
77-
protected function determineRouterScript($documentRoot, $router, $env)
78-
{
79-
if (null !== $router) {
80-
return realpath($router);
81-
}
82-
83-
if (false === $frontController = $this->guessFrontController($documentRoot, $env)) {
84-
return false;
85-
}
86-
87-
putenv('APP_FRONT_CONTROLLER='.$frontController);
88-
89-
return realpath($this
90-
->getContainer()
91-
->get('kernel')
92-
->locateResource(sprintf('@WebServerBundle/Resources/router.php'))
93-
);
94-
}
95-
96-
private function guessFrontController($documentRoot, $env)
97-
{
98-
foreach (array('app', 'index') as $prefix) {
99-
$file = sprintf('%s_%s.php', $prefix, $env);
100-
if (file_exists($documentRoot.'/'.$file)) {
101-
return $file;
102-
}
103-
104-
$file = sprintf('%s.php', $prefix);
105-
if (file_exists($documentRoot.'/'.$file)) {
106-
return $file;
107-
}
108-
}
109-
110-
return false;
28+
return !defined('HHVM_VERSION') && parent::isEnabled();
11129
}
11230
}

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

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@
1111

1212
namespace Symfony\Bundle\WebServerBundle\Command;
1313

14+
use Symfony\Bundle\WebServerBundle\WebServer;
1415
use Symfony\Component\Console\Input\InputArgument;
1516
use Symfony\Component\Console\Input\InputOption;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Output\OutputInterface;
1819
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1920
use Symfony\Component\Console\Style\SymfonyStyle;
20-
use Symfony\Component\Process\PhpExecutableFinder;
21-
use Symfony\Component\Process\Process;
22-
use Symfony\Component\Process\ProcessBuilder;
23-
use Symfony\Component\Process\Exception\RuntimeException;
2421

2522
/**
2623
* Runs Symfony application using a local web server.
@@ -36,10 +33,9 @@ protected function configure()
3633
{
3734
$this
3835
->setDefinition(array(
39-
new InputArgument('address', InputArgument::OPTIONAL, 'Address:port', '127.0.0.1'),
40-
new InputOption('port', 'p', InputOption::VALUE_REQUIRED, 'Address port number', '8000'),
36+
new InputArgument('addressport', InputArgument::OPTIONAL, 'The address to listen to (can be address:port, address, or port)', '127.0.0.1:8000'),
4137
new InputOption('docroot', 'd', InputOption::VALUE_REQUIRED, 'Document root', null),
42-
new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script'),
38+
new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script', dirname(__DIR__).'/Resources/router.php'),
4339
))
4440
->setName('server:run')
4541
->setDescription('Runs a local web server')
@@ -65,7 +61,6 @@ protected function configure()
6561
"prod", or "test".
6662
6763
See also: http://www.php.net/manual/en/features.commandline.webserver.php
68-
6964
EOF
7065
)
7166
;
@@ -83,85 +78,45 @@ protected function execute(InputInterface $input, OutputInterface $output)
8378
}
8479

8580
if (!is_dir($documentRoot)) {
86-
$io->error(sprintf('The document root directory "%s" does not exist', $documentRoot));
81+
$io->error(sprintf('The document root directory "%s" does not exist.', $documentRoot));
8782

8883
return 1;
8984
}
9085

9186
$env = $this->getContainer()->getParameter('kernel.environment');
92-
$address = $input->getArgument('address');
93-
94-
if (false === strpos($address, ':')) {
95-
$address = $address.':'.$input->getOption('port');
96-
}
97-
98-
if ($this->isOtherServerProcessRunning($address)) {
99-
$io->error(sprintf('A process is already listening on http://%s.', $address));
100-
101-
return 1;
102-
}
103-
104-
if (false === $router = $this->determineRouterScript($documentRoot, $input->getOption('router'), $env)) {
105-
$io->error('Unable to guess the front controller file.');
106-
107-
return 1;
108-
}
109-
11087
if ('prod' === $env) {
11188
$io->error('Running this server in production environment is NOT recommended!');
11289
}
11390

114-
$io->success(sprintf('Server listening on http://%s', $address));
115-
$io->comment('Quit the server with CONTROL-C.');
91+
$router = $input->getOption('router');
11692

117-
if (null === $builder = $this->createPhpProcessBuilder($io, $address, $router, $env)) {
118-
return 1;
119-
}
120-
121-
$builder->setWorkingDirectory($documentRoot);
122-
$builder->setTimeout(null);
123-
$process = $builder->getProcess();
12493
$callback = null;
125-
126-
if (OutputInterface::VERBOSITY_NORMAL > $output->getVerbosity()) {
127-
$process->disableOutput();
94+
$disableOutput = false;
95+
if ($output->isQuiet()) {
96+
$disableOutput = true;
12897
} else {
129-
try {
130-
$process->setTty(true);
131-
} catch (RuntimeException $e) {
132-
$callback = function ($type, $buffer) use ($output) {
133-
if (Process::ERR === $type && $output instanceof ConsoleOutputInterface) {
134-
$output = $output->getErrorOutput();
135-
}
136-
$output->write($buffer, false, OutputInterface::OUTPUT_RAW);
137-
};
138-
}
98+
$callback = function ($type, $buffer) use ($output) {
99+
if (Process::ERR === $type && $output instanceof ConsoleOutputInterface) {
100+
$output = $output->getErrorOutput();
101+
}
102+
$output->write($buffer, false, OutputInterface::OUTPUT_RAW);
103+
};
139104
}
140-
$process->run($callback);
141105

142-
if (!$process->isSuccessful()) {
143-
$errorMessages = array('Server terminated unexpectedly.');
106+
try {
107+
$server = new WebServer($input->getArgument('addressport'));
108+
$server->setConfig($documentRoot, $env);
144109

145-
if ($process->isOutputDisabled()) {
146-
$errorMessages[] = 'Run the command again with -v option for more details.';
147-
}
110+
$io->success(sprintf('Server listening on http://%s', $server->getAddress()));
111+
$io->comment('Quit the server with CONTROL-C.');
148112

149-
$io->error($errorMessages);
150-
}
113+
$exitCode = $server->run($router, $disableOutput, $callback);
114+
} catch (\Exception $e) {
115+
$io->error($e->getMessage());
151116

152-
return $process->getExitCode();
153-
}
154-
155-
private function createPhpProcessBuilder(SymfonyStyle $io, $address, $router, $env)
156-
{
157-
$finder = new PhpExecutableFinder();
158-
159-
if (false === $binary = $finder->find()) {
160-
$io->error('Unable to find PHP binary to run server.');
161-
162-
return;
117+
return 1;
163118
}
164119

165-
return new ProcessBuilder(array($binary, '-S', $address, $router));
120+
return $exitCode;
166121
}
167122
}

0 commit comments

Comments
 (0)