2017 © Pedro Peláez
 

library dotenv

DotEnv package - work derived from vlucas/phpdotenv

image

sebastiansulinski/dotenv

DotEnv package - work derived from vlucas/phpdotenv

  • Monday, February 5, 2018
  • by sebastiansulinski
  • Repository
  • 2 Watchers
  • 3 Stars
  • 7,077 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 9 Versions
  • 23 % Grown

The README.md

DotEnv

Package which enables to load environment variables from multiple .env files at multiple locations, (*1)

This package is a work that derived from package published by vlucas/phpdotenv with some additional functionality such as handling multiple .env files and setting up variables using instance of the class., (*2)

Build Status, (*3)

Installation

Install package using composer, (*4)

composer require sebastiansulinski/dotenv

Usage instructions

To use the plugin you'll need to have at least one .env file i.e., (*5)

// .env

DB_HOST=localhost
DB_NAME=test
DB_USER=user
DB_PASS=password

You load all your .env files when instantiating the SSD\DotEnv\DotEnv object., (*6)

require "vendor/autoload.php";

use SSD\DotEnv\DotEnv;

$dotEnv = new DotEnv(__DIR__ . DIRECTORY_SEPARATOR . '.env');

You can pass a single .env file, path to a directory with .env.* files or multiple paths / directories, (*7)

$dotEnv = new DotEnv(__DIR__ . DIRECTORY_SEPARATOR . '.env');
$dotEnv = new DotEnv(__DIR__);
$dotEnv = new DotEnv(
    __DIR__
    'another/path',
    'another/file/.env'
);

Loading variables

To load process the variables there are two methods load() and overload()., (*8)

The load() method will only set the variables that do not already exist, while overload() will set them all - overwriting any existing ones., (*9)

$dotEnv = new DotEnv(__DIR__);

// will only set variables
// that are not already set
$dotEnv->load();
$dotEnv = new DotEnv(__DIR__);

// will set all variables from the files
// overwriting any duplicates
$dotEnv->overload();

Required variables

To ensure that your system has all necessary variables available you can use required() method, which takes either a single variable name or an array of required variables., (*10)

$dotEnv = new DotEnv(__DIR__);

// will only set variables
// that are not already set
$dotEnv->load();

// either a single variable
$dotEnv->required('DB_HOST');
$dotEnv = new DotEnv(__DIR__);

// will only set variables
// that are not already set
$dotEnv->load();

// or an array of variables
$dotEnv->required([
    'DB_HOST',
    'DB_NAME',
    'DB_USER',
    'DB_PASS'
]);

If any of the required variables does not exist in any of the .env.*files - system will throw a RuntimeException., (*11)

Returning contents of .env file(s) as array

Use toArray() method to fetch the contents of the .env file(s), with or without setting up the environment variables., (*12)

$dotEnv = new DotEnv(__DIR__);

// will not set environment variables
$variables = $dotEnv->toArray();

var_dump($variables);

// ['DB_HOST' => '127.0.0.1', 'DB_NAME' => 'blog', ...]


// will set environment variables using load() method
$variables = $dotEnv->toArray(DotEnv::LOAD);

var_dump($variables);

// ['DB_HOST' => '127.0.0.1', 'DB_NAME' => 'blog', ...]


// will set environment variables using overload() method
$variables = $dotEnv->toArray(DotEnv::OVERLOAD);

var_dump($variables);

// ['DB_HOST' => '127.0.0.1', 'DB_NAME' => 'blog', ...]

Obtaining value stored in the variable

You can use a static get() method on the DotEnv object to retrieve the value stored in a given environment variable., (*13)

DotEnv::get('DB_HOST');

When you associate the string true, false with the variables within your .env file, they will automatically be converted to boolean true / false when using DotEnv::get. The same applies to the variable with null string, which will return null value., (*14)

If you specify a variable without any value associated (MY_VARIABLE=) - it will return an empty string ''., (*15)

You can provide a second argument to the get() method, which will be returned if variable was not found. The default value can be of a scalar or a Closure type., (*16)

DotEnv::get('DB_HOST', 'localhost');

DotEnv::get('DB_HOST', function() {

    return DotEnv::get('ENVIRONMENT') == 'live' ? 'localhost' : 127.0.0.1;

});

Checking if exists and equals

You can check if variable exists by using has() and whether it stores a given value by using is() methods., (*17)

DotEnv::has('NON_EXISTENT_VARIABLE');
// false

DotEnv::is('ENVIRONMENT', 'live')
// true / false

Setting variables

$dotEnv = new DotEnv(__DIR__);
$dotEnv->load();
$dotEnv->set('CUSTOM_VARIABLE', 123);
$dotEnv->required('CUSTOM_VARIABLE');

Variable referencing

If there is a variable that you'd like to inherit the value of you can use its name wrapped within the ${..} i.e., (*18)

MAIL_SMTP=true
MAIL_USER=mail@mail.com
MAIL_PASS=password
MAIL_PORT=587

MAIL_API_KEY=${MAIL_PASS}

The Versions

05/02 2018

dev-master

9999999-dev

DotEnv package - work derived from vlucas/phpdotenv

  Sources   Download

MIT BSD

The Requires

  • php ^7.1

 

The Development Requires

by Sebastian Sulinski

05/02 2018

v2.0.1

2.0.1.0

DotEnv package - work derived from vlucas/phpdotenv

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Sebastian Sulinski

29/11 2017

v2.0.0

2.0.0.0

DotEnv package - work derived from vlucas/phpdotenv

  Sources   Download

BSD

The Requires

  • php ^7.1

 

The Development Requires

by Sebastian Sulinski

12/11 2017

v1.2.0

1.2.0.0

DotEnv package - work derived from vlucas/phpdotenv

  Sources   Download

BSD

The Requires

  • php ^7.1

 

The Development Requires

by Sebastian Sulinski

01/07 2016

v1.1.1

1.1.1.0

DotEnv package - work derived from vlucas/phpdotenv

  Sources   Download

BSD

The Requires

  • php >=5.4.0

 

The Development Requires

by Sebastian Sulinski

01/07 2016

v1.1.0

1.1.0.0

DotEnv package - work derived from vlucas/phpdotenv

  Sources   Download

BSD

The Requires

  • php >=5.4.0

 

The Development Requires

by Sebastian Sulinski

21/12 2015

v1.0.1

1.0.1.0

DotEnv package - work derived from vlucas/phpdotenv

  Sources   Download

BSD

The Requires

  • php >=5.4.0

 

The Development Requires

by Sebastian Sulinski

19/12 2015

v1.0.0

1.0.0.0

DotEnv package - work derived from vlucas/phpdotenv

  Sources   Download

BSD

The Requires

  • php >=5.4.0

 

The Development Requires

by Sebastian Sulinski

27/08 2015

0.0.1

0.0.1.0

DotEnv package - work derived from vlucas/phpdotenv

  Sources   Download

BSD

The Requires

  • php >=5.4.0

 

by Sebastian Sulinski