A simple library to increase the power of your environment variables.

Overview

Environment

Build Status Code Climate Coverage Status Latest Stable Version License SensioLabsInsight

  • A simple library (with all methods covered by php unit tests) to increase the power of your environment variables, contribute with this project!

Installation

  • The package is available on Packagist.
  • The source files is PSR-2 compatible.
  • Autoloading is PSR-4 compatible.
composer require cekurte/environment

Documentation

Setup your variables to PHP with putenv("KEY=VALUE") or put your environment variables into OS directly. Additionally you can use this library to perform this task vlucas/phpdotenv (we recommend that you use this library).

And now, use our library to get values and increase the power of your environment variables.

get("YOUR_ENVIRONMENT_KEY", "defaultValue"); // Using a function... @deprecated $data = env("YOUR_ENVIRONMENT_KEY", "defaultValue"); // Note that if the second parameter is omitted // then the return value (if your key not exists) will be null. // A new way was added to you get all environment variables as an array. $data = Environment::getAll(); // You can use a filter to get only the environment variables that you need. $data = Environment::getAll([ new Cekurte\Environment\Filter\KeyRegexFilter('/PROCESSOR/'), new Cekurte\Environment\Filter\ValueRegexFilter('/6/'), ]); // The method above will get all environment variables, where: // the environment variable name has the word PROCESSOR AND // the environment variable value has the number six. // And you can develop your own filters, contribute with this project! ">


use function Cekurte\Environment\env;        // To get values using a function (requires php >=5.6)
use Cekurte\Environment\Environment;         // To get values using a static class
use Cekurte\Environment\EnvironmentVariable; // To get values using a object

// ...

$data = Environment::get("YOUR_ENVIRONMENT_KEY");

// Getting default data if your environment variable not exists or not is loaded.
$data = Environment::get("APP_DEBUG", true);

// ...
// Other ways to get the values of environment variables.

// Using a object...
$data = (new EnvironmentVariable())->get("YOUR_ENVIRONMENT_KEY", "defaultValue");

// Using a function... @deprecated
$data = env("YOUR_ENVIRONMENT_KEY", "defaultValue");

// Note that if the second parameter is omitted
// then the return value (if your key not exists) will be null.

// A new way was added to you get all environment variables as an array.
$data = Environment::getAll();

// You can use a filter to get only the environment variables that you need.
$data = Environment::getAll([
    new Cekurte\Environment\Filter\KeyRegexFilter('/PROCESSOR/'),
    new Cekurte\Environment\Filter\ValueRegexFilter('/6/'),
]);

// The method above will get all environment variables, where:
// the environment variable name has the word PROCESSOR AND
// the environment variable value has the number six.

// And you can develop your own filters, contribute with this project!

This command will get the value of the environment variable and will convert values to respective php data type.

Actually are available the following resources to process your environment variables:

Examples

In this section you can see the examples to all resource types.

ArrayResource

The array resource parse a value from environment variable that is a string to the array php data type. Supposing that there exists an environment variable named "ENV_ARRAY" with the following value [1,2,3,"key"=>"value"].

"value"] ">
export ENV_ARRAY=[1,2,3,"key"=>"value"]

When you read this environment variable with our library, then it will convert the data to the correct data type (in this case to array) using the ArrayResource class.

string(5) "value" // } var_dump(Cekurte\Environment\Environment::get("ENV_ARRAY")); ">


// array(4) {
//   [0]=> int(1)
//   [1]=> int(2)
//   [2]=> int(3)
//   ["key"]=> string(5) "value"
// }
var_dump(Cekurte\Environment\Environment::get("ENV_ARRAY"));

BooleanResource

The boolean resource parse a value from environment variable that is a string to the boolean php data type. Supposing that there exists an environment variable named "ENV_BOOL" with the following value true.

export ENV_BOOL=true

When you read this environment variable with our library, then it will convert the data to the correct data type (in this case to boolean) using the BooleanResource class.



// bool(true)
var_dump(Cekurte\Environment\Environment::get("ENV_BOOL"));

JsonResource

The json resource parse a value from environment variable that is a string to the array php data type. Supposing that there exists an environment variable named "ENV_JSON" with the following value {"key":"value"}.

export ENV_JSON={"key":"value"}

When you read this environment variable with our library, then it will convert the data to the correct data type (in this case to array) using the JsonResource class.

string(5) "value" // } var_dump(Cekurte\Environment\Environment::get("ENV_JSON")); ">


// array(1) {
//   ["key"]=> string(5) "value"
// }
var_dump(Cekurte\Environment\Environment::get("ENV_JSON"));

NullResource

The null resource parse a value from environment variable that is a string to the null php data type. Supposing that there exists an environment variable named "ENV_NULL" with the following value null.

export ENV_NULL=null

When you read this environment variable with our library, then it will convert the data to the correct data type (in this case to null) using the NullResource class.



// NULL
var_dump(Cekurte\Environment\Environment::get("ENV_NULL"));

NumericResource

The numeric resource parse a value from environment variable that is a string to the numeric php data type (an integer or a float). Supposing that there exists an environment variable named "ENV_NUMERIC" with the following value 123.

export ENV_NUMERIC=123

When you read this environment variable with our library, then it will convert the data to the correct data type (in this case to int) using the NumericResource class.



// int(123)
var_dump(Cekurte\Environment\Environment::get("ENV_NUMERIC"));

UnknownResource

The unknown resource no parse the values from environment variable and is used to get values when all resource types can not parse the data. Supposing that there exists an environment variable named "ENV_UNKNOWN" with the following value "unknown".

export ENV_UNKNOWN="unknown"

When you read this environment variable with our library, then it will get the raw value using the UnknownResource class.



// string(7) "unknown"
var_dump(Cekurte\Environment\Environment::get("ENV_UNKNOWN"));

If you liked of this library, give me a star =).

Contributing

  1. Give me a star =)
  2. Fork it
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Make your changes
  5. Run the tests, adding new ones for your own code if necessary (vendor/bin/phpunit)
  6. Commit your changes (git commit -am 'Added some feature')
  7. Push to the branch (git push origin my-new-feature)
  8. Create new Pull Request
You might also like...
Lightweight library that eases using components built for ReactPHP in a traditional, blocking environment.

clue/reactphp-block Lightweight library that eases integrating async components built for ReactPHP in a traditional, blocking environment. ReactPHP pr

A simple shell script which loads a magento environment

A simple shell script to load up a Magento environment and execute PHP code Very experimental and should only be ran on dev servers REQUIRES php pcntl

Workshop environment for Decoupled Drupal

Decoupled Drupal Workshop 👋 Welcome to the Bluehorn Digital Decoupled Drupal workshop repository! This repository contains a decoupled ready Drupal b

MajorDoMo is an open-source DIY smarthome automation platform aimed to be used in multi-protocol and multi-services environment.
MajorDoMo is an open-source DIY smarthome automation platform aimed to be used in multi-protocol and multi-services environment.

MajorDoMo (Major Domestic Module) is an open-source DIY smarthome automation platform aimed to be used in multi-protocol and multi-services environment. It is based on web-technologies stack and ready to be delivered to any modern device. It is very flexible in configuration with OOP paradigm used to set up automation rules and scripts. This platform can be installed on almost any personal computer running Windows or Linux OS.

🐘 A probe program for PHP environment (一款精美的 PHP 探針, 又名X探針、劉海探針)

Simplified Chinese | 简体中文 Traditional Chinese(Taiwan) | 正體中文(臺灣) Traditional Chinese(Hong Kong) | 正體中文(香港) Japanese | 日本語 😎 X Prober This is a probe

Full PHP development environment for Docker.
Full PHP development environment for Docker.

Full PHP development environment based on Docker. Use Docker First - Learn About It Later! Join Us Awesome People Laradock is an MIT-licensed open sou

The Phar Installation and Verification Environment (PHIVE)

The Phar Installation and Verification Environment (PHIVE) Installation and verification of phar distributed PHP applications has never been this easy

A Magento Development Environment Made of Docker Containers

Docker for Magento 1 Extension Development Tl;dr How do I use this? Clone it. Type docker-compose up -d. Install a Magento Mount your file share. Deve

A dockerized magento 2 community environment ready for development or production.

Painless Magento 2 & 1 A dockerized magento 2 community environment ready for development or production. It supports magento 1.9.x for development Ins

Comments
  • Method to get all env variables

    Method to get all env variables

    My use case is I want to get an array of all env vars that start with PHP_INI_, and loop through them with ini_set.

    If I could get an array of all env vars, that would help.

    opened by timkelty 10
  • Add a default argument

    Add a default argument

    It would be great if you could pass a second argument as the default value, if the env var isn't set.

    Currently, I have to do this:

    $enableIndexing = getenv('ENABLE_INDEXING') !== false ? Environment::get('ENABLE_INDEXING') : true;
    

    Would be great if I could just do:

    $enableIndexing = Environment::get('ENABLE_INDEXING', true);
    
    opened by timkelty 1
  • Add `getenv` to `EnvironmentVariable::getEnvironmentVariables()`

    Add `getenv` to `EnvironmentVariable::getEnvironmentVariables()`

    Include call to getenv() in EnvironmentVariable::getEnvironmentVariables() to align with results from EnvironmentVariable::getEnvironmentVariable().

    Fixes #10.

    opened by karlwilbur 0
  • `getenv()` values missing from `EnvironmentVariable::getEnvironmentVariables()`

    `getenv()` values missing from `EnvironmentVariable::getEnvironmentVariables()`

    Using Environment::get(), one can obtain variables not included in Environment::getAll(). This odd behavior is caused by EnvironmentVariable::getEnvironmentVariables() searching only $_ENV and $_SERVER, whereas EnvironmentVariable::getEnvironmentVariable() searches $_ENV, $_SERVER, and getenv().

    I suggest including a call to getenv() inside of EnvironmentVariable::getEnvironmentVariables() for the purpose of pulling in any variables that may not be included in $_ENV (such as when variables_order is set to "GPCS" thus excluding $_ENV).

    I am going to make this code change and submit a PR.

    opened by karlwilbur 0
Owner
João Paulo Cercal
João Paulo Cercal
Harden request headers, login interface and passwords to increase backend security.

JvMTECH.NeosHardening Package for Neos CMS Harden request headers, login interface and passwords to increase backend security. Installation composer r

Jung von Matt TECH 3 May 4, 2022
Increase "Death by 1000 needles" attack up to 100x times

Підсили свою атаку у 100 разів разом із db1000nX100 db1000nX100 це Linux контейнер для популярної програми db1000n, щo дозволяє значно підняти ефектив

null 62 Dec 31, 2022
This is a library to serialize PHP variables in JSON format

This is a library to serialize PHP variables in JSON format. It is similar of the serialize() function in PHP, but the output is a string JSON encoded. You can also unserialize the JSON generated by this tool and have you PHP content back.

Zumba 118 Dec 12, 2022
Examples of the power of WordPress plugins that will wreck your site.

Examples of the power of WordPress plugins that will wreck your site.

Teemu Suoranta 4 Mar 2, 2022
Find undefined and unused variables with the PHP Codesniffer static analysis tool.

PHP_CodeSniffer VariableAnalysis Plugin for PHP_CodeSniffer static analysis tool that adds analysis of problematic variable use. Warns if variables ar

Payton Swick 116 Dec 14, 2022
Find forgotten variables dump in PHP source code.

PHP VarDump Check This repository is abandoned. Suggested alternative: https://github.com/php-parallel-lint/PHP-Var-Dump-Check PHP console application

Jakub Onderka 27 Jul 13, 2022
Find forgotten variables dump in PHP source code.

PHP VarDump Check PHP console application for find forgotten variable dump. Support PHP build in method print_r, var_dump and var_export method and al

PHP Parallel lint 13 Jul 13, 2022
HLedger is cross-platform accounting software for both power users and folks new to accounting

HLedger Plain Text Accounting on Nextcloud HLedger is cross-platform accounting software for both power users and folks new to accounting. It's good f

Ryan Boder 11 Jan 20, 2022
A PocketMine-MP plugin that allows you to comfortably register events using a new piece of PHP 8 power — attributes.

AdvancedEvents This is a PocketMine-MP plugin that allows you to comfortably register events using a new piece of PHP 8 power — attributes. Inspired b

JuraSciix 7 Dec 5, 2022
Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Super Simple DTO Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Mohammed Manssour 8 Jun 8, 2023