Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a config to directly use top level keys as parameters #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 27 additions & 9 deletions Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function processFile(array $config)

$realFile = $config['file'];
$parameterKey = $config['parameter-key'];
$noParameterKey = $config['no-parameter-key'];

$exists = is_file($realFile);

Expand All @@ -32,17 +33,26 @@ public function processFile(array $config)

// Find the expected params
$expectedValues = $yamlParser->parse(file_get_contents($config['dist-file']));
if (!isset($expectedValues[$parameterKey])) {
throw new \InvalidArgumentException('The dist file seems invalid.');
if ($noParameterKey) {
$expectedParams = (array) $expectedValues;
} else {
if (!isset($expectedValues[$parameterKey])) {
throw new \InvalidArgumentException('The dist file seems invalid.');
}
$expectedParams = (array) $expectedValues[$parameterKey];
}
$expectedParams = (array) $expectedValues[$parameterKey];

// find the actual params
$actualValues = array_merge(
// Preserve other top-level keys than `$parameterKey` in the file
$expectedValues,
array($parameterKey => array())
);
if ($noParameterKey) {
$actualValues = array();
} else {
$actualValues = array_merge(
// Preserve other top-level keys than `$parameterKey` in the file
$expectedValues,
array($parameterKey => array())
);
}

if ($exists) {
$existingValues = $yamlParser->parse(file_get_contents($realFile));
if ($existingValues === null) {
Expand All @@ -54,7 +64,11 @@ public function processFile(array $config)
$actualValues = array_merge($actualValues, $existingValues);
}

$actualValues[$parameterKey] = $this->processParams($config, $expectedParams, (array) $actualValues[$parameterKey]);
if ($noParameterKey) {
$actualValues = $this->processParams($config, $expectedParams, (array) $actualValues);
} else {
$actualValues[$parameterKey] = $this->processParams($config, $expectedParams, (array) $actualValues[$parameterKey]);
}

if (!is_dir($dir = dirname($realFile))) {
mkdir($dir, 0755, true);
Expand All @@ -81,6 +95,10 @@ private function processConfig(array $config)
$config['parameter-key'] = 'parameters';
}

if (empty($config['no-parameter-key'])) {
$config['no-parameter-key'] = false;
}

return $config;
}

Expand Down
2 changes: 2 additions & 0 deletions Tests/fixtures/testcases/no_parameter_key/dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo: bar
boolean: false
2 changes: 2 additions & 0 deletions Tests/fixtures/testcases/no_parameter_key/existing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This file is auto-generated during the composer install
foo: existing_foo
3 changes: 3 additions & 0 deletions Tests/fixtures/testcases/no_parameter_key/expected.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file is auto-generated during the composer install
foo: existing_foo
boolean: false
4 changes: 4 additions & 0 deletions Tests/fixtures/testcases/no_parameter_key/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
title: "When 'no-parameter-key' config is set to true, top level keys are used as parameters"

config:
no-parameter-key: true