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
F2848357: D2497.id.diff
Wed, Apr 24, 19:22
Unknown Object (File)
Sun, Apr 21, 22:06
Unknown Object (File)
Sun, Apr 21, 15:08
Unknown Object (File)
Thu, Apr 18, 14:45
Unknown Object (File)
Thu, Apr 18, 02:15
Unknown Object (File)
Wed, Apr 17, 19:19
Unknown Object (File)
Tue, Apr 16, 01:25
Unknown Object (File)
Tue, Apr 16, 01:25
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 Not Applicable
Unit
Tests Not Applicable

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