Giter Club home page Giter Club logo

environment's Introduction

Environment Package

Packagist PHP Version Support Packagist Version Packagist Downloads Composer dependencies Test workflow Codecov

Read Environment file (.env).
Can complete or override data from getenv() / $_ENV / $_SERVER

Installation

composer require rancoud/environment

.env File example

# type of variables used
STRING=STRING
STRING_QUOTES=" STRING QUOTES "
INTEGER=9
FLOAT=9.0

BOOL_TRUE=TRUE
BOOL_FALSE=FALSE
NULL_VALUE=NULL

# variable in value
HOME=/user/www
CORE=$HOME/core
; $HOME won't be interpreted
USE_DOLLAR_IN_STRING="$HOME"

#comment line 1
;comment line 2

# import another env file use @ and the filename
@database.env

# multilines
RGPD="
i understand

    enough of email for \"me\"    

thanks
"

How to use it?

Warning, call constructor will not load values, you can:

  • use load() function
  • use any functions that automatically call load() inside

Simple example

// search .env file
$env = new Environment(__DIR__);
$values = $env->getAll();
$value = $env->get('a', 'defaultvalue');

Check keys and values

// check if a key exists
$env = new Environment(__DIR__);
$isExists = $env->exists('key1');
$isExists = $env->exists(['key1', 'key2']);

// check if value is set with allowed values
$isAllowed = $env->allowedValues('key1', ['value1', NULL, 'value2']);

Complete and Override values

Only type conversion will be done on those variables (no replacement with $).

You have 3 differents flags:

  • Environment::GETENV
  • Environment::ENV
  • Environment::SERVER

Complete is for filling values belong to keys having empty string or no values.
Override is for erasing values belong to keys.
The treatment given by the flags is always in the same order:

  1. getenv()
  2. $_ENV
  3. $_SERVER

You can also use 3 others flags.
Those will inject all keys and values found, your env file is not used for checkings keys.

  • Environment::GETENV_ALL
  • Environment::ENV_ALL
  • Environment::SERVER_ALL
$env = new Environment(__DIR__);

// complete with only getenv()
$env->complete(Environment::GETENV);

// complete with $_ENV then $_SERVER
$env->complete(Environment::SERVER | Environment::ENV);

// override with getenv() will erase values
$env->override(Environment::GETENV);

Enable cache

The file cached will not contains informations from getenv() / $_ENV / $_SERVER

// force using cache (if not exist it will be created)
$env = new Environment(__DIR__);
$env->enableCache();
$values = $env->getAll();

When load() is called?

For simplicity load() is automatically called when using thoses functions:

  • get
  • getAll
  • exists
  • complete

Multiline

You can check what kind of endline it using, by default it's PHP_EOL
You can change it with for using <br>

// force using cache (if not exist it will be created)
$env = new Environment(__DIR__);
$env->setEndline('<br />');

Include another .env

Inside .env file you can include another .env file with the @ operator at the begining of the line

Constructor variations

// search .env file
$env = new Environment(__DIR__);

// search dev.env file
$env = new Environment(__DIR__, 'dev.env');

// search .env file in folders __DIR__ then '/usr'
$env = new Environment([__DIR__, '/usr']);

// search dev.env file in folders __DIR__ then '/usr'
$env = new Environment([__DIR__, '/usr'], 'dev.env');

Environment Constructor

Settings

Mandatory

Parameter Type Description
folder string OR array folder to seek .env file

Optionnals

Parameter Type Default value Description
filename string .env custom name of .env file (don't forget to add file extension)

Environment Methods

General Commands

  • load():void
  • get(name: string, [default: mixed = null]): mixed|null
  • getAll(): arrray
  • exists(name: string|array): bool
  • allowedValues(name: string, values: array): bool

Cache File

  • enableCache(): void
  • disableCache(): void
  • flushCache(): void

Env variables

  • complete(flags: Environment::GETENV | Environment::ENV | Environment::SERVER): void
  • override(flags: Environment::GETENV | Environment::ENV | Environment::SERVER): void

Multilines endline interpretation

  • setEndline(endline: string): void
  • getEndline(): string

How to Dev

composer ci for php-cs-fixer and phpunit and coverage
composer lint for php-cs-fixer
composer test for phpunit and coverage

environment's People

Contributors

dependabot[bot] avatar rancoud avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.