PHPackages                             rancoud/environment - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. rancoud/environment

ActiveLibrary[Testing &amp; Quality](/categories/testing)

rancoud/environment
===================

Environment package

3.0.1(2mo ago)213.8k↓33.3%1MITPHPPHP &gt;=8.4.0CI passing

Since May 29Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/rancoud/Environment)[ Packagist](https://packagist.org/packages/rancoud/environment)[ RSS](/packages/rancoud-environment/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (28)Used By (1)

Environment Package
===================

[](#environment-package)

[![Packagist PHP Version Support](https://camo.githubusercontent.com/fd526fc759637e8fd58142a9625639d388c306627c68b0d8babe1921354e8265/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f72616e636f75642f656e7669726f6e6d656e74)](https://camo.githubusercontent.com/fd526fc759637e8fd58142a9625639d388c306627c68b0d8babe1921354e8265/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f72616e636f75642f656e7669726f6e6d656e74)[![Packagist Version](https://camo.githubusercontent.com/12301f72d8fc983ac2a020faa226dc2dc4575cf7b5bb9f4b77c04b67474750be/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72616e636f75642f656e7669726f6e6d656e74)](https://packagist.org/packages/rancoud/environment)[![Packagist Downloads](https://camo.githubusercontent.com/5f68c08991e670ec4e795555f79246593331a2b8979499891c793274e1f8e654/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72616e636f75642f656e7669726f6e6d656e74)](https://packagist.org/packages/rancoud/environment)[![Composer dependencies](https://camo.githubusercontent.com/aae95fbaa83bc6a3f4597f3a75da45ea46ec236fc324617f0e5a2f15e07fe750/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d302d627269676874677265656e)](https://github.com/rancoud/Environment/blob/master/composer.json)[![Test workflow](https://camo.githubusercontent.com/5743e3dfd16f5f80542b123bf2c811ec984d4f127bacd7e38ae2dbdeeb676a2d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f72616e636f75642f656e7669726f6e6d656e742f746573742e796d6c3f6272616e63683d6d6173746572)](https://github.com/rancoud/environment/actions/workflows/test.yml)[![Codecov](https://camo.githubusercontent.com/6afdc97049427891f013c1a8a72750a110f3cd9d0f2fc4d33212f70a6d4b3641/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f72616e636f75642f656e7669726f6e6d656e743f6c6f676f3d636f6465636f76)](https://codecov.io/gh/rancoud/environment)

Read Environment file (.env).
Can complete or override data from `getenv()` / `$_ENV` / `$_SERVER`

Installation
------------

[](#installation)

```
composer require rancoud/environment
```

.env File example
-----------------

[](#env-file-example)

```
# type of variables used
STRING=STRING
STRING_QUOTES=" STRING QUOTES "
INTEGER=9
FLOAT=9.0

BOOL_TRUE=TRUE
BOOL_FALSE=FALSE
NULL_VALUE=NULL

# variable in value
HOME=/user/www
CORE=$HOME/core
; $HOME won't be interpreted
USE_DOLLAR_IN_STRING="$HOME"

#comment line 1
;comment line 2

# import another env file use @ and the filename
@database.env

# multilines
RGPD="
i understand

    enough of email for \"me\"

thanks
"

```

How to use it?
--------------

[](#how-to-use-it)

Warning, call constructor will not load values, you can:

- use `load()` function
- use [any functions](#when-load-is-called) that automatically call `load()` inside

### Simple example

[](#simple-example)

```
// search .env file
$env = new Environment(__DIR__);
$values = $env->getAll();
$value = $env->get('a', 'defaultvalue');
```

### Check keys and values

[](#check-keys-and-values)

```
// check if a key exists
$env = new Environment(__DIR__);
$isExists = $env->exists('key1');
$isExists = $env->exists(['key1', 'key2']);

// check if value is set with allowed values
$isAllowed = $env->allowedValues('key1', ['value1', NULL, 'value2']);
```

### Complete and Override values

[](#complete-and-override-values)

Only type conversion will be done on those variables (no replacement with `$`).

You have 3 differents flags:

- Environment::GETENV
- Environment::ENV
- Environment::SERVER

Complete is for filling values belong to keys having empty string or no values.
Override is for erasing values belong to keys.
The treatment given by the flags is always in the same order:

1. `getenv()`
2. `$_ENV`
3. `$_SERVER`

You can also use 3 others flags.
Those will inject all keys and values found, your env file is not used for checkings keys.

- Environment::GETENV\_ALL
- Environment::ENV\_ALL
- Environment::SERVER\_ALL

```
$env = new Environment(__DIR__);

// complete with only getenv()
$env->complete(Environment::GETENV);

// complete with $_ENV then $_SERVER
$env->complete(Environment::SERVER | Environment::ENV);

// override with getenv() will erase values
$env->override(Environment::GETENV);
```

### Enable cache

[](#enable-cache)

The file cached will not contains informations from `getenv()` / `$_ENV` / `$_SERVER`

```
// force using cache (if not exist it will be created)
$env = new Environment(__DIR__);
$env->enableCache();
$values = $env->getAll();
```

### When load() is called?

[](#when-load-is-called)

For simplicity `load()` is automatically called when using thoses functions:

- get
- getAll
- exists
- complete

### Multiline

[](#multiline)

You can check what kind of endline it using, by default it's `PHP_EOL`
You can change it with for using ``

```
// force using cache (if not exist it will be created)
$env = new Environment(__DIR__);
$env->setEndline('');
```

### Include another .env

[](#include-another-env)

Inside .env file you can include another .env file with the `@` operator at the begining of the line

### Constructor variations

[](#constructor-variations)

```
// search .env file
$env = new Environment(__DIR__);

// search dev.env file
$env = new Environment(__DIR__, 'dev.env');

// search .env file in folders __DIR__ then '/usr'
$env = new Environment([__DIR__, '/usr']);

// search dev.env file in folders __DIR__ then '/usr'
$env = new Environment([__DIR__, '/usr'], 'dev.env');
```

Environment Constructor
-----------------------

[](#environment-constructor)

### Settings

[](#settings)

#### Mandatory

[](#mandatory)

ParameterTypeDescriptionfolderstring OR arrayfolder to seek .env file#### Optionnals

[](#optionnals)

ParameterTypeDefault valueDescriptionfilenamestring.envcustom name of .env file (don't forget to add file extension)Environment Methods
-------------------

[](#environment-methods)

### General Commands

[](#general-commands)

- load():void
- get(name: string, \[default: mixed = null\]): mixed|null
- getAll(): arrray
- exists(name: string|array): bool
- allowedValues(name: string, values: array): bool

### Cache File

[](#cache-file)

- enableCache(): void
- disableCache(): void
- flushCache(): void

### Env variables

[](#env-variables)

- complete(flags: Environment::GETENV | Environment::ENV | Environment::SERVER): void
- override(flags: Environment::GETENV | Environment::ENV | Environment::SERVER): void

### Multilines endline interpretation

[](#multilines-endline-interpretation)

- setEndline(endline: string): void
- getEndline(): string

How to Dev
----------

[](#how-to-dev)

`composer ci` for php-cs-fixer and phpunit and coverage
`composer lint` for php-cs-fixer
`composer test` for phpunit and coverage

###  Health Score

60

—

FairBetter than 99% of packages

Maintenance89

Actively maintained with recent releases

Popularity28

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity91

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 82.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~113 days

Recently: every ~122 days

Total

26

Last Release

64d ago

Major Versions

1.0.3 → 2.0.02020-06-10

2.2.1 → 3.0.02025-04-20

PHP version history (3 changes)1.0.0PHP &gt;=7.2.0

2.0.0PHP &gt;=7.4.0

3.0.0PHP &gt;=8.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/829a536bd4f71cadcd0266e272ccaf413e3fc9f2937248c9a2317ef0bf2d25ee?d=identicon)[rancoud](/maintainers/rancoud)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (243 commits)")[![rancoud](https://avatars.githubusercontent.com/u/1884186?v=4)](https://github.com/rancoud "rancoud (53 commits)")

---

Tags

composercoverageenvenvironmentpackagistphpphp7php8phpdotenvphpunit

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/rancoud-environment/health.svg)

```
[![Health](https://phpackages.com/badges/rancoud-environment/health.svg)](https://phpackages.com/packages/rancoud-environment)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[phpspec/phpspec

Specification-oriented BDD framework for PHP 7.1+

1.9k36.7M3.1k](/packages/phpspec-phpspec)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
