climbx/dotenv

.env files manager

1.0.5 2021-06-11 00:00 UTC

This package is auto-updated.

Last update: 2024-05-12 17:52:53 UTC


README

Build Status Maintainability Test Coverage

Dotenv component parses .env files and add them to both $_ENV and $_SERVER.

Install

$ composer require climbx/dotenv

Usage

# .env

foo=bar
bar=baz
// PHP

use Climbx\Dotenv\Dotenv;

$dotenv = new Dotenv();
$envPath = __DIR__ . '/.env';

/*
 * Loads env vars into $_ENV and $_SERVER
 * without overwriting existing variables
 */
$dotenv->load($envPath);

/*
 * Loads env vars into $_ENV and $_SERVER
 * if a variable already exists, it is overridden.
 */
$dotenv->overload($envPath);

Env files syntax

# Comment line

foo=bar                                 # End line comment
bar='this is a single quotted value'    # allows whitespaces
baz="this is a double quotted value"   
num=1234

Variables references

Simple var reference:

foo=bar
baz=$foo        # outputs ['baz' => 'bar']

Multiple var references:

foo=1
bar=2

spaced="$foo and $bar"   # outputs ['spaced' => '1 and 2']
collapsed=$one$two       # outputs ['collapsed' => '12']

Partial var references:

foo="one"
bar="two"

baz=${foo}/${bar}/three      # outputs ['baz' => 'one/two/three']

Escaped var declaration char

foo=bar
echaped1=\$foo   # outputs ['echaped1' => '$foo']
echaped2=\${foo} # outputs ['echaped2' => '${foo}']

Missing var reference.
Missing references don't throw an exception. If a reference is not found, the value is set to an empty string.

missing1=$foo     # outputs ['missing1' => '']
missing2=${foo}  # outputs ['missing2' => '']