8000 made the router configurable via env vars · symfony/symfony@21475e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 21475e2

Browse files
committed
made the router configurable via env vars
1 parent 2a6d158 commit 21475e2

File tree

6 files changed

+71
-134
lines changed

6 files changed

+71
-134
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,50 @@ protected function isOtherServerProcessRunning($address)
6262

6363
return false;
6464
}
65+
66+
/**
67+
* Determine the absolute file path for the router script, using the environment to choose a standard script
68+
* if no custom router script is specified.
69+
*
70+
* @param string $documentRoot Document root
71+
* @param string|null $router File path of the custom router script, if set by the user; otherwise null
72+
* @param string $env The application environment
73+
*
74+
* @return string|bool The absolute file path of the router script, or false on failure
75+
*/
76+
protected function determineRouterScript($documentRoot, $router, $env)
77+
{
78+
if (null !== $router) {
79+
return realpath($router);
80+
}
81+
82+
if (false === $frontController = $this->guessFrontController($documentRoot, $env))) {
83+
return false;
84+
}
85+
86+
putenv('APP_FRONT_CONTROLLER='.$frontController;
87+
88+
return realpath($this
89+
->getContainer()
90+
->get('kernel')
91+
->locateResource(sprintf('@WebServerBundle/Resources/router.php'))
92+
);
93+
}
94+
95+
private function guessFrontController($documentRoot, $env)
96+
{
97+
foreach (array('app', 'index') as $prefix) {
98+
$file = sprintf('%s_%s.php', $prefix, $env);
99+
if (file_exists($documentRoot.'/'.$file)) {
100+
return $file;
101+
}
102+
103+
$file = sprintf('%s.php', $prefix);
104+
if (file_exists($documentRoot.'/'.$file)) {
105+
return $file;
106+
}
107+
}
108+
109+
return false;
110+
}
65111
}

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,13 @@ protected function configure()
7777
protected function execute(InputInterface $input, OutputInterface $output)
7878
{
7979
$io = new SymfonyStyle($input, $output);
80-
$documentRoot = $input->getOption('docroot');
8180

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

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

8988
return 1;
9089
}
@@ -102,14 +101,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
102101
return 1;
103102
}
104103

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+
105110
if ('prod' === $env) {
106111
$io->error('Running PHP built-in server in production environment is NOT recommended!');
107112
}
108113

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

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

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

150155
private function createPhpProcessBuilder(SymfonyStyle $io, $address, $router, $env)
151156
{
152-
$router = $router ?: $this
153-
->getContainer()
154-
->get('kernel')
155-
->locateResource(sprintf('@FrameworkBundle/Resources/router_%s.php', $env))
156-
;
157-
158-
if (!file_exists($router)) {
159-
$io->error(sprintf('The given router script "%s" does not exist.', $router));
160-
161-
return;
162-
}
163-
164-
$router = realpath($router);
165157
$finder = new PhpExecutableFinder();
166158

167159
if (false === $binary = $finder->find()) {

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

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
9191
return 1;
9292
}
9393

94-
$documentRoot = $input->getOption('docroot');
95-
96-
if (null === $documentRoot) {
94+
if (null === $documentRoot = $input->getOption('docroot')) {
9795
$documentRoot = $this->getContainer()->getParameter('kernel.root_dir').'/../web';
9896
}
9997

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

103101
return 1;
104102
}
105103

106104
$env = $this->getContainer()->getParameter('kernel.environment');
107-
108-
if (false === $router = $this->determineRouterScript($input->getOption('router'), $env, $io)) {
109-
return 1;
110-
}
111-
112105
$address = $input->getArgument('address');
113106

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

120+ 10000
if (false === $router = $this->determineRouterScript($documentRoot, $input->getOption('router'), $env)) {
121+
$io->error('Unable to guess the front controller file.');
122+
123+
return 1;
124+
}
125+
127126
if ('prod' === $env) {
128127
$io->error('Running PHP built-in server in production environment is NOT recommended!');
129128
}
@@ -137,7 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
137136
}
138137

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

142141
return;
143142
}
@@ -174,35 +173,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
174173
}
175174
}
176175

177-
/**
178-
* Determine the absolute file path for the router script, using the environment to choose a standard script
179-
* if no custom router script is specified.
180-
*
181-
* @param string|null $router File path of the custom router script, if set by the user; otherwise null
182-
* @param string $env The application environment
183-
* @param SymfonyStyle $io An SymfonyStyle instance
184-
*
185-
* @return string|bool The absolute file path of the router script, or false on failure
186-
*/
187-
private function determineRouterScript($router, $env, SymfonyStyle $io)
188-
{
189-
if (null === $router) {
190-
$router = $this
191-
->getContainer()
192-
->get('kernel')
193-
->locateResource(sprintf('@FrameworkBundle/Resources/router_%s.php', $env))
194-
;
195-
}
196-
197-
if (false === $path = realpath($router)) {
198-
$io->error(sprintf('The given router script "%s" does not exist.', $router));
199-
200-
return false;
201-
}
202-
203-
return $path;
204-
}
205-
206176
/**
207177
* Creates a process to start PHP's built-in web server.
208178
*

src/Symfony/Bundle/WebServerBundle/Resources/router_dev.php renamed to src/Symfony/Bundle/WebServerBundle/Resources/router.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
return false;
3131
}
3232

33+
$script = getenv('APP_FRONT_CONTROLLER') ?: 'index.php';
34+
3335
$_SERVER = array_merge($_SERVER, $_ENV);
34-
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'app_dev.php';
36+
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.$script;
3537

3638
// Since we are rewriting to app_dev.php, adjust SCRIPT_NAME and PHP_SELF accordingly
37-
$_SERVER['SCRIPT_NAME'] = DIRECTORY_SEPARATOR.'app_dev.php';
38-
$_SERVER['PHP_SELF'] = DIRECTORY_SEPARATOR.'app_dev.php';
39+
$_SERVER['SCRIPT_NAME'] = DIRECTORY_SEPARATOR.$script;
40+
$_SERVER['PHP_SELF'] = DIRECTORY_SEPARATOR.$script;
3941

40-
require 'app_dev.php';
42+
require $script;
4143

4244
error_log(sprintf('%s:%d [%d]: %s', $_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_PORT'], http_response_code(), $_SERVER['REQUEST_URI']), 4);

src/Symfony/Bundle/WebServerBundle/Resources/router_prod.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Symfony/Bundle/WebServerBundle/Resources/router_test.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0