Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A small and fast .env loader for PHP

License

NotificationsYou must be signed in to change notification settings

adhocore/php-env

Repository files navigation

Latest VersionTravis BuildScrutinizer CICodecov branchStyleCISoftware License

  • Environment variable loader and retriever for PHP.
  • Sanitization/Filters can be applied on retrieval iffilter extension is loaded.
  • Using env to configure application is one of the12 postulates.

Installation

composer require adhocore/env

Usage

Loading

useAhc\Env\Loader;// Load env variables from .env file to `putenv` by default:(newLoader)->load('/project/root/.env');// Pass in boolean second param to control if the env should be reloaded:(newLoader)->load('/project/root/.env',true);// Load to $_SERVER global:(newLoader)->load('/project/root/.env',true, Loader::SERVER);// Load to $_ENV global and putenv():(newLoader)->load('/project/root/.env',true, Loader::ENV | Loader::PUTENV);// Load to all targets:(newLoader)->load('/project/root/.env',true, Loader::ALL);

Always wrap complex values within double quotes in.env file. Eg:APP_KEY="K&^¢*&D(?<µ}^(P\]X"

ENV Format

Supports# or; comments. Literal double quote should be escaped like"". See more examples below:

# comment linea=1b="2"c=$3#; also comment lined="lol"# emptye=# f is `"6"`f=""6""1_2=one_two# empty tooE=""A_B=Apple Ballx=Y

Reference is possible like so:

MAIN=1REF=${MAIN}/2REF2=${REF}/3# below will not be parsed as INV is not resolvedREF3=${INV}

Retrieving

useAhc\Env\Retriever;// Retrieve:echo Retriever::getEnv($key);// Default value:echo Retriever::getEnv('PAYMENT_GATEWAY','stripe');// Sanitization (pass third and optionally fourth parameters):echo Retriever::getEnv('MYSQL_PORT',3306,FILTER_VALIDATE_INT);// Or you can use `env()` which is alias of `Retriever::getEnv()`:echoenv('THE_KEY');

Seefilter_var for more on sanitizing/filtering values!

Benchmark

If you are interestedhere is a simple benchmark.


Consideration

By default this library only loads env toputenv().Be cautious exposing confidential credentials into$_ENV and$_SERVER which bug/error catchers may log.

Although this libray is already fast enough, in production you might want to boost performance a little by loading if only required:

if (!getenv('<LAST_ENV_APP_SHOULD_BE_AWARE_OF>')) {// Override false :)    (newLoader)->load('/project/root/.env',false);}

For example if your app last introducedFB_APP_ID env, but this value is not already hard set in the machine,it would be loaded via.env file else you are already covered.

Credits

This project isreleasemanaged byplease.


[8]ページ先頭

©2009-2025 Movatter.jp