Skip to content

Commit

Permalink
Merge pull request #6 from jimdelois/feature-extend-with-prefix
Browse files Browse the repository at this point in the history
Adding Ability to Extend Config with Additional Prefix Filtration
  • Loading branch information
jimdelois committed Jun 10, 2016
2 parents 5ddcc9a + ee51c54 commit 7b134ce
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,26 @@ $config->get('PHPFPM_BUFFER_COUNT'); // throws InvalidKeyException
```
This becomes particularly useful when registering and referencing various configurations within, say, a container and services within the application are only interested in certain variables.

The `withPrefix` method will take a given, "base" Configuration object and return a new one from it, applying another layer of filtering by prefix on from the keys in the base.

```php
$container['config.debug'] = function () {
return new \Improv\Configuration($_ENV, 'APP_DEBUG_');
$container['config.app'] = function () {
return new \Improv\Configuration($_ENV, 'APP_');
};

$container['config.debug'] = function (Container $container) {
$base = $container->get('config.app');
return $base->withPrefix('DEBUG_');
};

$container['config.db'] = function () {
return new \Improv\Configuration($_ENV, 'APP_DB_');
$container['config.db'] = function (Container $container) {
$base = $container->get('config.app');
return $base->withPrefix('DB_');
};

$container['config.api'] = function () {
return new \Improv\Configuration($_ENV, 'APP_SOMESERVICE_API_');
$container['config.api'] = function (Container $container) {
$base = $container->get('config.app');
return $base->withPrefix('SOMESERVICE_API_');
};

// ... //
Expand Down
28 changes: 28 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class Configuration
*/
private $config;

/**
* @var string
*/
private $prefix;

/**
* @var MapperFactory
*/
Expand All @@ -32,6 +37,7 @@ public function __construct(array $config, $prefix = '', MapperFactory $mapper_f
{

$this->config = $config;
$this->prefix = $prefix;
$this->factory_mapper = ($mapper_factory) ?: new MapperFactory();

if (!$prefix) {
Expand Down Expand Up @@ -109,6 +115,28 @@ public function get($key)
return $this->config[$key];
}

/**
* Generates a new Configuration object from the current one, applying another layer of prefix filtration.
*
* @param string $prefix
*
* @return Configuration
*/
public function withPrefix($prefix)
{
$config = new self($this->config, $prefix, $this->factory_mapper);
$config->mappers = [];

foreach ($this->mappers as $key => $mapper) {
if (strpos($key, $prefix) !== 0) {
continue;
}
$config->mappers[str_replace($prefix, '', $key)] = $mapper;
}

return $config;
}

/**
* Internal helper method to execute key existence checking
*
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,40 @@ public function mappingValues()
self::assertSame('Mapped Value 2', $sut->get('KEY2'));
self::assertSame('Mapped Value 3', $sut->get('KEY3'));
}

/**
* @test
*/
public function withPrefix()
{
$data = [
'OUTER_PREFIX_ONE' => 'One',
'OUTER_PREFIX_TWO' => 'Two',
'OUTER_THING' => 'Thing',
];

$original = new Configuration($data, 'OUTER_');

$original->map('PREFIX_TWO')->using(function ($val) {
return strrev(strtoupper($val));
});

$original->map('THING')->using(function ($val) {
return strrev(strtoupper($val));
});

self::assertSame('GNIHT', $original->get('THING'));

$sut = $original->withPrefix('PREFIX_');

self::assertInstanceOf(Configuration::class, $sut);

self::assertSame('One', $sut->get('ONE'));
self::assertSame('OWT', $sut->get('TWO'));

$this->expectException(InvalidKeyException::class);
$this->expectExceptionMessage('No such key "THING" exists in Config.');

$sut->get('THING');
}
}

0 comments on commit 7b134ce

Please sign in to comment.