Skip to content

Commit ed56df9

Browse files
authored
Merge pull request #2 from adhocore/develop
Develop
2 parents 50fca59 + ad9538f commit ed56df9

7 files changed

+98
-32
lines changed

.travis.yml

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
sudo: required
2-
31
cache:
42
directories:
53
- $HOME/.composer/cache
@@ -8,15 +6,28 @@ git:
86
depth: 1
97

108
language: php
9+
1110
php:
1211
- 5.4
1312
- 5.5
1413
- 5.6
1514
- 7.0
1615
- 7.1
16+
- 7.2
17+
- nightly
18+
19+
matrix:
20+
allow_failures:
21+
- php: nightly
1722

1823
install:
1924
- composer install --prefer-dist
2025

26+
before_script:
27+
- for P in src tests; do find $P -type f -name '*.php' -exec php -l {} \;; done
28+
2129
script:
22-
- vendor/bin/phpunit --coverage-text
30+
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml
31+
32+
after_success:
33+
- bash <(curl -s https://codecov.io/bash)

composer.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
"php": ">=5.4"
3030
},
3131
"require-dev": {
32-
"phpunit/phpunit": "^4.8"
32+
"phpunit/phpunit": "^4.8 || ^5.7 || ^6.5"
33+
},
34+
"config": {
35+
"optimize-autoloader": true,
36+
"preferred-install": {
37+
"*": "dist"
38+
}
3339
}
3440
}

readme.md

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
## adhocore/env [![build status](https://travis-ci.org/adhocore/env.svg?branch=master)](https://travis-ci.org/adhocore/env)
1+
## adhocore/env
2+
3+
[![Latest Version](https://img.shields.io/github/release/adhocore/env.svg?style=flat-square)](https://github.com/adhocore/env/releases)
4+
[![Travis Build](https://travis-ci.org/adhocore/env.svg?branch=master)](https://travis-ci.org/adhocore/env?branch=master)
5+
[![Scrutinizer CI](https://img.shields.io/scrutinizer/g/adhocore/env.svg?style=flat-square)](https://scrutinizer-ci.com/g/adhocore/env/?branch=master)
6+
[![Codecov branch](https://img.shields.io/codecov/c/github/adhocore/env/master.svg?style=flat-square)](https://codecov.io/gh/adhocore/env)
7+
[![StyleCI](https://styleci.io/repos/107715208/shield)](https://styleci.io/repos/107715208)
8+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
29

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

614
## Installation
715
```
816
composer require adhocore/env
917
```
1018

1119
## Usage
20+
21+
### Loading
22+
1223
```php
1324
use Ahc\Env\Loader;
14-
use Ahc\Env\Retriever;
1525

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

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

2838
// Load to all targets:
2939
(new Loader)->load('/project/root/.env', true, Loader::ALL);
40+
```
41+
42+
### Retrieving
43+
44+
```php
45+
use Ahc\Env\Retriever;
3046

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

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

62+
### Consideration
63+
64+
By default this library only loads env to `putenv()`.
65+
Be cautious exposing confidential credentials into `$_ENV` and `$_SERVER` which bug/error catchers may log.
66+
67+
Although this libray is already fast enough, in production you might want to boost performance a little by loading if only required:
68+
69+
```php
70+
if (!getenv('<LAST_ENV_APP_SHOULD_BE_AWARE_OF>')) {
71+
// Override false :)
72+
(new Loader)->load('/project/root/.env', false);
73+
}
74+
```
75+
76+
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.

src/Loader.php

+24-15
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,40 @@ public function load($file, $override = false, $mode = self::PUTENV)
7070
* @param bool $override
7171
* @param int $mode
7272
*/
73-
protected function setEnv(array $vars, $override, $mode)
73+
protected function setEnv(array $vars, $override, $mode = self::PUTENV)
7474
{
75-
if ($mode < self::PUTENV || $mode > self::ALL) {
76-
$mode = self::PUTENV;
77-
}
78-
79-
$default = microtime(1);
75+
$default = \microtime(1);
8076

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

87-
if ($mode & self::ENV) {
88-
$_ENV[$key] = $value;
89-
}
83+
$this->toEnv($key, $value, $mode);
84+
$this->toServer($key, $value, $mode);
85+
$this->toPutenv($key, $value, $mode);
86+
}
87+
}
9088

91-
if ($mode & self::PUTENV) {
92-
\putenv("$key=$value");
93-
}
89+
private function toEnv($key, $value, $mode)
90+
{
91+
if ($mode & self::ENV) {
92+
$_ENV[$key] = $value;
93+
}
94+
}
9495

95-
if ($mode & self::SERVER) {
96-
$_SERVER[$key] = $value;
97-
}
96+
private function toServer($key, $value, $mode)
97+
{
98+
if ($mode & self::SERVER) {
99+
$_SERVER[$key] = $value;
100+
}
101+
}
102+
103+
private function toPutenv($key, $value, $mode)
104+
{
105+
if ($mode & self::PUTENV) {
106+
\putenv("$key=$value");
98107
}
99108
}
100109
}

src/Retriever.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,24 @@ class Retriever
2424
*/
2525
public static function getEnv($key, $default = null, $filter = null, $options = null)
2626
{
27+
if (false !== $env = \getenv($key)) {
28+
return static::prepareValue($env, $filter, $options);
29+
}
30+
31+
if (isset($_ENV[$key])) {
32+
return static::prepareValue($_ENV[$key], $filter, $options);
33+
}
34+
2735
if (isset($_SERVER[$key])) {
28-
$env = $_SERVER[$key];
29-
} elseif (isset($_ENV[$key])) {
30-
$env = $_ENV[$key];
31-
} elseif (false === $env = \getenv($key)) {
32-
// Default is not passed through filter!
33-
return $default;
36+
return static::prepareValue($_SERVER[$key], $filter, $options);
3437
}
3538

39+
// Default is not passed through filter!
40+
return $default;
41+
}
42+
43+
protected static function prepareValue($env, $filter, $options)
44+
{
3645
static $special = [
3746
'true' => true, 'false' => false, 'null' => null,
3847
'TRUE' => true, 'FALSE' => false, 'NULL' => null,

tests/LoaderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use Ahc\Env\Loader;
66

7-
class LoaderTest extends \PHPUnit_Framework_TestCase
7+
class LoaderTest extends \PHPUnit\Framework\TestCase
88
{
99
public function testLoadPutenv()
1010
{
1111
$loader = new Loader;
1212

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

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

1717
$this->assertEquals('1', getenv('a'), 'Unquoted number');
1818
$this->assertEquals('2', getenv('b'), 'Quoted number');

tests/RetrieverTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Ahc\Env\Retriever;
66

7-
class RetrieverTest extends \PHPUnit_Framework_TestCase
7+
class RetrieverTest extends \PHPUnit\Framework\TestCase
88
{
99
public function testLoad()
1010
{

0 commit comments

Comments
 (0)