Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ $aggregator = new ConfigAggregator(
'parameter_usage' => '%foo%',
'parameter_name' => '%%foo%%',
'recursive_parameter_usage' => '%bar.baz%',
'parameterized_parameter_usage' => '%bar.quux%',
]),
],
null,
Expand All @@ -26,6 +27,7 @@ $aggregator = new ConfigAggregator(
'foo' => 'bar',
'bar' => [
'baz' => 'qoo',
'quux' => '%foo%',
],
]),
]
Expand All @@ -37,24 +39,30 @@ var_dump($aggregator->getMergedConfig());
Result:

```php
array(3) {
array(5) {
'parameter_usage' =>
string(3) "bar"
'parameter_name' =>
string(5) "%foo%"
'recursive_parameter_usage' =>
string(3) "qoo"
'parameterized_parameter_usage' =>
string(3) "bar"
'parameters' =>
array(3) {
'foo' =>
string(3) "bar"
array(4) {
'foo' =>
string(3) "bar"
'bar' =>
array(1) {
array(2) {
'baz' =>
string(3) "qoo"
'quux' =>
string(3) "bar"
}
'bar.baz' =>
string(3) "qoo"
'bar.quux' =>
string(3) "bar"
}
}
```
Expand Down
20 changes: 15 additions & 5 deletions docs/book/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Parameters may be defined as nested associative arrays as well; in such cases, a
`.` character references an additional layer of hierarchy to dereference:
`%foo.bar%` refers to the paramter found at `'foo' => [ 'bar' => 'value' ]`.

You can use parameters which reference other parameters as well.

If you wish to use a literal `%name%` within your configuration, you **must**
double-escape the percentage signs: `%%name%%`. Failure to do so will result in
an exception when post-processing the configuration.
Expand All @@ -32,6 +34,7 @@ $aggregator = new ConfigAggregator(
'parameter_usage' => '%foo%',
'parameter_name' => '%%foo%%',
'recursive_parameter_usage' => '%bar.baz%',
'parameterized_parameter_usage' => '%bar.quux%',
]),
],
null,
Expand All @@ -40,6 +43,7 @@ $aggregator = new ConfigAggregator(
'foo' => 'bar',
'bar' => [
'baz' => 'qoo',
'quux' => '%foo%',
],
]),
]
Expand All @@ -51,24 +55,30 @@ var_dump($aggregator->getMergedConfig());
The result of the above will be:

```php
array(3) {
array(5) {
'parameter_usage' =>
string(3) "bar"
'parameter_name' =>
string(5) "%foo%"
'recursive_parameter_usage' =>
string(3) "qoo"
'parameterized_parameter_usage' =>
string(3) "bar"
'parameters' =>
array(3) {
'foo' =>
string(3) "bar"
array(4) {
'foo' =>
string(3) "bar"
'bar' =>
array(1) {
array(2) {
'baz' =>
string(3) "qoo"
'quux' =>
string(3) "bar"
}
'bar.baz' =>
string(3) "qoo"
'bar.quux' =>
string(3) "bar"
}
}
```
25 changes: 15 additions & 10 deletions src/ParameterPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class ParameterPostProcessor
{
/**
* @var ParameterBag
* @var array
*/
private $parameters;

Expand All @@ -24,19 +24,15 @@ class ParameterPostProcessor
*/
public function __construct(array $parameters)
{
$this->parameters = new ParameterBag($parameters);
$this->parameters = $parameters;
}

public function __invoke(array $config) : array
{
$parameters = $this->parameters;

// First, convert values to needed parameters
$convertedParameters = $this->convertValues($parameters->all());
$parameters->clear();
$parameters->add($convertedParameters);

try {
$parameters = $this->getResolvedParameters();

array_walk_recursive($config, function (&$value) use ($parameters) {
$value = $parameters->unescapeValue($parameters->resolveValue($value));
});
Expand All @@ -49,7 +45,7 @@ public function __invoke(array $config) : array
return $config;
}

private function convertValues(array $values, string $prefix = '') : array
private function resolveNestedParameters(array $values, string $prefix = '') : array
{
$convertedValues = [];
foreach ($values as $key => $value) {
Expand All @@ -60,10 +56,19 @@ private function convertValues(array $values, string $prefix = '') : array

$convertedValues[$prefix . $key] = $value;
if (is_array($value)) {
$convertedValues += $this->convertValues($value, $prefix . $key . '.');
$convertedValues += $this->resolveNestedParameters($value, $prefix . $key . '.');
}
}

return $convertedValues;
}

private function getResolvedParameters() : ParameterBag
{
$resolved = $this->resolveNestedParameters($this->parameters);
$bag = new ParameterBag($resolved);

$bag->resolve();
return $bag;
}
}
41 changes: 41 additions & 0 deletions test/ParameterPostProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,45 @@ public function testCanDetectMissingParameter()
$this->expectException(ParameterNotFoundException::class);
$processor(['foo' => '%foo%']);
}

public function testResolvesParameterizedParameters()
{
$processor = new ParameterPostProcessor([
'foo' => 'bar',
'bar' => 'baz',
'baz' => '%foo%',
'nested' => [
'foo' => '%bar%',
'bar' => '%nested.foo%',
],
]);

$processed = $processor(['foo' => '%nested.bar%']);

$this->assertArraySubset([
'foo' => 'baz',
'parameters' => [
'nested' => [
'bar' => 'baz',
],
],
], $processed);
}

public function testResolvesParameterizedParametersInReversedOrder()
{
$processor = new ParameterPostProcessor([
'foo' => '%bar%',
'bar' => '%baz%',
'baz' => 'qux',
]);

$processed = $processor([]);

$this->assertArraySubset([
'parameters' => [
'foo' => 'qux',
],
], $processed);
}
}