You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Environment variable loader and retriever for PHP.
4
11
- 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).
5
13
6
14
## Installation
7
15
```
8
16
composer require adhocore/env
9
17
```
10
18
11
19
## Usage
20
+
21
+
### Loading
22
+
12
23
```php
13
24
use Ahc\Env\Loader;
14
-
use Ahc\Env\Retriever;
15
25
16
26
// Load env variables from .env file to `putenv` by default:
17
27
(new Loader)->load('/project/root/.env');
@@ -22,11 +32,17 @@ use Ahc\Env\Retriever;
22
32
// Load to $_SERVER global:
23
33
(new Loader)->load('/project/root/.env', true, Loader::SERVER);
24
34
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);
27
37
28
38
// Load to all targets:
29
39
(new Loader)->load('/project/root/.env', true, Loader::ALL);
40
+
```
41
+
42
+
### Retrieving
43
+
44
+
```php
45
+
use Ahc\Env\Retriever;
30
46
31
47
// Retrieve:
32
48
echo Retriever::getEnv($key);
@@ -43,3 +59,18 @@ echo env('THE_KEY');
43
59
44
60
See [filter_var](http://php.net/filter_var) for more on sanitizing/filtering values!
45
61
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.
0 commit comments