Page MenuHomeDevCentral

Read from environment when PHP doesn't populate $_ENV
ClosedPublic

Authored by dereckson on Feb 5 2022, 04:03.
Tags
None
Referenced Files
F3928672: D2497.diff
Sun, Dec 22, 05:50
Unknown Object (File)
Thu, Dec 19, 05:40
Unknown Object (File)
Thu, Dec 19, 05:40
Unknown Object (File)
Thu, Dec 19, 02:59
Unknown Object (File)
Thu, Dec 19, 02:43
Unknown Object (File)
Thu, Dec 19, 02:21
Unknown Object (File)
Mon, Dec 16, 12:42
Unknown Object (File)
Thu, Dec 12, 09:09
Subscribers
None

Details

Summary

The Environment class provides a standard way to query both
$_ENV and $_SERVER. Advantage against getenv() is it doesn't
return false if the value doesn't exist, but throw an exception.

The getOr method allows to provide a default value,
so $_ENV['foo'] ?? $_SERVER['foo'] ?? default can be
expressed a more compact way.

This is intended to allow applications to fetch environment
and be sure to get the environment variable as an answer.

$_ENV and $_SERVER for environment

PHP allows to control the order of superglobals array,
like $_SERVER and $_ENV, but also to restrain to set one.

When software is configured through environment variables,
this situation creates a tricky situation:

  • The default PHP value is EGPCS, so we've $_ENV
  • The suggested production config is GPCS, without $_ENV

By default, Fedora and Ubuntu PHP packages maintainers
set the variables_order configuration variable to GPCS,
not filling the $_ENV array.

Meanwhile, any variable defined through php-fpm environment
will be filled in $_SERVER. $_SERVER would also receive the
php-fpm process environment if clean_env is set to false.

As far as a generic application is concerned, this situation
means any environment variable could be set in $_ENV, $_SERVER
or both. In PHP core, the getenv() method read both arrays.

References

Test Plan

Units tests covering all methods.

Diff Detail

Repository
rKOT Keruald OmniTools
Lint
Lint Passed
Unit
Tests Skipped
Branch
main
Build Status
Buildable 3889
Build 4140: arc lint + arc unit

Event Timeline

dereckson created this revision.
dereckson retitled this revision from WIP: Environment to WIP: EGPCS vs GPCS for environment.Feb 5 2022, 04:13
dereckson edited the summary of this revision. (Show Details)
dereckson edited the test plan for this revision. (Show Details)
dereckson edited the summary of this revision. (Show Details)
This revision is now accepted and ready to land.Feb 5 2022, 11:25

First adopter will be Zed configuration:

-define('CONTENT_DIR', $_ENV['CONTENT_DIR'] ?? 'content');
+define('CONTENT_DIR', Environment::getOr('CONTENT_DIR', 'content'));

-define('CACHE_DIR', $_ENV['CACHE_DIR'] ?? 'cache');
+define('CACHE_DIR', Environment::getOr('CACHE_DIR', 'cache'));

Those defines will be migrated later as regular configuration variables,
all stored in a configuration array, allowing a syntax like this one:

[
    'paths' => [
        'content' => Environment::getOr('CONTENT_DIR', 'content'),
        'cache' => Environment::getOr('CACHE_DIR', 'cache'),
    ],
    // ...
]
This revision was automatically updated to reflect the committed changes.
dereckson retitled this revision from WIP: EGPCS vs GPCS for environment to Read from environment when PHP doesn't populate $_ENV.Feb 5 2022, 12:29