Commit 07a50e90 authored by fago's avatar fago

Initial version.

parents
name: Services Environment Variable Parameters
type: module
description: Allows setting service parameters by environment variables.
core: 8.x
# Services Environment Variable Parameters
initially developed by drunomics GmbH <hello@drunomics.com>
## Usage
* Install as usual.
* Set environment variables using the pattern:
DRUPAL_SERVICE_{ variable }={ value }
The variable name following the same rules:
* Casing is kept as is
* '__' is replaced by dots (".").
* '___' is used for setting nested array structures
## Usage example - configuring CORS:
For example the following environment variables would set Drupal's CORS values:
DRUPAL_SERVICE_cors__config___enabled=1
DRUPAL_SERVICE_cors__config___allowedOrigins___0=http://www.example.com
<?php
namespace Drupal\services_env_parameter;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
/**
* Implements a service provider.
*/
class ServicesEnvParameterServiceProvider extends ServiceProviderBase {
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container) {
foreach ($_ENV as $key => $value) {
if (strpos($key, 'DRUPAL_SERVICE_') === 0) {
// Remove DRUPAL_SERVICE_ prefix.
$key = substr($key, strlen('DRUPAL_SERVICE_'));
$key = str_replace('__', '.', $key);
// Split by '____' which is '._' now.
$parts = explode('._', $key);
if ($container->getParameterBag()->has($parts[0])) {
$key = $parts[0];
unset($parts[0]);
Please register or sign in to reply
$this->applyParamterValue($container, $key, $parts, $value);
}
}
}
}
/**
* Internal helper for applying parameter values.
*/
private function applyParamterValue(ContainerBuilder $container, $key, $sub_keys, $value) {
if (!$sub_keys) {
$container->setParameter($key, $value);
}
else {
// Take care of setting nested parameters correctly.
$parameter = $container->getParameter($key);
NestedArray::setValue($parameter, $sub_keys, $value);
$container->setParameter($key, $parameter);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment