Skip to content

Commit b491d0d

Browse files
[Runtime] Add environment variable APP_RUNTIME_MODE
1 parent c2ac73c commit b491d0d

14 files changed

+197
-0
lines changed

src/Symfony/Component/Runtime/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Add the environnement variable `APP_RUNTIME_MODE`
8+
49
5.4
510
---
611

src/Symfony/Component/Runtime/Internal/autoload_runtime.template

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ if (!is_object($app)) {
1212
throw new TypeError(sprintf('Invalid return value: callable object expected, "%s" returned from "%s".', get_debug_type($app), $_SERVER['SCRIPT_FILENAME']));
1313
}
1414

15+
if (null === ($_ENV['APP_RUNTIME_MODE'] ??= $_SERVER['APP_RUNTIME_MODE'] ?? null)) {
16+
if ($_ENV['FRANKENPHP_WORKER'] ?? $_SERVER['FRANKENPHP_WORKER'] ?? false) {
17+
$_ENV['APP_RUNTIME_MODE'] = 'worker';
18+
} elseif (\PHP_SAPI === 'cli' || \PHP_SAPI === 'phpdbg') {
19+
$_ENV['APP_RUNTIME_MODE'] = 'cli';
20+
} else {
21+
$_ENV['APP_RUNTIME_MODE'] = 'web';
22+
}
23+
}
24+
1525
$runtime = $_SERVER['APP_RUNTIME'] ?? $_ENV['APP_RUNTIME'] ?? %runtime_class%;
1626
$runtime = new $runtime(($_SERVER['APP_RUNTIME_OPTIONS'] ?? $_ENV['APP_RUNTIME_OPTIONS'] ?? []) + %runtime_options%);
1727

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
require __DIR__.'/autoload.php';
13+
14+
return function (array $context): void {
15+
echo 'From context ', $context['APP_RUNTIME_MODE'], ', from $_ENV ', $_ENV['APP_RUNTIME_MODE'];
16+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test set ENV variable APP_RUNTIME_MODE by guessing it from PHP_SAPI
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime_mode_from_default.php';
9+
10+
?>
11+
--EXPECTF--
12+
From context cli, from $_ENV cli
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
$_ENV['APP_RUNTIME_MODE'] = 'env';
13+
14+
require __DIR__.'/autoload.php';
15+
16+
return function (array $context): void {
17+
echo 'From context ', $context['APP_RUNTIME_MODE'], ', from $_ENV ', $_ENV['APP_RUNTIME_MODE'];
18+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test set ENV variable APP_RUNTIME_MODE from $_ENV
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime_mode_from_env.php';
9+
10+
?>
11+
--EXPECTF--
12+
From context env, from $_ENV env
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
$_SERVER['APP_RUNTIME_MODE'] = 'server';
13+
$_ENV['APP_RUNTIME_MODE'] = 'env';
14+
$_ENV['FRANKENPHP_WORKER'] = '1';
15+
$_SERVER['FRANKENPHP_WORKER'] = '1';
16+
17+
require __DIR__.'/autoload.php';
18+
19+
return function (array $context): void {
20+
echo 'From context ', $context['APP_RUNTIME_MODE'], ', from $_ENV ', $_ENV['APP_RUNTIME_MODE'];
21+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Test set ENV variable APP_RUNTIME_MODE from $_ENV with variable also set in $_SERVER and FRANKENPHP_WORKER
3+
also set in $_ENV and $_SERVER
4+
--INI--
5+
display_errors=1
6+
--FILE--
7+
<?php
8+
9+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime_mode_from_env_with_server_and_frankenphp_set.php';
10+
11+
?>
12+
--EXPECTF--
13+
From context server, from $_ENV env
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
$_ENV['FRANKENPHP_WORKER'] = '1';
13+
14+
require __DIR__.'/autoload.php';
15+
16+
return function (array $context): void {
17+
echo 'From context ', $context['APP_RUNTIME_MODE'], ', from $_ENV ', $_ENV['APP_RUNTIME_MODE'];
18+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Test set ENV variable APP_RUNTIME_MODE with FRANKENPHP_WORKER $_ENV variable set
3+
--INI--
4+
display_errors=1
5+
--FILE--
6+
<?php
7+
8+
require $_SERVER['SCRIPT_FILENAME'] = __DIR__.'/runtime_mode_from_frankenphp_env_var.php';
9+
10+
?>
11+
--EXPECTF--
12+
From context worker, from $_ENV worker

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy