All Projects → adhocore → php-env

adhocore / php-env

Licence: MIT license
A small and fast .env loader for PHP

Programming Languages

PHP
23972 projects - #3 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to php-env

checkdotenv
Verify environment variables presence for Node JS.
Stars: ✭ 12 (-36.84%)
Mutual labels:  dotenv, environment-variables, env
exenv
Exenv makes loading environment variables from external sources easy.
Stars: ✭ 35 (+84.21%)
Mutual labels:  dotenv, environment-variables, env
envfile
Parse and write environment files with Node.js
Stars: ✭ 42 (+121.05%)
Mutual labels:  dotenv, environment-variables, env
webpack-dotenv-plugin
Use dotenv with webpack.
Stars: ✭ 53 (+178.95%)
Mutual labels:  dotenv, environment-variables, env
ts-dotenv
Strongly-typed environment variables for Node.js
Stars: ✭ 18 (-5.26%)
Mutual labels:  dotenv, environment-variables, env
dotenvy
Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents
Stars: ✭ 31 (+63.16%)
Mutual labels:  dotenv, environment-variables, env
ini
📝 Go INI config management. support multi file load, data override merge. parse ENV variable, parse variable reference. Dotenv file parse and loader. INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用。DotEnv 解析加载
Stars: ✭ 72 (+278.95%)
Mutual labels:  dotenv, environment-variables, env
read-env
🔧 Transform environment variables into JSON object with sanitized values.
Stars: ✭ 60 (+215.79%)
Mutual labels:  environment-variables, env
envyable
The simplest yaml to ENV config loader.
Stars: ✭ 78 (+310.53%)
Mutual labels:  environment-variables, env
superconfig
Access environment variables. Also includes presence validation, type coercion and default values.
Stars: ✭ 33 (+73.68%)
Mutual labels:  dotenv, environment-variables
envman
Manage your .env configuration easily
Stars: ✭ 20 (+5.26%)
Mutual labels:  environment-variables, env
dart environment config
Environment specific config generator for Dart and Flutter applications during CI/CD builds
Stars: ✭ 87 (+357.89%)
Mutual labels:  dotenv, environment-variables
env-dot-prop
♻️ Get, set, or delete nested properties of process.env using a dot path
Stars: ✭ 31 (+63.16%)
Mutual labels:  environment-variables, env
envclasses
envclasses is a library to map fields on dataclass object to environment variables.
Stars: ✭ 26 (+36.84%)
Mutual labels:  environment-variables, env
tfenv
Transform environment variables for use with Terraform (e.g. `HOSTNAME` ⇨ `TF_VAR_hostname`)
Stars: ✭ 120 (+531.58%)
Mutual labels:  environment-variables, env
envsafe
🔒 Makes sure you don't accidentally deploy apps with missing or invalid environment variables.
Stars: ✭ 705 (+3610.53%)
Mutual labels:  environment-variables, env
vite-plugin-environment
Easily expose environment variables in Vite.js
Stars: ✭ 57 (+200%)
Mutual labels:  environment-variables, env
angular-cli-envvars
Example project for my article "Angular CLI and OS Environment Variables"
Stars: ✭ 56 (+194.74%)
Mutual labels:  dotenv, environment-variables
dotenv validator
This gem check if required env variables are present and its format using the .env and .env.sample files from Dotenv.
Stars: ✭ 33 (+73.68%)
Mutual labels:  dotenv, env
php-underscore
PHP underscore inspired &/or cloned from _.js, with extra goodies like higher order messaging
Stars: ✭ 42 (+121.05%)
Mutual labels:  adhocore, hacktoberfest2021

adhocore/env

Latest Version Travis Build Scrutinizer CI Codecov branch StyleCI Software License Donate 15 Donate 25 Donate 50 Tweet

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

Installation

composer require adhocore/env

Usage

Loading

use Ahc\Env\Loader;

// Load env variables from .env file to `putenv` by default:
(new Loader)->load('/project/root/.env');

// Pass in boolean second param to control if the env should be reloaded:
(new Loader)->load('/project/root/.env', true);

// Load to $_SERVER global:
(new Loader)->load('/project/root/.env', true, Loader::SERVER);

// Load to $_ENV global and putenv():
(new Loader)->load('/project/root/.env', true, Loader::ENV | Loader::PUTENV);

// Load to all targets:
(new Loader)->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 line
a=1
b="2"
c=$3#
; also comment line
d="lol"
# empty
e=
# f is `"6"`
f=""6""
1_2=one_two
# empty too
E=""
A_B=Apple Ball
x=Y

Reference is possible like so:

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

Retrieving

use Ahc\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()`:
echo env('THE_KEY');

See filter_var for more on sanitizing/filtering values!

Benchmark

If you are interested here is a simple benchmark.


Consideration

By default this library only loads env to putenv(). 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 :)
    (new Loader)->load('/project/root/.env', false);
}

For example if your app last introduced FB_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 is release managed by please.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].