codervio/envmanager

Codervio Environment manager

1.7 2018-03-07 23:55 UTC

This package is not auto-updated.

Last update: 2024-12-22 06:55:17 UTC


README

Join the chat at https://gitter.im/Codervio/Envmanager

Latest Version on Packagist Build Status Total Downloads

The Environment manager parses, populates dot environment variables from env files to super global $_ENV variable, apache and getenv function. It supports for checking variables and fetching system only variables.

It provides for editing environment file and manipulate them.

Screenshot

Donations

Due I am working 100% alone without any helps, organizations and any others team I can be satisfy for receiving any amount of payment to improve, develop and continue building on origin idea of framework.

You can pay any amount to PayPal: https://www.paypal.me/codervio?locale.x=en_US

Installation

  1. Installation via Composer on Packagist
  2. Installation using Git GIT clone component

Prerequisities

PHP version requirements: PHP >7.0

PHP extension: mbstring

Use use Codervio\Environment\EnvParser declaration for parsing dot env files.

Use use Codervio\Environment\EnvEditor declaration for edit and manage dot env file.

A [requirements] - Requirements and auto detect encodings script automatically can check mbstring extension and automatically detects encoding types.

Usage

Example of fetching env variables and loading into global super variables like $_ENV, using function getenv() or directly using instance:

If on a file example.env contains a data:

FOO=bar

After loading instance it can be fetching a variable:

use Codervio\Envmanager\Envparser;

$envparser = new Envparser('.env');
$envparser->load();
$envparser->run();

$result = $parser->getValue('FOO');
var_dump($result);

Returns a result will automatically detect type of getting env variables:

(string) 'bar'

Or get a result using env variables globally:

    echo apache_getenv('FOO')
    echo getenv('FOO')
    echo $_ENV('FOO')

Returning a result

A result returns in following orders:

  • using apache_getenv() if apache service is configured internally to use only Apache environment
  • using getenv() that most Unix systems supports
  • using PHP native super globals $_ENV function

It will automatically parse to PHP env functions and super globals so you can access via:

  • superglobals functions $_ENV
  • superglobals $_SERVER variable
  • using getenv() if is enabled by system
  • using apache_getenv() if is enabled by Apache service

Changelog

Status of core:

PHP version above 7.0. Quality assurance: Unit tests provided

Table of Contents

Envparser

Common env variables

  • For loading simple ENV variables use [example]
FOO=bar
VAREMPTY=
FOO1=foo1
WITHSPACES="with spaces"

Lists examples env variables

  • Lists of examples env variables: [lists]

Comments

  • Writing comments: [comments]
# comment
# a comment #comment
## A main comment ##
FOO=bar # a comment

Comments parsing as variable (dev only, not recommeded)

It is possible to parse comments variables such using:

 new Envparser('main.env', true);

A variable inside comment can be visible

 #COM1=BAR1

using command:

 $envparser->getAllValues(); # to get all values
 $envparser->getValue('#COM1'); # to get commented key

which returns as array and keeps # mark:

  ["#COM1"]=>
  string(4) "BAR1"

or directly:

 $envparser->getValue('#COM1');

will parsing a variable

  string(4) "BAR1"

Parsing apache env variables or unix exports env variables

  • Parsing export or setenv variables: [envexports]
setenv FOO1=value # general csh case
export FOO2=value 
SetEnv FOO3=value # Apache camel case

Get a system only variables

It is possible to fetch all system variables:

use Codervio\Envmanager\Envparser;

$envparser = new Envparser();
$envparser->load();
$envparser->run();

var_dump($envparser->getSystemVars());
  • For fetching single variable or just check a variable exists see [getSystemVars] and [checkSystemVar()].
  • See validation types for values in environment variables: [required()]
  • To fetch a comment from a file of specific variable use: [getComment()]

References

  • [requirements] - Requirements and auto detect encodings
  • [setEncoding()] - Manually specify encoding
  • [getEncoding()] - Detect encoding type from file
  • [checkSuperGlobalsSet()] - Check if set or get env directive for $_ENV active
  • [Envparser()] - A construct parser constructor
  • [load()] - Load an environment .env file or folder with .env files
  • [getComment()] - Get a comment from a variable of .env file
  • [getValue()] - Get a value from system environment variables and parsing variables
  • [getAllValues()] - Returns parsed environment variables internally as array
  • [getSystemVars()] - Fetch all or one system variables
  • [checkSystemVar()]- Returns boolean if system variables exists
  • [setStrictBool()] - Parse value to boolen on non-standard values such as 'y/n' or '1/0'
  • [required()] - Instance of variable validator and variable validators

EnvEditor

  • [Enveditor] - Instance environment for creating environment file
  • [Help] - Common helps and issues in a code