Skip to content

[Runtime] Add environment variable APP_RUNTIME_MODE #51408

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

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

6.4
---

* Add the environnement variable `APP_RUNTIME_MODE`

5.4
---

Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Runtime/Internal/autoload_runtime.template
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need to have this specific check? Wouldn't that be something to put in FrankenPHP instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do that also for CLI and SAPI. The main benefit of doing it here is to not have to duplicate this code in every runtime supporting FrankenPHP worker mode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting APP_RUNTIME_MODE in the bridge would be a better demonstration of how this have to be configured. Otherwise we'll receive PR to add Bref or React support in this file.

$_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%);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test set ENV variable APP_RUNTIME_MODE by guessing it from PHP_SAPI
--INI--
display_errors=1
--FILE--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime_mode_from_default.php';

?>
--EXPECTF--
From context cli, from $_ENV cli
18 changes: 18 additions & 0 deletions src/Symfony/Component/Runtime/Tests/phpt/runtime_mode_from_env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test set ENV variable APP_RUNTIME_MODE from $_ENV
--INI--
display_errors=1
--FILE--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime_mode_from_env.php';

?>
--EXPECTF--
From context env, from $_ENV env
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'];
};
Original file line number Diff line number Diff line change
@@ -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--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime_mode_from_env_with_server_and_frankenphp_set.php';

?>
--EXPECTF--
From context server, from $_ENV env
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test set ENV variable APP_RUNTIME_MODE with FRANKENPHP_WORKER $_ENV variable set
--INI--
display_errors=1
--FILE--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime_mode_from_frankenphp_env_var.php';

?>
--EXPECTF--
From context worker, from $_ENV worker
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test set ENV variable APP_RUNTIME_MODE with FRANKENPHP_WORKER $_SERVER variable set
--INI--
display_errors=1
--FILE--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime_mode_from_frankenphp_server_var.php';

?>
--EXPECTF--
From context worker, from $_ENV worker
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test set ENV variable APP_RUNTIME_MODE from $_SERVER
--INI--
display_errors=1
--FILE--
<?php

require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime_mode_from_server.php';

?>
--EXPECTF--
From context server, from $_ENV server