phossa2/env

This package is abandoned and no longer maintained. No replacement package was suggested.

Library to load environments from a shell style file.

2.0.6 2016-07-04 02:59 UTC

This package is not auto-updated.

Last update: 2020-02-05 04:00:42 UTC


README

PLEASE USE phoole/env library instead

Build Status Code Quality Code Climate PHP 7 ready HHVM Latest Stable Version License

phossa2/env is a library to load environment variables from fully bash shell compatible files.

It requires PHP 5.4, supports PHP 7.0+ and HHVM. It is compliant with PSR-1, PSR-2, PSR-4.

Installation

Install via the composer utility.

composer require "phossa2/env=2.*"

or add the following lines to your composer.json

{
    "require": {
       "phossa2/env": "2.*"
    }
}

Usage

  • Put your environments in file .env,

    # this is comment line
    BASE_DIR='/home/web'
    
    # reference here
    APP_DIR=${BASE_DIR}/app   # another comment here
  • Load and use your env variables in PHP script

    <?php
    // ...
    $env = new Phossa2\Env\Environment();
    
    # load env
    $env->load(__DIR__ . '/.env');
    
    // use your env
    echo getenv('APP_DIR');

Features

  • Compatible with bash if not using extended features like relaxed syntax, get current dir and file or PHP globals.

  • Support shell default values, ${param:-new} or ${param:=new}

  • Able to source another_env_file in the env file

  • By default, WILL overwrite any existing environment variables. This is the default behavior in bash.

    To disable overwrite and honor existing env variables,

    env->load('./.env', $overload = false);
  • Relaxed syntax (not compatible with bash) in env file

    # spaces before and after '=' is allowed. NOT recommended though
    ROOT_DIR = /var/tmp
  • Get current path, dir, filename with ${BASH_SOURCE}, ${BASH_SOURCE%/*}, ${BASH_SOURCE##*/}

    # set current file
    MY_FILE=${BASH_SOURCE##*/}
    
    # set root dir to current dir
    ROOT_DIR=${BASH_SOURCE%/*}

    or with ${__PATH__}, ${__DIR__}, ${__FILE__}, which is not compatible with bash script.

  • Support PHP global variables like $_SERVER etc.

    This is not compatible with shell script, thus NOT recommended.

    HOST=${_SERVER.HTTP_HOST}
  • Support PHP 5.4+, PHP 7.0+, HHVM

  • PHP7 ready for return type declarations and argument type declarations.

  • PSR-1, PSR-2, PSR-4 compliant.

Change log

Please see CHANGELOG from more information.

Testing

$ composer test

Contributing

Please see CONTRIBUTE for more information.

Dependencies

  • PHP >= 5.4.0

  • phossa2/shared >= 2.0.21

License

MIT License