Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ddrv/php-env

Read variables from environment or .env file
https://github.com/ddrv/php-env

env environment environment-variables library php

Last synced: about 2 months ago
JSON representation

Read variables from environment or .env file

Awesome Lists containing this project

README

        

# ddrv/env

> Read variables from environment or .env file

# Install

```text
composer require ddrv/env:^2.0
```

# Usage

```text
# /path/to/project/.env

APP_VAR_3=value3
```

```php
'value1',
* 'APP_VAR_2' => 'value2',
* ];
*/

$env = new Env(
new EnvVariableProvider(),
new FileVariableProvider('/path/to/project/.env'),
);

$env->get('APP_VAR_1'); // returns 'value1'
$env->get('APP_VAR_2'); // returns 'value2'
$env->get('APP_VAR_3'); // returns 'value3'
$env->get('APP_VAR_4'); // returns null
$env->get('APP_VAR_5', 'defaultValue'); // returns 'defaultValue'
```

## Prefixes

```php
'value1',
* 'APP_VAR_2' => 'value2',
* ];
*/

$env = new Env(
new PrefixedVariableProvider(new EnvVariableProvider(), 'APP_'),
new PrefixedVariableProvider(new FileVariableProvider('/path/to/project/.env'), 'APP_'),
);

$env->get('VAR_1'); // returns 'value1'
$env->get('VAR_2'); // returns 'value2'
$env->get('VAR_3'); // returns 'value3'
$env->get('VAR_4'); // returns null
$env->get('VAR_5', 'defaultValue'); // returns 'defaultValue'
```