Skip to content
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

Load environment variables from $_ENV, $_SERVER and getenv() if in thread-safe environment #205

Merged
merged 3 commits into from
Nov 18, 2022

Conversation

clue
Copy link
Owner

@clue clue commented Nov 15, 2022

This changeset adds support for loading environment variables from the DI container configuration from $_ENV, $_SERVER and getenv() if in a thread-safe environment:

<?php

require __DIR__ . '/../vendor/autoload.php';

$container = new FrameworkX\Container([
    React\MySQL\ConnectionInterface::class => function (string $DB_HOST = 'localhost', string $DB_USER = 'root', string $DB_PASS = '', string $DB_NAME = 'acme') {
        // connect to database defined in optional $DB_* environment variables
        $uri = 'mysql://' . $DB_USER . ':' . rawurlencode($DB_PASS) . '@' . $DB_HOST . '/' . $DB_NAME . '?idle=0.001';
        return (new React\MySQL\Factory())->createLazyConnection($uri);
    }
]);

$app = new FrameworkX\App($container);

// …

The previous version only supported explicit configuration of container variables and automatically loading environment variables from $_SERVER only. The updated version will now also load environment variables from $_ENV and getenv() if executed in a thread-safe environment.

  • The $_ENV superglobal will only contain the environment variables when variables_order includes E (the default in PHP, but disabled by default in Debian/Ubuntu-based distributions). Accordingly, this is commonly an empty array.
  • The $_SERVER superglobal will only contain the environment variables in CLI and CGI/FastCGI environment and when variables_order includes S (the default). Most notably, this will not contain any environment variables for PHP's development web server.
  • The getenv() function (and putenv()) can be used to access the process environment. This works across all environments, but may exhibit unsafe behavior and leak environment variables from other threads if executed in a multithreaded environment (such as when running as a multithreaded Apache module). Accordingly, we will only fall back to this method when PHP is not compiled with thread safety (NTS, which is the default and means it can not safely be used in a multithreaded environment anyway) or when used in an environment that does not leverage multithreading to handle parallel requests (ZTS used in CLI or CGI/FastCGI). Most notably, this will now be used as a fall back for PHP's development web server.

As a result, automatically loading environment variables from the Container will now "just work" across all environments. Considering the above logic, the resulting code appears relatively straightforward. Together with this analysis and multiple tries, you're looking at several days worth of work, enjoy!

Builds on top of #184 and #91.
Refs #200, #201 and #204.
Also refs #101 as this is the next big step in adding better configuration support and support for environment variables and .env (dotenv) files in the future.

@clue clue added the new feature New feature or request label Nov 15, 2022
@SimonFrings SimonFrings merged commit a477f66 into clue:main Nov 18, 2022
@SimonFrings SimonFrings deleted the process-env branch November 18, 2022 15:08
@SimonFrings SimonFrings added this to the v0.13.0 milestone Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants