8000 Web server bundle by fabpot · Pull Request #21039 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Web server bundle #21039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 6, 2017
Prev Previous commit
Next Next commit
made the router configurable via env vars
  • Loading branch information
fabpot committed Dec 30, 2016
commit ac1ba7700ef0009a9f91cb9f3012b044029ce272
48 changes: 48 additions & 0 deletions src/Symfony/Bundle/WebServerBundle/Command/ServerCommand.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Bundle\WebServerBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

/**
* Base methods for commands related to PHP's built-in web server.
*
Expand Down Expand Up @@ -62,4 +64,50 @@ protected function isOtherServerProcessRunning($address)

return false;
}

/**
* Determine the absolute file path for the router script, using the environment to choose a standard script
* if no custom router script is specified.
*
* @param string $documentRoot Document root
* @param string|null $router File path of the custom router script, if set by the user; otherwise null
* @param string $env The application environment
*
* @return string|bool The absolute file path of the router script, or false on failure
*/
protected function determineRouterScript($documentRoot, $router, $env)
{
if (null !== $router) {
return realpath($router);
}

if (false === $frontController = $this->guessFrontController($documentRoot, $env)) {
return false;
}

putenv('APP_FRONT_CONTROLLER='.$frontController);

return realpath($this
->getContainer()
->get('kernel')
->locateResource(sprintf('@WebServerBundle/Resources/router.php'))
);
}

private function guessFrontController($documentRoot, $env)
{
foreach (array('app', 'index') as $prefix) {
$file = sprintf('%s_%s.php', $prefix, $env);
if (file_exists($documentRoot.'/'.$file)) {
return $file;
}

$file = sprintf('%s.php', $prefix);
if (file_exists($documentRoot.'/'.$file)) {
return $file;
}
}

return false;
}
}
28 changes: 10 additions & 18 deletions src/Symfony/Bundle/WebServerBundle/Command/ServerRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$documentRoot = $input->getOption('docroot');

if (null === $documentRoot) {
if (null === $documentRoot = $input->getOption('docroot')) {
$documentRoot = $this->getContainer()->getParameter('kernel.root_dir').'/../web';
}

if (!is_dir($documentRoot)) {
$io->error(sprintf('The given document root directory "%s" does not exist', $documentRoot));
$io->error(sprintf('The document root directory "%s" does not exist', $documentRoot));

return 1;
}
Expand All @@ -102,14 +101,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

if (false === $router = $this->determineRouterScript($documentRoot, $input->getOption('router'), $env)) {
$io->error('Unable to guess the front controller file.');

return 1;
}

if ('prod' === $env) {
$io->error('Running PHP built-in server in production environment is NOT recommended!');
}

$io->success(sprintf('Server running on http://%s', $address));
$io->success(sprintf('Server listening on http://%s', $address));
$io->comment('Quit the server with CONTROL-C.');

if (null === $builder = $this->createPhpProcessBuilder($io, $address, $input->getOption('router'), $env)) {
if (null === $builder = $this->createPhpProcessBuilder($io, $address, $router, $env)) {
return 1;
}

Expand Down Expand Up @@ -149,19 +154,6 @@ protected function execute(InputInterface $input, OutputInterface $output)

private function createPhpProcessBuilder(SymfonyStyle $io, $address, $router, $env)
{
$router = $router ?: $this
->getContainer()
->get('kernel')
->locateResource(sprintf('@FrameworkBundle/Resources/router_%s.php', $env))
;

if (!file_exists($router)) {
$io->error(sprintf('The given router script "%s" does not exist.', $router));

return;
}

$router = realpath($router);
$finder = new PhpExecutableFinder();

if (false === $binary = $finder->find()) {
Expand Down
48 changes: 9 additions & 39 deletions src/Symfony/Bundle/WebServerBundle/Command/ServerStartCommand.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

$documentRoot = $input->getOption('docroot');

if (null === $documentRoot) {
if (null === $documentRoot = $input->getOption('docroot')) {
$documentRoot = $this->getContainer()->getParameter('kernel.root_dir').'/../web';
}

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

return 1;
}
8000
$env = $this->getContainer()->getParameter('kernel.environment');

if (false === $router = $this->determineRouterScript($input->getOption('router'), $env, $io)) {
return 1;
}

$address = $input->getArgument('address');

if (false === strpos($address, ':')) {
Expand All @@ -124,6 +117,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

if (false === $router = $this->determineRouterScript($documentRoot, $input->getOption('router'), $env)) {
$io->error('Unable to guess the front controller file.');

return 1;
}

if ('prod' === $env) {
$io->error('Running PHP built-in server in production environment is NOT recommended!');
}
Expand All @@ -137,7 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if ($pid > 0) {
$io->success(sprintf('Web server listening on http://%s', $address));
$io->success(sprintf('Server listening on http://%s', $address));

return;
}
Expand Down Expand Up @@ -174,35 +173,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

/**
* Determine the absolute file path for the router script, using the environment to choose a standard script
* if no custom router script is specified.
*
* @param string|null $router File path of the custom router script, if set by the user; otherwise null
* @param string $env The application environment
* @param SymfonyStyle $io An SymfonyStyle instance
*
* @return string|bool The absolute file path of the router script, or false on failure
*/
private function determineRouterScript($router, $env, SymfonyStyle $io)
{
if (null === $router) {
$router = $this
->getContainer()
->get('kernel')
->locateResource(sprintf('@FrameworkBundle/Resources/router_%s.php', $env))
;
}

if (false === $path = realpath($router)) {
$io->error(sprintf('The given router script "%s" does not exist.', $router));

return false;
}

return $path;
}

/**
* Creates a process to start PHP's built-in web server.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
return false;
}

$script = getenv('APP_FRONT_CONTROLLER') ?: 'index.php';

$_SERVER = array_merge($_SERVER, $_ENV);
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'app_dev.php';
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.$script;

// Since we are rewriting to app_dev.php, adjust SCRIPT_NAME and PHP_SELF accordingly
$_SERVER['SCRIPT_NAME'] = DIRECTORY_SEPARATOR.'app_dev.php';
$_SERVER['PHP_SELF'] = DIRECTORY_SEPARATOR.'app_dev.php';
$_SERVER['SCRIPT_NAME'] = DIRECTORY_SEPARATOR.$script;
$_SERVER['PHP_SELF'] = DIRECTORY_SEPARATOR.$script;

require 'app_dev.php';
require $script;

error_log(sprintf('%s:%d [%d]: %s', $_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_PORT'], http_response_code(), $_SERVER['REQUEST_URI']), 4);
8790 42 changes: 0 additions & 42 deletions src/Symfony/Bundle/WebServerBundle/Resources/router_prod.php

This file was deleted.

31 changes: 0 additions & 31 deletions src/Symfony/Bundle/WebServerBundle/Resources/router_test.php

This file was deleted.

0