Skip to content

Commit

Permalink
[FrameworkBundle] refactored the built-in web server
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 3, 2014
1 parent 887e6ff commit 5ea6437
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions src/Symfony/Bundle/FrameworkBundle/Command/ServerRunCommand.php
Expand Up @@ -43,7 +43,7 @@ protected function configure()
{
$this
->setDefinition(array(
new InputArgument('address', InputArgument::OPTIONAL, 'Address:port', 'localhost:8000'),
new InputArgument('address', InputArgument::OPTIONAL, 'Address:port', '127.0.0.1:8000'),
new InputOption('docroot', 'd', InputOption::VALUE_REQUIRED, 'Document root', 'web/'),
new InputOption('router', 'r', InputOption::VALUE_REQUIRED, 'Path to custom router script'),
))
Expand Down Expand Up @@ -84,20 +84,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('<error>Running PHP built-in server in production environment is NOT recommended!</error>');
}

$router = $input->getOption('router') ?: $this
->getContainer()
->get('kernel')
->locateResource(sprintf('@FrameworkBundle/Resources/config/router_%s.php', $env))
;

$output->writeln(sprintf("Server running on <info>%s</info>\n", $input->getArgument('address')));

if (defined('HHVM_VERSION')) {
$this->executeWithHHVM($input, $output, $env);
return;
$builder = $this->createHhvmProcessBuilder($input, $output, $env);
} else {
$builder = $this->createPhpProcessBuilder($input, $output, $env);
}

$builder = new ProcessBuilder(array(PHP_BINARY, '-S', $input->getArgument('address'), $router));
$builder->setWorkingDirectory($input->getOption('docroot'));
$builder->setTimeout(null);
$builder->getProcess()->run(function ($type, $buffer) use ($output) {
Expand All @@ -107,12 +101,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
});
}

protected function executeWithHHVM(InputInterface $input, OutputInterface $output, $env)
private function createPhpProcessBuilder(InputInterface $input, OutputInterface $output, $env)
{
$router = $input->getOption('router') ?: $this
->getContainer()
->get('kernel')
->locateResource(sprintf('@FrameworkBundle/Resources/config/router_%s.php', $env))
;

return new ProcessBuilder(array(PHP_BINARY, '-S', $input->getArgument('address'), $router));
}

private function createHhvmProcessBuilder(InputInterface $input, OutputInterface $output, $env)
{
list($ip, $port) = explode(':', $input->getArgument('address'));
$output->writeln(sprintf("Server(with HHVM) running on <info>$ip:$port</info>\n", $ip, $port));

$docroot = realpath($input->getOption('docroot'));
$bootstrap = ('prod' === $env ? 'app.php' : 'app_dev.php');
$bootstrap = 'prod' === $env ? 'app.php' : 'app_dev.php';
$config = <<<EOF
Server {
IP = $ip
Expand All @@ -123,9 +128,9 @@ protected function executeWithHHVM(InputInterface $input, OutputInterface $outpu
}
VirtualHost {
* {
Pattern = .*
RewriteRules {
* {
Pattern = .*
RewriteRules {
* {
pattern = .?
Expand All @@ -135,8 +140,8 @@ protected function executeWithHHVM(InputInterface $input, OutputInterface $outpu
# append the original query string
qsa = true
}
}
}
}
}
}
StaticFile {
Expand All @@ -155,16 +160,10 @@ protected function executeWithHHVM(InputInterface $input, OutputInterface $outpu
}
}
EOF;
$tmpfile = $this->getContainer()->get('kernel')->getCacheDir().DIRECTORY_SEPARATOR.'hhvm-server-'.md5($config).'.hdf';
file_put_contents($tmpfile, $config);
$builder = new ProcessBuilder(array(PHP_BINARY, '-ms', "-c$tmpfile"));
$builder->setWorkingDirectory($docroot);
$builder->setTimeout(null);
$builder->getProcess()->run(function ($type, $buffer) use ($output) {
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
$output->write($buffer);
}
});
}

$configFile = $this->getContainer()->get('kernel')->getCacheDir().'/hhvm-server-'.md5($config).'.hdf';
file_put_contents($configFile, $config);

return new ProcessBuilder(array(PHP_BINARY, '--mode', 'server', '--config', $configFile));
}
}

0 comments on commit 5ea6437

Please sign in to comment.