Consul PHP Environment Variables
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
![Total Downloads][ico-downloads], (*1)
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., (*2)
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., (*3)
Install
Via Composer, (*4)
``` bash
$ composer require dlapps/consul-php-envvar, (*5)
## 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:
``` php
$manager = (new ConsulEnvManagerBuilder())->build();
The same builder can be customised:, (*6)
- 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.
``` php
$manager = (new ConsulEnvManagerBuilder())
->withOverwriteEvenIfDefined(true)
->withConsulServer('https://consul.example.com:9123')
->build();, (*7)
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:
``` php
$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., (*8)
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., (*9)
An example of parameters.php and parameters.yml can be found below:, (*10)
``` php
declare(strict_types = 1);, (*11)
$manager = (new \DL\ConsulPhpEnvVar\Builder\ConsulEnvManagerBuilder())->build();, (*12)
$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',
];, (*13)
$manager->getEnvVarsFromConsul($mappings);
$manager->exposeEnvironmentIntoContainer($container, $mappings);, (*14)
``` yaml
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
``` bash
$ composer test, (*15)
## PSR-2 Compatibility
``` bash
$ composer check-styles
$ composer fix-styles
Contributing
Please see CONTRIBUTING and CONDUCT for details., (*16)
Security
If you discover any security related issues, please email petre [at] dreamlabs.ro instead of using the issue tracker., (*17)
Credits
License
The MIT License (MIT). Please see License File for more information., (*18)