opxcore/config-environment

The OpxCore config environment loader.

1.0.6 2021-02-17 13:03 UTC

This package is auto-updated.

Last update: 2024-04-17 20:09:20 UTC


README

Build Status Coverage Status Latest Stable Version Total Downloads License

Installing

composer require opxcore/config-environment

Important notice

All environment variables are stored in static property so any instance of environment would have access to the same set of environment variables.

Usage

use OpxCore\Config\Environment;

$environment = new Environment($path);

where $path is directory where environment file is placed.

Loading environment

To load environment variables from file call $environment->load($filename), where $filename is a name of file. All variables would be stored locally, not affecting any of $_ENV, $_SERVER and so on.

Additional arguments is load($filename, $safe, $silent). If $safe is set to true variables would not be overwritten if they are already present. If $silent is set to true no exceptions would be thrown. Function returns true if environment file wss processed successfully, false in case of any errors.

Reading environment variables

$environment->get($key, $default) returns environment variable if it set, otherwise returns value assigned to default or return value of callable passed to as default.

$environment->has($key) returns true is environment variable set, false otherwise.

Manipulations

$environment->set($key, $value, $safe) sets value to environment. If $safe is set to true variables would not be overwritten if they are already present. Passed values of string type it would be parsed (see below), others would be set as is.

$environment->unset($key) removes variable from environment.

Format

Each line of environment must contain one value, consisting of variable name and its value separated by = sign. Other words, part of line before = sign would be used as variable name, another part would be value of variable.

For example:

APP_NAME=My awesome application

Each value would be parsed according next rules:

String

Each quoted value would be converted to unquoted strings.

DB_CONNECTION="mysql"
DB_HOST="127.0.0.1"

Result of $environment->get('DB_HOST') would be '127.0.0.1'.

Boolean

If value is true or false it would be converted to boolean type.

CACHE_ENABLE=true
304_RESPONSE=false

Null

Null values would set to null

CACHE_DRIVER=null

Array

Arrays must start with [ and ends with ]. Items must be separated by commas. Each item would be parsed according this rules. Nested arrays are not supported.

BROADCAST_DRIVER=[log,telegram]

Numbers

If value represents any of number format it would be converted to integer or float.

COUNT=42
MULTIPLIER=1.05
FLOAT=0.15E-10

If parser would not recognise type of value, original string would be returned.

APP_KEY=base64:0vqkPYSbwPm3MOzdxQJ76Ps6pouZRjN5xPx3b+dm628=

Result of $environment->get('APP_KEY') would be 'base64:0vqkPYSbwPm3MOzdxQJ76Ps6pouZRjN5xPx3b+dm628='.