Skip to content
/ env Public

Set and use environment from JSON file for PHP.

License

Notifications You must be signed in to change notification settings

kijtra/env

Repository files navigation

kijtra/env

Build Status Coverage Status Software License Total Downloads

Set and use environment from JSON file. Inspired vlucas/phpdotenv.
⚠️ JSON FILE ONLY

Support PHP >= 5.3

Install

Using Composer

composer.phar require kijtra/env

SetUp

<?php
// Autoload only
require_once '/path/to/vendor/autoload.php';

Function usage

JSON example

{
    "FOO": "Foo value",
    "BAR": {
        "BAR-one": "Bar One value",
        "BAR-two": "Bar Two value"
    }
}

Usage

<?php
// Load JSON file
env(array('/path/to/example.json'));

// Or multiple files
// env(array('/path/to/example.json', '../../other.json'));

// Basic use
echo env('FOO'); // "Foo value"

// You can use dot separated name
// (better than vlucas/phpdotenv)
echo env('BAR.BAR-one'); // "Bar One value"

// Get all environments
$all = env();

// If you need clear all environment, set PHP_EOL
env(PHP_EOL);

JSON format

Simple

{
    "FOO": "Foo value",
    "BAR": "Bar value"
}

Nested

{
    "FOO": {
        "FOO-one": "Foo One value",
        "FOO-two": "Foo Two value"
    },
    "BAR": [
        "Bar One value",
        "Bar Two value"
    ]
}

Placeholder

{
    "FOO": "Foo value",
    "BAR": "{FOO} after Bar value"
}

And reverse replacing placeholder
(better than vlucas/phpdotenv)

{
    "FOO": "{BAR} before Foo value",
    "BAR": "Bar value"
}

Nested placeholder
(better than vlucas/phpdotenv)

{
    "FOO": {
        "FOO-one": "Foo One value",
        "FOO-two": "Foo Two value"
    },
    "BAR": "{FOO.FOO-two} and Bar value"
}

Class version

Function env() is alias of this class.

<?php
use \kijtra\Env;

// Load JSON file
Env::file('/path/to/env.json');

// Or multiple files
Env::file(array('/path/to/env.json', '../other.json'));
Env::file('/path/to/env.json', '../other.json');

// Basic use
echo Env::get('FOO');

// You can use dot separated name
echo Env::get('BAR.BAR-one');

// Get all environments
$all = Env::all();

// Clear all environment
 Env::clear();

TODO

JavaScript(node.js) version