Skip to content

[Runtime] Possibility to define the env and/or debug key #41608

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
1 change: 1 addition & 0 deletions src/Symfony/Component/Runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

5.3.0
-----
Expand Down
14 changes: 9 additions & 5 deletions src/Symfony/Component/Runtime/GenericRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,30 @@ 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);
}

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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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()));
}

Expand Down
18 changes: 11 additions & 7 deletions src/Symfony/Component/Runtime/SymfonyRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Runtime/Tests/phpt/.env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
SOME_VAR=foo_bar
ENV_MODE=foo
DEBUG_MODE=0
2 changes: 1 addition & 1 deletion src/Symfony/Component/Runtime/Tests/phpt/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Runtime/Tests/phpt/runtime-options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$_SERVER['APP_RUNTIME_OPTIONS'] = [
'env_var_names' => [
'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'];
};
12 changes: 12 additions & 0 deletions src/Symfony/Component/Runtime/Tests/phpt/runtime-options.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test Options
--INI--
display_errors=1
--FILE--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime-options.php';

?>
--EXPECTF--
Env mode foo, debug mode 0