dlapps/consul-php-envvar

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

1.1.1 2017-02-02 11:14 UTC

This package is not auto-updated.

Last update: 2024-12-22 10:20:22 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score SensioLabsInsight Total Downloads

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

Via Composer

$ composer require dlapps/consul-php-envvar

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

$ composer test

PSR-2 Compatibility

$ composer check-styles
$ composer fix-styles

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

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

Credits

License

The MIT License (MIT). Please see License File for more information.