PHPackages                             jan-di/config - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. jan-di/config

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

jan-di/config
=============

Library to load, validate and cache configuration from environment variables and .env files

0.3.0(5y ago)189[1 issues](https://github.com/jan-di/php-config/issues)MITPHPPHP &gt;=7.4

Since Jan 5Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jan-di/php-config)[ Packagist](https://packagist.org/packages/jan-di/config)[ RSS](/packages/jan-di-config/feed)WikiDiscussions main Synced 6d ago

READMEChangelog (3)Dependencies (9)Versions (4)Used By (0)

jan-di/config
=============

[](#jan-diconfig)

 [![](https://camo.githubusercontent.com/28c8d4d4dfe2c2ef8ea57a3c073726e24e38fc569bd260f4aa83be12d2c4c8fc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a616e2d64692f636f6e666967)](https://packagist.org/packages/jan-di/config) [![](https://camo.githubusercontent.com/4c067a3bd2881f650e8ef008a319d28ea892a9074cd173d5b2a7d6d85b89d9dd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f73746172732f6a616e2d64692f636f6e666967)](https://packagist.org/packages/jan-di/config) [![](https://camo.githubusercontent.com/55b35d019850065320d9af19112b838d43e1f004b1fdd464b1bee032e14f427a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6a616e2d64692f636f6e666967)](https://packagist.org/packages/jan-di/config) [![](https://camo.githubusercontent.com/8e970facc9743dc1b7754db17547ffa00d2544a83461ea2243ae6fbf056fbfa6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a616e2d64692f636f6e666967)](https://github.com/jan-di/php-config/blob/main/LICENSE)

Library to load, validate and cache configuration from environment variables and .env files

Install
-------

[](#install)

Install via composer:

`composer require jan-di/config`

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

When creating the Config Builder, specify all definitions for the config values. You can use certain type specific constraints to validate the values later. By default, values are read from the environment variables. Optionally you can enable a Dotenv Adapter to add variables from a .env file to the environment before building the config.

```
use Dotenv\Dotenv;
use Jandi\Config\ConfigBuilder;
use Jandi\Config\Dotenv\VlucasDotenvAdapter;
use Jandi\Config\Entry\StringEntry;

// define all config entries via a fluent API:
$configBuilder = new ConfigBuilder([
    (new StringEntry('APP_ENV', 'development')),
    (new StringEntry('APP_TOKEN'))->setMinLength(20)
]);

// optional: Add a Dotenv loader to load .env files in environment before building config
$dotenv = Dotenv::createImmutable(__DIR__, '');
$configBuilder->enableDotEnv(new VlucasDotenvAdapter($dotenv));

// build config array from environment variables
// The values will be fetched validated against the rules from the entries.
$config = $configBuilder->build();
```

### Configuration Cache

[](#configuration-cache)

To improve performance and prevent that .env files are parsed on every request, the config values should be cached. This will create a compiled PHP file that holds all fetched values. This file should be cached by Opcache to optimize further.

```
use Dotenv\Dotenv;
use Jandi\Config\ConfigBuilder;
use Jandi\Config\Dotenv\VlucasDotenvAdapter;
use Jandi\Config\Entry\StringEntry;

// build config, see above
$configBuilder = new ConfigBuilder([
    (new StringEntry('APP_ENV'))->setDefaultValue('development'),
    (new StringEntry('APP_TOKEN'))->setMinLength(20)
]);
$configBuilder
    ->enableCaching('/path/to/cache/file') // Activate caching
    ->enableDotEnv(new VlucasDotenvAdapter(Dotenv::createImmutable(__DIR__, '.env')));

// Since caching is enabled, the builder will check if the cache file exists.
// if the cache file exists, no values are read from .env or the real environment.
$config = $configBuilder->build();

// The cache file is not created automatically. Instead you have to provide a condition,
// when the cache has to be written. Often this is related to the values inside the config.
if (!$config->isCached() && $config->getValue('APP_ENV') === 'production') {
    $configBuilder->dumpCache($config);
}
```

### Validating values and default values

[](#validating-values-and-default-values)

Each Entrytype has its own set of validating rules that can be set using a fluent API. A specified value must comply to all rules, otherwise the config is not built. Note that the default value is also validated against the ruleset to ensure that its also a valid value. Every entry that does not have a default value, must be specified. Otherwise the container won't get built successfully.

```
(new StringEntry('STRING', 'value1'))
    ->setMinLength(5)
    ->setMaxLength(10)
    ->setAllowedValues(['value1','value2'])
    ->setRegexPattern('/value.*/')

(new BoolEntry('BOOL', 'true'));

(new IntEntry('INT', '3'))
    ->setLowerLimit(0)
    ->setUpperLimit(8);

(new FloatEntry('FLOAT', '5.5'))
    ->setLowerLimit(3.4)
    ->setUpperLimit(7.8);
```

### Catch Errors

[](#catch-errors)

The container builder is designed to stop if atleast one variable is missing or has an invalid value. This assures that a successfully built config always have all needed values. The BuildException has a few methods to show summaries of all missing/invalid values in different formats.

```
try {
    $builder->build();
} catch(BuildExceptionTest $e) {
    echo $e->getTextSummary();
    exit;
}

// There where 2 error/s while building configuration:
//
// APP_ENV [string] Value is invalid: not allowed. Allowed values: abce.
// APP_TOKEN [string] Variable is missing and has no default value.
```

### Export to Array

[](#export-to-array)

You can export the values/default values to a simple array to process it further.

```
$config = $configBuilder->build();

$values = $config->exportValues();
$defaultValues = $config->exportDefaultValues();

// returns something like: ['APP_ENV' => 'development']
```

### Supported DotEnv adapters

[](#supported-dotenv-adapters)

Currently, the following Dotenv Libraries are supported out of the box:

- [vlucas/phpdotenv](https://github.com/vlucas/phpdotenv)
- [symfony/dotenv](https://github.com/symfony/dotenv)
- [josegonzalez/dotenv](https://github.com/josegonzalez/php-dotenv)

Alternatively you can provide you own by implementing [AdapterInterface](https://github.com/jan-di/php-config/blob/main/src/Dotenv/AdapterInterface.php)

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

3

Last Release

1957d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/23323185?v=4)[Jan Dittrich](/maintainers/jan-di)[@jan-di](https://github.com/jan-di)

---

Top Contributors

[![jan-di](https://avatars.githubusercontent.com/u/23323185?v=4)](https://github.com/jan-di "jan-di (28 commits)")

---

Tags

configurationdotenvenvironment-variablesphp

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jan-di-config/health.svg)

```
[![Health](https://phpackages.com/badges/jan-di-config/health.svg)](https://phpackages.com/packages/jan-di-config)
```

###  Alternatives

[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[jakubkulhan/chrome-devtools-protocol

Chrome Devtools Protocol client for PHP

183967.6k3](/packages/jakubkulhan-chrome-devtools-protocol)[brumann/polyfill-unserialize

Backports unserialize options introduced in PHP 7.0 to older PHP versions.

343.8M1](/packages/brumann-polyfill-unserialize)[cerbero/laravel-enum

Laravel package to supercharge enum functionalities.

18989.6k](/packages/cerbero-laravel-enum)[keyvanakbary/slugifier

A full-featured, simple, clean and pure functional implementation for creating slugs

68187.9k4](/packages/keyvanakbary-slugifier)[genericmilk/docudoodle

Generate documentation for your Laravel application using OpenAI

15120.5k](/packages/genericmilk-docudoodle)

PHPackages © 2026

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