8000 [Runtime] Allow to define the env and/or debug key · symfony/symfony@2548a0a · GitHub
[go: up one dir, main page]

Skip to content

Commit 2548a0a

Browse files
maxheliasfabpot
authored andcommitted
[Runtime] Allow to define the env and/or debug key
1 parent d1fd413 commit 2548a0a

File tree

7 files changed

+49
-13
lines changed

7 files changed

+49
-13
lines changed

src/Symfony/Component/Runtime/CHANGELOG.md

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

77
* The component is not experimental anymore
8+
* Add options "env_var_names" to `GenericRuntime` and `SymfonyRuntime`
89

910
5.3.0
1011
-----

src/Symfony/Component/Runtime/GenericRuntime.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,30 @@ class GenericRuntime implements RuntimeInterface
5151
* debug?: ?bool,
5252
* runtimes?: ?array,
5353
* error_handler?: string|false,
54+
* env_var_names?: ?array,
5455
* } $options
5556
*/
5657
public function __construct(array $options = [])
5758
{
58-
$debug = $options['debug'] ?? $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? true;
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';
61+
62+
$debug = $options['debug'] ?? $_SERVER[$debugKey] ?? $_ENV[$debugKey] ?? true;
5963

6064
if (!\is_bool($debug)) {
6165
$debug = filter_var($debug, \FILTER_VALIDATE_BOOLEAN);
6266
}
6367

6468
if ($debug) {
6569
umask(0000);
66-
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '1';
70+
$_SERVER[$debugKey] = $_ENV[$debugKey] = '1';
6771

6872
if (false !== $errorHandler = ($options['error_handler'] ?? BasicErrorHandler::class)) {
6973
$errorHandler::register($debug);
7074
$options['error_handler'] = false;
7175
}
7276
} else {
73-
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0';
77+
$_SERVER[$debugKey] = $_ENV[$debugKey] = '0';
7478
}
7579

7680
$this->options = $options;
@@ -103,7 +107,7 @@ public function getResolver(callable $callable, \ReflectionFunction $reflector =
103107
return $arguments;
104108
};
105109

106-
if ($_SERVER['APP_DEBUG']) {
110+
if ($_SERVER[$this->options['env_var_names']['debug_key']]) {
107111
return new DebugClosureResolver($callable, $arguments);
108112
}
109113

@@ -135,7 +139,7 @@ public function getRunner(?object $application): RunnerInterface
135139
$application = \Closure::fromCallable($application);
136140
}
137141

138-
if ($_SERVER['APP_DEBUG'] && ($r = new \ReflectionFunction($application)) && $r->getNumberOfRequiredParameters()) {
142+
if ($_SERVER[$this->options['env_var_names']['debug_key']] && ($r = new \ReflectionFunction($application)) && $r->getNumberOfRequiredParameters()) {
139143
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()));
140144
}
141145

src/Symfony/Component/Runtime/SymfonyRuntime.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,30 @@ class SymfonyRuntime extends GenericRuntime
8282
* use_putenv?: ?bool,
8383
* runtimes?: ?array,
8484
* error_handler?: string|false,
85+
* env_var_names?: ?array,
8586
* } $options
8687
*/
8788
public function __construct(array $options = [])
8889
{
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';
92+
8993
if (isset($options['env'])) {
90-
$_SERVER['APP_ENV'] = $options['env'];
94+
$_SERVER[$envKey] = $options['env'];
9195
} elseif (isset($_SERVER['argv']) && class_exists(ArgvInput::class)) {
9296
$this->options = $options;
9397
$this->getInput();
9498
}
9599

96100
if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) {
97-
(new Dotenv())
101+
(new Dotenv($envKey, $debugKey))
98102
->setProdEnvs((array) ($options['prod_envs'] ?? ['prod']))
99103
->usePutenv($options['use_putenv'] ?? false)
100104
->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test']));
101-
$options['debug'] ?? $options['debug'] = '1' === $_SERVER['APP_DEBUG'];
105+
$options['debug'] ?? $options['debug'] = '1' === $_SERVER[$debugKey];
102106
$options['disable_dotenv'] = true;
103107
} else {
104-
$_SERVER['APP_ENV'] ?? $_SERVER['APP_ENV'] = 'dev';
108+
$_SERVER[$envKey] ?? $_SERVER[$envKey] = 'dev';
105109
}
106110

107111
$options['error_handler'] ?? $options['error_handler'] = SymfonyErrorHandler::class;
@@ -140,7 +144,7 @@ public function getRunner(?object $application): RunnerInterface
140144
}
141145

142146
set_time_limit(0);
143-
$defaultEnv = !isset($this->options['env']) ? ($_SERVER['APP_ENV'] ?? 'dev') : null;
147+
$defaultEnv = !isset($this->options['env']) ? ($_SERVER[$this->options['env_var_names']['env_key']] ?? 'dev') : null;
144148
$output = $this->output ?? $this->output = new ConsoleOutput();
145149

146150
return new ConsoleApplicationRunner($application, $defaultEnv, $this->getInput(), $output);
@@ -208,11 +212,11 @@ private function getInput(): ArgvInput
208212
}
209213

210214
if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) {
211-
putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env);
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);
212216
}
213217

214218
if ($input->hasParameterOption('--no-debug', true)) {
215-
putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
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');
216220
}
217221

218222
return $this->input = $input;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
SOME_VAR=foo_bar
2+
ENV_MODE=foo
3+
DEBUG_MODE=0

src/Symfony/Component/Runtime/Tests/phpt/autoload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
$_SERVER['APP_RUNTIME_OPTIONS'] = [
66
'project_dir' => __DIR__,
7-
];
7+
] + ($_SERVER['APP_RUNTIME_OPTIONS'] ?? []);
88

99
if (file_exists(dirname(__DIR__, 2).'/vendor/autoload.php')) {
1010
if (true === (require_once dirname(__DIR__, 2).'/vendor/autoload.php') || empty($_SERVER['SCRIPT_FILENAME'])) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$_SERVER['APP_RUNTIME_OPTIONS'] = [
4+
'env_var_names' => [
5+
'env_key' => 'ENV_MODE',
6+
'debug_key' => 'DEBUG_MODE',
7+
],
8+
];
9+
require __DIR__.'/autoload.php';
10+
11+
return function (array $context): void {
12+
echo 'Env mode ', $context['ENV_MODE'], ', debug mode ', $context['DEBUG_MODE'];
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test Options
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime-options.php';
9+
10+
?>
11+
--EXPECTF--
12+
Env mode foo, debug mode 0

0 commit comments

Comments
 (0)
0