melbahja/environ

Load environment variables from .env file ONLY on $_ENV and runtime.

v1.1.0 2020-07-19 23:38 UTC

This package is auto-updated.

Last update: 2024-10-29 05:10:52 UTC


README

Load PHP environment variables from .env (INI syntax) file only on $_ENV and runtime.

Build Status Twitter

Installation :

composer require melbahja/environ

Why?

Some env libraries load variables into $_SERVER and $_REQUEST, which is a stupid idea that can lead to expose credentials and insert sensitive information into log files. environ only load variables to superglobal $_ENV and runtime via putenv.

Usage :

/path/to/your/project/.env

; set a var
APP_MODE = "dev"

; array
[DATABASE]
HOST = '127.0.0.1'
USERNAME = 'root'
PASSWORD = null

YourScript.php

require 'vendor/autoload.php';

use Melbahja\Environ\Environ;

// environ looking for .env or env.ini file in your directory
Environ::load('/path/to/your/project');

var_dump(Environ::get('APP_MODE')); // string

var_dump(Environ::get('DATABASE')); // array

var_dump($_ENV['DATABASE']); // array

Note:

Arrays will not be available in getenv(), you can only access them via $_ENV or Environ::get().

Helper

  # if you want a helper
  function env(string $var, $default = null)
  {
    return \Melbahja\Environ\Environ::get($var, $default);
  }

Environ methods :

Environ::load(string $directory): bool
Environ::get(string $var, $default = null): mixed
Environ::set(string $var, $value): bool
# Example: Environ::is('apache'), Environ::is('cli')
Environ::is(string $sapi): bool

License :

MIT Copyright (c) 2018-present Mohamed Elbahja