From 2548a0af365cd3481ca77e8b72574c1c2f9ba69e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20H=C3=A9lias?= Date: Tue, 8 Jun 2021 11:17:09 +0200 Subject: [PATCH] [Runtime] Allow to define the env and/or debug key --- src/Symfony/Component/Runtime/CHANGELOG.md | 1 + .../Component/Runtime/GenericRuntime.php | 14 +++++++++----- .../Component/Runtime/SymfonyRuntime.php | 18 +++++++++++------- src/Symfony/Component/Runtime/Tests/phpt/.env | 2 ++ .../Component/Runtime/Tests/phpt/autoload.php | 2 +- .../Runtime/Tests/phpt/runtime-options.php | 13 +++++++++++++ .../Runtime/Tests/phpt/runtime-options.phpt | 12 ++++++++++++ 7 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 src/Symfony/Component/Runtime/Tests/phpt/runtime-options.php create mode 100644 src/Symfony/Component/Runtime/Tests/phpt/runtime-options.phpt diff --git a/src/Symfony/Component/Runtime/CHANGELOG.md b/src/Symfony/Component/Runtime/CHANGELOG.md index cc24c38681fae..a705a84234f35 100644 --- a/src/Symfony/Component/Runtime/CHANGELOG.md +++ b/src/Symfony/Component/Runtime/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * The component is not experimental anymore + * Add options "env_var_names" to `GenericRuntime` and `SymfonyRuntime` 5.3.0 ----- diff --git a/src/Symfony/Component/Runtime/GenericRuntime.php b/src/Symfony/Component/Runtime/GenericRuntime.php index 85063f58e3187..03c8c51067069 100644 --- a/src/Symfony/Component/Runtime/GenericRuntime.php +++ b/src/Symfony/Component/Runtime/GenericRuntime.php @@ -51,11 +51,15 @@ class GenericRuntime implements RuntimeInterface * debug?: ?bool, * runtimes?: ?array, * error_handler?: string|false, + * env_var_names?: ?array, * } $options */ public function __construct(array $options = []) { - $debug = $options['debug'] ?? $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? true; + $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'; + + $debug = $options['debug'] ?? $_SERVER[$debugKey] ?? $_ENV[$debugKey] ?? true; if (!\is_bool($debug)) { $debug = filter_var($debug, \FILTER_VALIDATE_BOOLEAN); @@ -63,14 +67,14 @@ public function __construct(array $options = []) if ($debug) { umask(0000); - $_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '1'; + $_SERVER[$debugKey] = $_ENV[$debugKey] = '1'; if (false !== $errorHandler = ($options['error_handler'] ?? BasicErrorHandler::class)) { $errorHandler::register($debug); $options['error_handler'] = false; } } else { - $_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0'; + $_SERVER[$debugKey] = $_ENV[$debugKey] = '0'; } $this->options = $options; @@ -103,7 +107,7 @@ public function getResolver(callable $callable, \ReflectionFunction $reflector = return $arguments; }; - if ($_SERVER['APP_DEBUG']) { + if ($_SERVER[$this->options['env_var_names']['debug_key']]) { return new DebugClosureResolver($callable, $arguments); } @@ -135,7 +139,7 @@ public function getRunner(?object $application): RunnerInterface $application = \Closure::fromCallable($application); } - if ($_SERVER['APP_DEBUG'] && ($r = new \ReflectionFunction($application)) && $r->getNumberOfRequiredParameters()) { + if ($_SERVER[$this->options['env_var_names']['debug_key']] && ($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())); } diff --git a/src/Symfony/Component/Runtime/SymfonyRuntime.php b/src/Symfony/Component/Runtime/SymfonyRuntime.php index e15560bf02a17..3ef4fd65f200d 100644 --- a/src/Symfony/Component/Runtime/SymfonyRuntime.php +++ b/src/Symfony/Component/Runtime/SymfonyRuntime.php @@ -82,26 +82,30 @@ class SymfonyRuntime extends GenericRuntime * use_putenv?: ?bool, * runtimes?: ?array, * error_handler?: string|false, + * env_var_names?: ?array, * } $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'; + if (isset($options['env'])) { - $_SERVER['APP_ENV'] = $options['env']; + $_SERVER[$envKey] = $options['env']; } elseif (isset($_SERVER['argv']) && class_exists(ArgvInput::class)) { $this->options = $options; $this->getInput(); } if (!($options['disable_dotenv'] ?? false) && isset($options['project_dir']) && !class_exists(MissingDotenv::class, false)) { - (new Dotenv()) + (new Dotenv($envKey, $debugKey)) ->setProdEnvs((array) ($options['prod_envs'] ?? ['prod'])) ->usePutenv($options['use_putenv'] ?? false) ->bootEnv($options['project_dir'].'/'.($options['dotenv_path'] ?? '.env'), 'dev', (array) ($options['test_envs'] ?? ['test'])); - $options['debug'] ?? $options['debug'] = '1' === $_SERVER['APP_DEBUG']; + $options['debug'] ?? $options['debug'] = '1' === $_SERVER[$debugKey]; $options['disable_dotenv'] = true; } else { - $_SERVER['APP_ENV'] ?? $_SERVER['APP_ENV'] = 'dev'; + $_SERVER[$envKey] ?? $_SERVER[$envKey] = 'dev'; } $options['error_handler'] ?? $options['error_handler'] = SymfonyErrorHandler::class; @@ -140,7 +144,7 @@ public function getRunner(?object $application): RunnerInterface } set_time_limit(0); - $defaultEnv = !isset($this->options['env']) ? ($_SERVER['APP_ENV'] ?? 'dev') : null; + $defaultEnv = !isset($this->options['env']) ? ($_SERVER[$this->options['env_var_names']['env_key']] ?? 'dev') : null; $output = $this->output ?? $this->output = new ConsoleOutput(); return new ConsoleApplicationRunner($application, $defaultEnv, $this->getInput(), $output); @@ -208,11 +212,11 @@ private function getInput(): ArgvInput } if (null !== $env = $input->getParameterOption(['--env', '-e'], null, true)) { - putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = $env); + 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); } if ($input->hasParameterOption('--no-debug', true)) { - putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0'); + 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'); } return $this->input = $input; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/.env b/src/Symfony/Component/Runtime/Tests/phpt/.env index 1853ef1741a1c..9fd6ab5426972 100644 --- a/src/Symfony/Component/Runtime/Tests/phpt/.env +++ b/src/Symfony/Component/Runtime/Tests/phpt/.env @@ -1 +1,3 @@ SOME_VAR=foo_bar +ENV_MODE=foo +DEBUG_MODE=0 diff --git a/src/Symfony/Component/Runtime/Tests/phpt/autoload.php b/src/Symfony/Component/Runtime/Tests/phpt/autoload.php index 78036ad2c494c..300e6bcb1a686 100644 --- a/src/Symfony/Component/Runtime/Tests/phpt/autoload.php +++ b/src/Symfony/Component/Runtime/Tests/phpt/autoload.php @@ -4,7 +4,7 @@ $_SERVER['APP_RUNTIME_OPTIONS'] = [ 'project_dir' => __DIR__, -]; +] + ($_SERVER['APP_RUNTIME_OPTIONS'] ?? []); if (file_exists(dirname(__DIR__, 2).'/vendor/autoload.php')) { if (true === (require_once dirname(__DIR__, 2).'/vendor/autoload.php') || empty($_SERVER['SCRIPT_FILENAME'])) { diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime-options.php b/src/Symfony/Component/Runtime/Tests/phpt/runtime-options.php new file mode 100644 index 0000000000000..60a73cb0ea69d --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime-options.php @@ -0,0 +1,13 @@ + [ + 'env_key' => 'ENV_MODE', + 'debug_key' => 'DEBUG_MODE', + ], +]; +require __DIR__.'/autoload.php'; + +return function (array $context): void { + echo 'Env mode ', $context['ENV_MODE'], ', debug mode ', $context['DEBUG_MODE']; +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime-options.phpt b/src/Symfony/Component/Runtime/Tests/phpt/runtime-options.phpt new file mode 100644 index 0000000000000..f0ee02d1b741c --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime-options.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test Options +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +Env mode foo, debug mode 0