GithubHelp home page GithubHelp logo

isabella232 / php-env Goto Github PK

View Code? Open in Web Editor NEW

This project forked from silinternational/php-env

0.0 0.0 0.0 61 KB

Utility class for working with environment variables in PHP that handles 'true', 'false', and 'null' more intelligently.

License: MIT License

Shell 0.42% PHP 98.57% Makefile 1.02%

php-env's Introduction

Sil/PhpEnv/

Simple PHP library for getting (or requiring) environment variables, designed to handle true, false, and null more intelligently. If desired, an environment variable's value can be split into an array automatically.

Build Status

Scrutinizer Code Quality Build Status

Setup

  1. Clone this repo.
  2. Copy local.env.dist to local.env and update GitHub.com token as appropriate.
  3. Run make test to install dependencies and run PHPUnit tests.

Makefile script

There is a Makefile in place to simplify common tasks.

  • make test - does composer install and runs phpunit tests

Classes in Sil/PhpEnv namespace

  1. Env: use Sil\PhpEnv\Env;
  2. EnvVarNotFoundException: use Sil\PhpEnv\EnvVarNotFoundException;

Class Env summary of functions

  1. get - public static function get($varname, $default = null)

    • searches the local environment for $varname and returns the corresponding value string
    • if $varname is not set or the value is empty (only whitespace), get returns $default parameter
    • if the value string corresponding to $varname is 'true', 'false' or 'null', get returns php values of true, false, or null respectively
    • NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php true.
      false and null are handled similarly. Other value strings will have leading/trailing whitespace trimmed.
  2. getString - public static function getString($varname, $default = null): ?string

    • searches the local environment for $varname and returns the corresponding trimmed value string
    • if $varname is not set or the value is empty (only whitespace), getString returns $default parameter
  3. getBoolean - public static function getBoolean($varname, $default = null): ?bool

    • searches the local environment for $varname and returns the corresponding boolean value string
    • if $varname is not set or the value is empty (only whitespace), getBoolean returns $default parameter
    • if the value string corresponding to $varname is 'true', 'false' or 'null', getBoolean returns php values of true, false, or null respectively
    • if the value is not boolean, getBoolean returns $default parameter
    • NOTE: Any value string containing 'true' with any combination of case and/or leading/trailing whitespace still returns php true. false and null are handled similarly.
  4. getArray - public static function getArray($varname, array $default = [])

    • searches the local environment for $varname and returns the corresponding value string with comma separated elements as a php array
    • if $varname is not set or the value is empty (only whitespace), getArray returns $default parameter which must be an array
    • if $default is not an array, it throws a TypeError exception
  5. requireEnv - public static function requireEnv($varname)

    • searches the local environment for $varname and returns the corresponding value string
    • if $varname is not set or the value is empty (only whitespace), it throws EnvVarNotFoundException
    • 'true', 'false', and 'null' are handled the same as get()
  6. requireArray - public static function requireArray($varname)

    • searches the local environment for $varname and returns the corresponding value string with comma separated elements as a php array
    • if $varname is not set or the value is empty (only whitespace), it throws EnvVarNotFoundException

Class EnvVarNotFoundException

class EnvVarNotFoundException extends \Exception

EnvVarNotFoundException is thrown by requireEnv() and requireArray() when $varname is not found in the local environment or the corresponding value string is empty (only whitespace)


Env example function calls

Assume this local.env file

EMPTY=
SPACES=      
WHITESPACE= Some whitespace    
FALSE=False
TRUE=TRUE
NULL=null
LOWERCASE=abc123
UPPERCASE=ABC123
ARRAY0=
ARRAY1=one
ARRAY=one,two,,three

Example function calls and results

  • get - public static function get($varname, $default = null)

    1. Env::get('NOTFOUND') - returns null
    2. Env::get('NOTFOUND', 'bad data') - returns 'bad data'
    3. Env::get('EMPTY') - returns ''
    4. Env::get('SPACES') - returns ''
    5. Env::get('WHITESPACE') - returns 'Some whitespace'
    6. Env::get('FALSE') - returns false
    7. Env::get('TRUE') - returns true
    8. Env::get('NULL') - returns null
    9. Env::get('LOWERCASE') - returns 'abc123'
    10. Env::get('UPPERCASE') - returns 'ABC123'
  • requireEnv - public static function requireEnv($varname)

    1. Env::requireEnv('NOTFOUND') - throws EnvVarNotFoundException
    2. Env::requireEnv('EMPTY') - throws EnvVarNotFoundException
    3. Env::requireEnv('WHITESPACE') - returns 'Some whitespace'
    4. Env::requireEnv('FALSE') - returns false
    5. Env::requireEnv('LOWERCASE') - returns 'abc123'
  • getArray - public static function getArray($varname, array $default = [])

    1. Env::getArray('NOTFOUND') - returns []
    2. Env::getArray('NOTFOUND', ['one', 'two']) - returns ['one', 'two']
    3. Env::getArray('NOTFOUND', 'one,two,three') - throws TypeError exception
    4. Env::getArray('ARRAY0') - returns ['']
    5. Env::getArray('ARRAY1') - returns ['one']
    6. Env::getArray('ARRAY') - returns ['one', 'two', '', 'three']
  • requireArray - public static function requireArray($varname)

    1. Env::requireArray('NOTFOUND') - throws EnvVarNotFoundException
    2. Env::requireArray('EMPTY') - throws EnvVarNotFoundException
    3. Env::requireArray('ARRAY1') - returns ['one']
    4. Env::requireArray('ARRAY') - returns ['one', 'two', '', 'three']

php-env's People

Contributors

forevermatt avatar lvail avatar mtompset avatar fillup avatar baggerone avatar longrunningprocess 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.