Skip to content

[Runtime] tweak config for env var names #43201

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 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
---

* The component is not experimental anymore
* Add options "env_var_names" to `GenericRuntime` and `SymfonyRuntime`
* Add options "env_var_name" and "debug_var_name" to `GenericRuntime` and `SymfonyRuntime`

5.3.0
-----
Expand Down
15 changes: 9 additions & 6 deletions src/Symfony/Component/Runtime/GenericRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class_exists(ClosureResolver::class);
* to the "APP_DEBUG" environment variable;
* - "runtimes" maps types to a GenericRuntime implementation
* that knows how to deal with each of them;
* - "error_handler" defines the class to use to handle PHP errors.
* - "error_handler" defines the class to use to handle PHP errors;
* - "env_var_name" and "debug_var_name" define the name of the env
* vars that hold the Symfony env and the debug flag respectively.
*
* The app-callable can declare arguments among either:
* - "array $context" to get a local array similar to $_SERVER;
Expand All @@ -51,13 +53,14 @@ class GenericRuntime implements RuntimeInterface
* debug?: ?bool,
* runtimes?: ?array,
* error_handler?: string|false,
* env_var_names?: ?array,
* env_var_name?: string,
* debug_var_name?: string,
* } $options
*/
public function __construct(array $options = [])
{
$options['env_var_names']['env_key'] ?? $options['env_var_names']['env_key'] = 'APP_ENV';
$debugKey = $options['env_var_names']['debug_key'] ?? $options['env_var_names']['debug_key'] = 'APP_DEBUG';
$options['env_var_name'] ?? $options['env_var_name'] = 'APP_ENV';
$debugKey = $options['debug_var_name'] ?? $options['debug_var_name'] = 'APP_DEBUG';

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

Expand Down Expand Up @@ -107,7 +110,7 @@ public function getResolver(callable $callable, \ReflectionFunction $reflector =
return $arguments;
};

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

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

if ($_SERVER[$this->options['env_var_names']['debug_key']] && ($r = new \ReflectionFunction($application)) && $r->getNumberOfRequiredParameters()) {
if ($_SERVER[$this->options['debug_var_name']] && ($r = new \ReflectionFunction($application)) && $r->getNumberOfRequiredParameters()) {
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()));
}

Expand Down
13 changes: 7 additions & 6 deletions src/Symfony/Component/Runtime/SymfonyRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ class SymfonyRuntime extends GenericRuntime
* use_putenv?: ?bool,
* runtimes?: ?array,
* error_handler?: string|false,
* env_var_names?: ?array,
* env_var_name?: string,
* debug_var_name?: string,
* } $options
*/
public function __construct(array $options = [])
{
$envKey = $options['env_var_names']['env_key'] ?? $options['env_var_names']['env_key'] = 'APP_ENV';
$debugKey = $options['env_var_names']['debug_key'] ?? $options['env_var_names']['debug_key'] = 'APP_DEBUG';
$envKey = $options['env_var_name'] ?? $options['env_var_name'] = 'APP_ENV';
$debugKey = $options['debug_var_name'] ?? $options['debug_var_name'] = 'APP_DEBUG';

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

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

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

if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
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);
putenv($this->options['env_var_name'].'='.$_SERVER[$this->options['env_var_name']] = $_ENV[$this->options['env_var_name']] = $env);
}

if ($input->hasParameterOption('--no-debug', true)) {
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');
putenv($this->options['debug_var_name'].'='.$_SERVER[$this->options['debug_var_name']] = $_ENV[$this->options['debug_var_name']] = '0');
}

return $this->input = $input;
Expand Down