Skip to content

Commit f56b471

Browse files
committed
minor #43201 [Runtime] tweak config for env var names (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- [Runtime] tweak config for env var names | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Tweaking #41608 Commits ------- c4ef4d7 [Runtime] tweak config for env var names
2 parents 8653b33 + c4ef4d7 commit f56b471

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/Symfony/Component/Runtime/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CHANGELOG
55
---
66

77
* The component is not experimental anymore
8-
* Add options "env_var_names" to `GenericRuntime` and `SymfonyRuntime`
8+
* Add options "env_var_name" and "debug_var_name" to `GenericRuntime` and `SymfonyRuntime`
99

1010
5.3.0
1111
-----

src/Symfony/Component/Runtime/GenericRuntime.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ class_exists(ClosureResolver::class);
2727
* to the "APP_DEBUG" environment variable;
2828
* - "runtimes" maps types to a GenericRuntime implementation
2929
* that knows how to deal with each of them;
30-
* - "error_handler" defines the class to use to handle PHP errors.
30+
* - "error_handler" defines the class to use to handle PHP errors;
31+
* - "env_var_name" and "debug_var_name" define the name of the env
32+
* vars that hold the Symfony env and the debug flag respectively.
3133
*
3234
* The app-callable can declare arguments among either:
3335
* - "array $context" to get a local array similar to $_SERVER;
@@ -51,13 +53,14 @@ class GenericRuntime implements RuntimeInterface
5153
* debug?: ?bool,
5254
* runtimes?: ?array,
5355
* error_handler?: string|false,
54-
* env_var_names?: ?array,
56+
* env_var_name?: string,
57+
* debug_var_name?: string,
5558
* } $options
5659
*/
5760
public function __construct(array $options = [])
5861
{
59-
$options['env_var_names']['env_key'] ?? $options['env_var_names']['env_key'] = 'APP_ENV';
60-
$debugKey = $options['env_var_names']['debug_key'] ?? $options['env_var_names']['debug_key'] = 'APP_DEBUG';
62+
$options['env_var_name'] ?? $options['env_var_name'] = 'APP_ENV';
63+
$debugKey = $options['debug_var_name'] ?? $options['debug_var_name'] = 'APP_DEBUG';
6164

6265
$debug = $options['debug'] ?? $_SERVER[$debugKey] ?? $_ENV[$debugKey] ?? true;
6366

@@ -107,7 +110,7 @@ public function getResolver(callable $callable, \ReflectionFunction $reflector =
107110
return $arguments;
108111
};
109112

110-
if ($_SERVER[$this->options['env_var_names']['debug_key']]) {
113+
if ($_SERVER[$this->options['debug_var_name']]) {
111114
return new DebugClosureResolver($callable, $arguments);
112115
}
113116

@@ -139,7 +142,7 @@ public function getRunner(?object $application): RunnerInterface
139142
$application = \Closure::fromCallable($application);
140143
}
141144

142-
if ($_SERVER[$this->options['env_var_names']['debug_key']] && ($r = new \ReflectionFunction($application)) && $r->getNumberOfRequiredParameters()) {
145+
if ($_SERVER[$this->options['debug_var_name']] && ($r = new \ReflectionFunction($application)) && $r->getNumberOfRequiredParameters()) {
143146
throw new \ArgumentCountError(sprintf('Zero argument should be required by the runner callable, but at least one is in "%s" on line "%d.', $r->getFileName(), $r->getStartLine()));
144147
}
145148

src/Symfony/Component/Runtime/SymfonyRuntime.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ class SymfonyRuntime extends GenericRuntime
8282
* use_putenv?: ?bool,
8383
* runtimes?: ?array,
8484
* error_handler?: string|false,
85-
* env_var_names?: ?array,
85+
* env_var_name?: string,
86+
* debug_var_name?: string,
8687
* } $options
8788
*/
8889
public function __construct(array $options = [])
8990
{
90-
$envKey = $options['env_var_names']['env_key'] ?? $options['env_var_names']['env_key'] = 'APP_ENV';
91-
$debugKey = $options['env_var_names']['debug_key'] ?? $options['env_var_names']['debug_key'] = 'APP_DEBUG';
91+
$envKey = $options['env_var_name'] ?? $options['env_var_name'] = 'APP_ENV';
92+
$debugKey = $options['debug_var_name'] ?? $options['debug_var_name'] = 'APP_DEBUG';
9293

9394
if (isset($options['env'])) {
9495
$_SERVER[$envKey] = $options['env'];
@@ -144,7 +145,7 @@ public function getRunner(?object $application): RunnerInterface
144145
}
145146

146147
set_time_limit(0);
147-
$defaultEnv = !isset($this->options['env']) ? ($_SERVER[$this->options['env_var_names']['env_key']] ?? 'dev') : null;
148+
$defaultEnv = !isset($this->options['env']) ? ($_SERVER[$this->options['env_var_name']] ?? 'dev') : null;
148149
$output = $this->output ?? $this->output = new ConsoleOutput();
149150

150151
return new ConsoleApplicationRunner($application, $defaultEnv, $this->getInput(), $output);
@@ -212,11 +213,11 @@ private function getInput(): ArgvInput
212213
}
213214

214215
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
215-
putenv($this->options['env_var_names']['env_key'].'='.$_SERVER[$this->options['env_var_names']['env_key']] = $_ENV[$this->options['env_var_names']['env_key']] = $env);
216+
putenv($this->options['env_var_name'].'='.$_SERVER[$this->options['env_var_name']] = $_ENV[$this->options['env_var_name']] = $env);
216217
}
217218

218219
if ($input->hasParameterOption('--no-debug', true)) {
219-
putenv($this->options['env_var_names']['debug_key'].'='.$_SERVER[$this->options['env_var_names']['debug_key']] = $_ENV[$this->options['env_var_names']['debug_key']] = '0');
220+
putenv($this->options['debug_var_name'].'='.$_SERVER[$this->options['debug_var_name']] = $_ENV[$this->options['debug_var_name']] = '0');
220221
}
221222

222223
return $this->input = $input;

0 commit comments

Comments
 (0)