diff --git a/src/Symfony/Component/Runtime/CHANGELOG.md b/src/Symfony/Component/Runtime/CHANGELOG.md index 3f5d90c8db902..b3bb7d8b823d1 100644 --- a/src/Symfony/Component/Runtime/CHANGELOG.md +++ b/src/Symfony/Component/Runtime/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.4 +--- + +* Add the environnement variable `APP_RUNTIME_MODE` + 5.4 --- diff --git a/src/Symfony/Component/Runtime/Internal/autoload_runtime.template b/src/Symfony/Component/Runtime/Internal/autoload_runtime.template index 68af945932edf..99f8c67314377 100644 --- a/src/Symfony/Component/Runtime/Internal/autoload_runtime.template +++ b/src/Symfony/Component/Runtime/Internal/autoload_runtime.template @@ -12,6 +12,16 @@ if (!is_object($app)) { throw new TypeError(sprintf('Invalid return value: callable object expected, "%s" returned from "%s".', get_debug_type($app), $_SERVER['SCRIPT_FILENAME'])); } +if (null === ($_ENV['APP_RUNTIME_MODE'] ??= $_SERVER['APP_RUNTIME_MODE'] ?? null)) { + if ($_ENV['FRANKENPHP_WORKER'] ?? $_SERVER['FRANKENPHP_WORKER'] ?? false) { + $_ENV['APP_RUNTIME_MODE'] = 'worker'; + } elseif (\PHP_SAPI === 'cli' || \PHP_SAPI === 'phpdbg') { + $_ENV['APP_RUNTIME_MODE'] = 'cli'; + } else { + $_ENV['APP_RUNTIME_MODE'] = 'web'; + } +} + $runtime = $_SERVER['APP_RUNTIME'] ?? $_ENV['APP_RUNTIME'] ?? %runtime_class%; $runtime = new $runtime(($_SERVER['APP_RUNTIME_OPTIONS'] ?? $_ENV['APP_RUNTIME_OPTIONS'] ?? []) + %runtime_options%); diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_default.php b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_default.php new file mode 100644 index 0000000000000..41ce32f9f651e --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_default.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require __DIR__.'/autoload.php'; + +return function (array $context): void { + echo 'From context ', $context['APP_RUNTIME_MODE'], ', from $_ENV ', $_ENV['APP_RUNTIME_MODE']; +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_default.php.phpt b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_default.php.phpt new file mode 100644 index 0000000000000..f47e9aaa60119 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_default.php.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test set ENV variable APP_RUNTIME_MODE by guessing it from PHP_SAPI +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +From context cli, from $_ENV cli diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env.php b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env.php new file mode 100644 index 0000000000000..8868fa5e78ba3 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +$_ENV['APP_RUNTIME_MODE'] = 'env'; + +require __DIR__.'/autoload.php'; + +return function (array $context): void { + echo 'From context ', $context['APP_RUNTIME_MODE'], ', from $_ENV ', $_ENV['APP_RUNTIME_MODE']; +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env.php.phpt b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env.php.phpt new file mode 100644 index 0000000000000..3778c0719a1dc --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env.php.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test set ENV variable APP_RUNTIME_MODE from $_ENV +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +From context env, from $_ENV env diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env_with_server_and_frankenphp_set.php b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env_with_server_and_frankenphp_set.php new file mode 100644 index 0000000000000..31397bd993fd1 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env_with_server_and_frankenphp_set.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +$_SERVER['APP_RUNTIME_MODE'] = 'server'; +$_ENV['APP_RUNTIME_MODE'] = 'env'; +$_ENV['FRANKENPHP_WORKER'] = '1'; +$_SERVER['FRANKENPHP_WORKER'] = '1'; + +require __DIR__.'/autoload.php'; + +return function (array $context): void { + echo 'From context ', $context['APP_RUNTIME_MODE'], ', from $_ENV ', $_ENV['APP_RUNTIME_MODE']; +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env_with_server_and_frankenphp_set.php.phpt b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env_with_server_and_frankenphp_set.php.phpt new file mode 100644 index 0000000000000..c5c02da4d35a6 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env_with_server_and_frankenphp_set.php.phpt @@ -0,0 +1,13 @@ +--TEST-- +Test set ENV variable APP_RUNTIME_MODE from $_ENV with variable also set in $_SERVER and FRANKENPHP_WORKER +also set in $_ENV and $_SERVER +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +From context server, from $_ENV env diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_env_var.php b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_env_var.php new file mode 100644 index 0000000000000..a252eb4b5f177 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_env_var.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +$_ENV['FRANKENPHP_WORKER'] = '1'; + +require __DIR__.'/autoload.php'; + +return function (array $context): void { + echo 'From context ', $context['APP_RUNTIME_MODE'], ', from $_ENV ', $_ENV['APP_RUNTIME_MODE']; +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_env_var.php.phpt b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_env_var.php.phpt new file mode 100644 index 0000000000000..34e4c12a48042 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_env_var.php.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test set ENV variable APP_RUNTIME_MODE with FRANKENPHP_WORKER $_ENV variable set +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +From context worker, from $_ENV worker diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_server_var.php b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_server_var.php new file mode 100644 index 0000000000000..a6942d586c47c --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_server_var.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +$_SERVER['FRANKENPHP_WORKER'] = '1'; + +require __DIR__.'/autoload.php'; + +return function (array $context): void { + echo 'From context ', $context['APP_RUNTIME_MODE'], ', from $_ENV ', $_ENV['APP_RUNTIME_MODE']; +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_server_var.php.phpt b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_server_var.php.phpt new file mode 100644 index 0000000000000..85bdc6cffabc0 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_frankenphp_server_var.php.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test set ENV variable APP_RUNTIME_MODE with FRANKENPHP_WORKER $_SERVER variable set +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +From context worker, from $_ENV worker diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_server.php b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_server.php new file mode 100644 index 0000000000000..69b0c91883f24 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_server.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +$_SERVER['APP_RUNTIME_MODE'] = 'server'; + +require __DIR__.'/autoload.php'; + +return function (array $context): void { + echo 'From context ', $context['APP_RUNTIME_MODE'], ', from $_ENV ', $_ENV['APP_RUNTIME_MODE']; +}; diff --git a/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_server.php.phpt b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_server.php.phpt new file mode 100644 index 0000000000000..5f987e8bfeed3 --- /dev/null +++ b/src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_server.php.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test set ENV variable APP_RUNTIME_MODE from $_SERVER +--INI-- +display_errors=1 +--FILE-- + +--EXPECTF-- +From context server, from $_ENV server
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: