cekurte/environment

A library to get the values from environment variables and process to php data types

v0.3.0 2016-05-27 15:00 UTC

This package is auto-updated.

Last update: 2024-11-16 02:27:32 UTC


README

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.

<?php

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"].

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.

<?php

// 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.

<?php

// 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.

<?php

// 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.

<?php

// 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.

<?php

// 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.

<?php

// 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