PHPackages                             dlapps/consul-php-envvar - 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. dlapps/consul-php-envvar

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

dlapps/consul-php-envvar
========================

Define missing environment variables from Consul KV and expose them in Symfony 3.2+ containers

1.1.1(9y ago)21172[1 PRs](https://github.com/dlapps/consul-php-envvar/pulls)MITPHPPHP &gt;=7.0

Since Feb 2Pushed 7y ago6 watchersCompare

[ Source](https://github.com/dlapps/consul-php-envvar)[ Packagist](https://packagist.org/packages/dlapps/consul-php-envvar)[ Docs](https://github.com/dlapps/consul-php-envvar)[ RSS](/packages/dlapps-consul-php-envvar/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (6)Used By (0)

Consul PHP Environment Variables
================================

[](#consul-php-environment-variables)

[![Latest Version on Packagist](https://camo.githubusercontent.com/0d93140a2e831bcdd5c36ed482afde9e27edd412bda3d967db4c75a11325f750/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646c617070732f636f6e73756c2d7068702d656e767661722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dlapps/consul-php-envvar)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/a180741b7353be780d66b85847a3b610d382469007068ee9fdcb733863b30e6b/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f646c617070732f636f6e73756c2d7068702d656e767661722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/dlapps/consul-php-envvar)[![Coverage Status](https://camo.githubusercontent.com/2e58250066a1673b87cbcb163c14644a1cbd34560c4d982851dc0b991ed66cf3/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f646c617070732f636f6e73756c2d7068702d656e767661722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/dlapps/consul-php-envvar/code-structure)[![Quality Score](https://camo.githubusercontent.com/5dcaa1e4cdc88f7edf56f4f7497a8f95211ae1c6ea532819ea7b1ebdbbcb20c3/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f646c617070732f636f6e73756c2d7068702d656e767661722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/dlapps/consul-php-envvar)[![SensioLabsInsight](https://camo.githubusercontent.com/8e4abce90ac53b7ee4f64855cecfd174cce7a7fb0d319fac70e282b7a197ce2f/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f35333335613863332d356639382d346261342d383763342d3534326263393130646361342f6d696e692e706e67)](https://insight.sensiolabs.com/projects/5335a8c3-5f98-4ba4-87c4-542bc910dca4)[![Total Downloads](https://camo.githubusercontent.com/a51a4f4c9ff36093e3b6ecd7644bf025465bb71f8b4dfb8cdd31d44385a9c2d7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646c617070732f636f6e73756c2d7068702d656e767661722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dlapps/consul-php-envvar)

The library enables developers to retrieve missing environment variables from a Consul KV store and make them available in a running PHP process, and also in a Symfony 3.2+ container.

The package supports PSR-4 autoloading, is PSR-2 compliant and has been well tested through automated tests. The library is also actively used within the Dreamlabs ecosystem.

Install
-------

[](#install)

Via Composer

```
$ composer require dlapps/consul-php-envvar
```

Usage
-----

[](#usage)

In order to be able to interact with the library, an instance of the `ConsulEnvManager` class is required. One can easily be obtained through the dedicated builder, like so:

```
$manager = (new ConsulEnvManagerBuilder())->build();
```

The same builder can be customised:

- By setting the overwrite flag to true, even if an environment variable has been previously defined, it will still be updated with the latest from Consul.
- By setting the URL of the Consul server.

```
$manager = (new ConsulEnvManagerBuilder())
    ->withOverwriteEvenIfDefined(true)
    ->withConsulServer('https://consul.example.com:9123')
    ->build();
```

Once a `ConsulEnvManager` instance has been obtained, the `ConsulEnvManager::getEnvVarsFromConsul($mappings)` method can be used to provide an array of environment variable mappings to Consul KV paths. An example can be seen below:

```
$manager = (new ConsulEnvManagerBuilder())->build();

$manager->getEnvVarsFromConsul([
    'MYSQL_HOST' => 'dreamlabs/mysql/host',
    'MYSQL_PORT' => 'dreamlabs/mysql/port',
    'MYSQL_DB'   => 'dreamlabs/mysql/db',
    'MYSQL_USER' => 'dreamlabs/mysql/user',
    'MYSQL_PASS' => 'dreamlabs/mysql/pass',
]);
```

After running the snippet above, the currently running PHP process will have access to the `MYSQL_HOST`, `MYSQL_PORT`, `MYSQL_DB`, `MYSQL_USER` and `MYSQL_PASS` environment variables.

In order to also register the environment variables within a Symfony 3.2+ container as default values, the `ConsulEnvManager::exposeEnvironmentIntoContainer($container, $mappings)` method can be used.

An example of `parameters.php` and `parameters.yml` can be found below:

```
declare(strict_types = 1);

$manager = (new \DL\ConsulPhpEnvVar\Builder\ConsulEnvManagerBuilder())->build();

$mappings = [
    'MYSQL_HOST'          => 'dreamlabs/mysql/host',
    'MYSQL_PORT'          => 'dreamlabs/mysql/port',
    'MYSQL_DB'            => 'dreamlabs/mysql/db',
    'MYSQL_USER'          => 'dreamlabs/mysql/user',
    'MYSQL_PASS'          => 'dreamlabs/mysql/pass',
];

$manager->getEnvVarsFromConsul($mappings);
$manager->exposeEnvironmentIntoContainer($container, $mappings);
```

```
parameters:
    database_host: '%env(MYSQL_HOST)%'
    database_port: '%env(MYSQL_PORT)%'
    database_name: '%env(MYSQL_DB)%'
    database_user: '%env(MYSQL_USER)%'
    database_password: '%env(MYSQL_PASS)%'
```

Testing
-------

[](#testing)

```
$ composer test
```

PSR-2 Compatibility
-------------------

[](#psr-2-compatibility)

```
$ composer check-styles
$ composer fix-styles
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email petre \[at\] dreamlabs.ro instead of using the issue tracker.

Credits
-------

[](#credits)

- [Petre Pătrașc](https://github.com/petrepatrasc)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 93.3% 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

5

Last Release

3383d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3b7d04eec9371ca46d3214b3a20d873e59992405eb1ccd8afde316d8e84e5353?d=identicon)[petrepatrasc](/maintainers/petrepatrasc)

---

Top Contributors

[![petrepatrasc](https://avatars.githubusercontent.com/u/1115084?v=4)](https://github.com/petrepatrasc "petrepatrasc (14 commits)")[![bangpound](https://avatars.githubusercontent.com/u/6731?v=4)](https://github.com/bangpound "bangpound (1 commits)")

---

Tags

consulenvironment-variablesphpphpenvironmentvariableconsul

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/dlapps-consul-php-envvar/health.svg)

```
[![Health](https://phpackages.com/badges/dlapps-consul-php-envvar/health.svg)](https://phpackages.com/packages/dlapps-consul-php-envvar)
```

###  Alternatives

[leedavis81/vent

PHP variable events.

814.6k](/packages/leedavis81-vent)

PHPackages © 2026

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