Skip to content

Commit

Permalink
Add support of environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Feb 19, 2017
1 parent 7ab02bc commit 8929f2b
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 15 deletions.
134 changes: 134 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Fxp\Composer\AssetPlugin\Config;

use Fxp\Composer\AssetPlugin\Exception\InvalidArgumentException;

/**
* Helper of package config.
*
Expand All @@ -23,6 +25,11 @@ final class Config
*/
private $config;

/**
* @var array
*/
private $cacheEnv = array();

/**
* Constructor.
*
Expand Down Expand Up @@ -56,8 +63,135 @@ public function getArray($key, array $default = array())
*/
public function get($key, $default = null)
{
if (array_key_exists($key, $this->cacheEnv)) {
return $this->cacheEnv[$key];
} else {
$envKey = $this->convertEnvKey($key);
$envValue = getenv($envKey);

if (false !== $envValue) {
return $this->cacheEnv[$key] = $this->convertEnvValue($envValue, $envKey);
}
}

return array_key_exists($key, $this->config)
? $this->config[$key]
: $default;
}

/**
* Convert the config key into environment variable.
*
* @param string $key The config key
*
* @return string
*/
private function convertEnvKey($key)
{
return 'FXP_ASSET__'.strtoupper(str_replace('-', '_', $key));
}

/**
* Convert the value of environment variable into php variable.
*
* @param string $value The value of environment variable
* @param string $environmentVariable The environment variable name
*
* @return string|bool|int|array
*/
private function convertEnvValue($value, $environmentVariable)
{
$value = trim(trim(trim($value, '\''), '"'));

if ($this->isBoolean($value)) {
$value = $this->convertBoolean($value);
} elseif ($this->isInteger($value)) {
$value = $this->convertInteger($value);
} elseif ($this->isJson($value)) {
$value = $this->convertJson($value, $environmentVariable);
}

return $value;
}

/**
* Check if the value of environment variable is a boolean.
*
* @param string $value The value of environment variable
*
* @return bool
*/
private function isBoolean($value)
{
$value = strtolower($value);

return in_array($value, array('true', 'false', '1', '0', 'yes', 'no', 'y', 'n'), true);
}

/**
* Convert the value of environment variable into a boolean.
*
* @param string $value The value of environment variable
*
* @return bool
*/
private function convertBoolean($value)
{
return in_array($value, array('true', '1', 'yes', 'y'), true);
}

/**
* Check if the value of environment variable is a integer.
*
* @param string $value The value of environment variable
*
* @return bool
*/
private function isInteger($value)
{
return ctype_digit(trim($value, '-'));
}

/**
* Convert the value of environment variable into a integer.
*
* @param string $value The value of environment variable
*
* @return bool
*/
private function convertInteger($value)
{
return (int) $value;
}

/**
* Check if the value of environment variable is a string JSON.
*
* @param string $value The value of environment variable
*
* @return bool
*/
private function isJson($value)
{
return 0 === strpos($value, '{') || 0 === strpos($value, '[');
}

/**
* Convert the value of environment variable into a json array.
*
* @param string $value The value of environment variable
* @param string $environmentVariable The environment variable name
*
* @return array
*/
private function convertJson($value, $environmentVariable)
{
$value = json_decode($value, true);

if (json_last_error()) {
throw new InvalidArgumentException(sprintf('The "%s" environment variable isn\'t a valid JSON', $environmentVariable));
}

return $value;
}
}
34 changes: 30 additions & 4 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,37 @@ in the file of your choice:
> But you can use the command `composer global config -e` to edit the global `composer.json`
file with your text editor.

### Define the config in a environment variable

You can define each option (`config.fxp-asset.*`) directly in the PHP environment variables. For
this, all variables will start with `FXP_ASSET__` and uppercased, and each `-` will replaced by `_`.

The accepted value types are:

- string
- boolean
- integer
- JSON array or object

**Example:**
```json
{
"config": {
"fxp-asset": {
"pattern-skip-version": "(-patch)"
}
}
}
```

Can be overrided by `FXP_ASSET__PATTERN_SKIP_VERSION="(-build)"` environment variable.

### Config priority order

The config values are retrieved in priority in:

1. the project `composer.json` file
2. the global `<COMPOSER_HOME>/config.json` file
3. the global `<COMPOSER_HOME>/composer.json` file
4. the deprecated config `extra.asset-*` of the project `composer.json` file
1. the environment variables starting with `FXP_ASSET__`
2. the project `composer.json` file
3. the global `<COMPOSER_HOME>/config.json` file
4. the global `<COMPOSER_HOME>/composer.json` file
5. the deprecated config `extra.asset-*` of the project `composer.json` file
70 changes: 59 additions & 11 deletions Tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,36 @@ protected function setUp()
public function getDataForGetConfig()
{
return array(
array('foo', 42, 42),
array('bar', 'foo', 'empty'),
array('baz', false, true),
array('repositories', 42, 0),
array('global-composer-foo', 90, 0),
array('global-composer-bar', 70, 0),
array('global-config-foo', 23, 0),
array('foo', 42, 42),
array('bar', 'foo', 'empty'),
array('baz', false, true),
array('repositories', 42, 0),
array('global-composer-foo', 90, 0),
array('global-composer-bar', 70, 0),
array('global-config-foo', 23, 0),
array('env-boolean', false, true, 'FXP_ASSET__ENV_BOOLEAN=false'),
array('env-integer', -32, 0, 'FXP_ASSET__ENV_INTEGER=-32'),
array('env-json', array('foo' => 'bar'), array(), 'FXP_ASSET__ENV_JSON="{"foo": "bar"}"'),
array('env-json-array', array(array('foo' => 'bar')), array(), 'FXP_ASSET__ENV_JSON_ARRAY="[{"foo": "bar"}]"'),
array('env-string', 'baz', 'foo', 'FXP_ASSET__ENV_STRING=baz'),
);
}

/**
* @dataProvider getDataForGetConfig
*
* @param string $key The key
* @param mixed $expected The expected value
* @param mixed|null $default The default value
* @param string $key The key
* @param mixed $expected The expected value
* @param mixed|null $default The default value
* @param string|null $env The env variable
*/
public function testGetConfig($key, $expected, $default = null)
public function testGetConfig($key, $expected, $default = null, $env = null)
{
// add env variables
if (null !== $env) {
putenv($env);
}

$globalPath = realpath(__DIR__.'/../Fixtures/package/global');
$this->composerConfig->expects($this->any())
->method('has')
Expand All @@ -106,6 +117,7 @@ public function testGetConfig($key, $expected, $default = null)
'fxp-asset' => array(
'bar' => 'foo',
'baz' => false,
'env-foo' => 55,
),
));

Expand All @@ -123,10 +135,46 @@ public function testGetConfig($key, $expected, $default = null)
}

$config = ConfigBuilder::build($this->composer, $this->io);
$value = $config->get($key, $default);

// remove env variables
if (null !== $env) {
$envKey = substr($env, 0, strpos($env, '='));
putenv($envKey);
$this->assertFalse(getenv($envKey));
}

$this->assertSame($expected, $value);
// test cache
$this->assertSame($expected, $config->get($key, $default));
}

/**
* @expectedException \Fxp\Composer\AssetPlugin\Exception\InvalidArgumentException
* @expectedExceptionMessage The "FXP_ASSET__ENV_JSON" environment variable isn't a valid JSON
*/
public function testGetEnvConfigWithInvalidJson()
{
putenv('FXP_ASSET__ENV_JSON="{"foo"}"');
$config = ConfigBuilder::build($this->composer, $this->io);
$ex = null;

try {
$config->get('env-json');
} catch (\Exception $e) {
$ex = $e;
}

putenv('FXP_ASSET__ENV_JSON');
$this->assertFalse(getenv('FXP_ASSET__ENV_JSON'));

if (null === $ex) {
throw new \Exception('The expected exception was not thrown');
}

throw $ex;
}

public function testValidateConfig()
{
$deprecated = array(
Expand Down

0 comments on commit 8929f2b

Please sign in to comment.