Skip to content

Commit

Permalink
Merge pull request #2 from adhocore/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
adhocore committed Jul 15, 2018
2 parents 50fca59 + ad9538f commit ed56df9
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 32 deletions.
17 changes: 14 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
sudo: required

cache:
directories:
- $HOME/.composer/cache
Expand All @@ -8,15 +6,28 @@ git:
depth: 1

language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- nightly

matrix:
allow_failures:
- php: nightly

install:
- composer install --prefer-dist

before_script:
- for P in src tests; do find $P -type f -name '*.php' -exec php -l {} \;; done

script:
- vendor/bin/phpunit --coverage-text
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml

after_success:
- bash <(curl -s https://codecov.io/bash)
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^4.8 || ^5.7 || ^6.5"
},
"config": {
"optimize-autoloader": true,
"preferred-install": {
"*": "dist"
}
}
}
39 changes: 35 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
## adhocore/env [![build status](https://travis-ci.org/adhocore/env.svg?branch=master)](https://travis-ci.org/adhocore/env)
## adhocore/env

[![Latest Version](https://img.shields.io/github/release/adhocore/env.svg?style=flat-square)](https://github.com/adhocore/env/releases)
[![Travis Build](https://travis-ci.org/adhocore/env.svg?branch=master)](https://travis-ci.org/adhocore/env?branch=master)
[![Scrutinizer CI](https://img.shields.io/scrutinizer/g/adhocore/env.svg?style=flat-square)](https://scrutinizer-ci.com/g/adhocore/env/?branch=master)
[![Codecov branch](https://img.shields.io/codecov/c/github/adhocore/env/master.svg?style=flat-square)](https://codecov.io/gh/adhocore/env)
[![StyleCI](https://styleci.io/repos/107715208/shield)](https://styleci.io/repos/107715208)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

- Environment variable loader and retriever for PHP.
- Sanitization/Filters can be applied on retrieval if `filter` extension is loaded.
- Using env to configure application is one of the [12 postulates](https://12factor.net/config).

## Installation
```
composer require adhocore/env
```

## Usage

### Loading

```php
use Ahc\Env\Loader;
use Ahc\Env\Retriever;

// Load env variables from .env file to `putenv` by default:
(new Loader)->load('/project/root/.env');
Expand All @@ -22,11 +32,17 @@ use Ahc\Env\Retriever;
// Load to $_SERVER global:
(new Loader)->load('/project/root/.env', true, Loader::SERVER);

// Load to $_ENV global:
(new Loader)->load('/project/root/.env', true, Loader::ENV);
// Load to $_ENV global and putenv():
(new Loader)->load('/project/root/.env', true, Loader::ENV | Loader::PUTENV);

// Load to all targets:
(new Loader)->load('/project/root/.env', true, Loader::ALL);
```

### Retrieving

```php
use Ahc\Env\Retriever;

// Retrieve:
echo Retriever::getEnv($key);
Expand All @@ -43,3 +59,18 @@ echo env('THE_KEY');

See [filter_var](http://php.net/filter_var) for more on sanitizing/filtering values!

### Consideration

By default this library only loads env to `putenv()`.
Be cautious exposing confidential credentials into `$_ENV` and `$_SERVER` which bug/error catchers may log.

Although this libray is already fast enough, in production you might want to boost performance a little by loading if only required:

```php
if (!getenv('<LAST_ENV_APP_SHOULD_BE_AWARE_OF>')) {
// Override false :)
(new Loader)->load('/project/root/.env', false);
}
```

For example if your app last introduced `FB_APP_ID` env, but this value is not already hard set in the machine, it would be loaded via `.env` file else you are already covered.
39 changes: 24 additions & 15 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,40 @@ public function load($file, $override = false, $mode = self::PUTENV)
* @param bool $override
* @param int $mode
*/
protected function setEnv(array $vars, $override, $mode)
protected function setEnv(array $vars, $override, $mode = self::PUTENV)
{
if ($mode < self::PUTENV || $mode > self::ALL) {
$mode = self::PUTENV;
}

$default = microtime(1);
$default = \microtime(1);

foreach ($vars as $key => $value) {
// Skip if we already have value and cant override.
if (!$override && $default !== Retriever::getEnv($key, $default)) {
continue;
}

if ($mode & self::ENV) {
$_ENV[$key] = $value;
}
$this->toEnv($key, $value, $mode);
$this->toServer($key, $value, $mode);
$this->toPutenv($key, $value, $mode);
}
}

if ($mode & self::PUTENV) {
\putenv("$key=$value");
}
private function toEnv($key, $value, $mode)
{
if ($mode & self::ENV) {
$_ENV[$key] = $value;
}
}

if ($mode & self::SERVER) {
$_SERVER[$key] = $value;
}
private function toServer($key, $value, $mode)
{
if ($mode & self::SERVER) {
$_SERVER[$key] = $value;
}
}

private function toPutenv($key, $value, $mode)
{
if ($mode & self::PUTENV) {
\putenv("$key=$value");
}
}
}
21 changes: 15 additions & 6 deletions src/Retriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@ class Retriever
*/
public static function getEnv($key, $default = null, $filter = null, $options = null)
{
if (false !== $env = \getenv($key)) {
return static::prepareValue($env, $filter, $options);
}

if (isset($_ENV[$key])) {
return static::prepareValue($_ENV[$key], $filter, $options);
}

if (isset($_SERVER[$key])) {
$env = $_SERVER[$key];
} elseif (isset($_ENV[$key])) {
$env = $_ENV[$key];
} elseif (false === $env = \getenv($key)) {
// Default is not passed through filter!
return $default;
return static::prepareValue($_SERVER[$key], $filter, $options);
}

// Default is not passed through filter!
return $default;
}

protected static function prepareValue($env, $filter, $options)
{
static $special = [
'true' => true, 'false' => false, 'null' => null,
'TRUE' => true, 'FALSE' => false, 'NULL' => null,
Expand Down
4 changes: 2 additions & 2 deletions tests/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use Ahc\Env\Loader;

class LoaderTest extends \PHPUnit_Framework_TestCase
class LoaderTest extends \PHPUnit\Framework\TestCase
{
public function testLoadPutenv()
{
$loader = new Loader;

$_SERVER['x'] = $_ENV['x'] = 'X';

$loader->load(__DIR__ . '/stubs/test.env', false, 0);
$loader->load(__DIR__ . '/stubs/test.env', false);

$this->assertEquals('1', getenv('a'), 'Unquoted number');
$this->assertEquals('2', getenv('b'), 'Quoted number');
Expand Down
2 changes: 1 addition & 1 deletion tests/RetrieverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Ahc\Env\Retriever;

class RetrieverTest extends \PHPUnit_Framework_TestCase
class RetrieverTest extends \PHPUnit\Framework\TestCase
{
public function testLoad()
{
Expand Down

0 comments on commit ed56df9

Please sign in to comment.