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
Unknown Object (File)
Fri, Nov 15, 07:46
Unknown Object (File)
Thu, Nov 14, 03:04
Unknown Object (File)
Tue, Nov 12, 07:32
Unknown Object (File)
Tue, Nov 12, 05:55
Unknown Object (File)
Tue, Nov 12, 04:16
Unknown Object (File)
Tue, Nov 12, 03:29
Unknown Object (File)
Tue, Nov 12, 00:48
Unknown Object (File)
Mon, Nov 11, 22:08
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