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

Add ability to use Config without params #131

Merged
merged 1 commit into from
Feb 11, 2023
Merged
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 1.2.1 under development

- no changes in this release.
- Enh #131: Add ability to use `Config` without params (@vjik)

## 1.2.0 February 08, 2023

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ $config = new Config(
);
```

You can pass `null` as `$params` group name. In this case `$params` will empty array.

### Using sub-configs

In order to access a sub-config, use the following in your config:
Expand Down
14 changes: 8 additions & 6 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class Config implements ConfigInterface
* @param ConfigPaths $paths The config paths instance.
* @param string|null $environment The environment name.
* @param object[] $modifiers Modifiers that affect merge process.
* @param string $paramsGroup Group name for $params.
* @param string|null $paramsGroup Group name for `$params`. If it is `null`, then `$params` will be empty array.
* @param string $mergePlanFile The merge plan filepath.
*
* @throws ErrorException If the environment does not exist.
Expand All @@ -41,7 +41,7 @@ public function __construct(
ConfigPaths $paths,
string $environment = null,
array $modifiers = [],
private string $paramsGroup = 'params',
private ?string $paramsGroup = 'params',
string $mergePlanFile = Options::DEFAULT_MERGE_PLAN_FILE,
) {
$environment = empty($environment) ? Options::DEFAULT_ENVIRONMENT : $environment;
Expand Down Expand Up @@ -85,9 +85,11 @@ public function has(string $group): bool
*/
private function runBuildParams(): void
{
$this->isBuildingParams = true;
$this->runBuildGroup($this->paramsGroup);
$this->isBuildingParams = false;
if ($this->paramsGroup !== null) {
$this->isBuildingParams = true;
$this->runBuildGroup($this->paramsGroup);
$this->isBuildingParams = false;
}
}

/**
Expand Down Expand Up @@ -188,7 +190,7 @@ private function buildFile(string $filePath): array

if (!$this->isBuildingParams) {
$scope['config'] = $this;
$scope['params'] = $this->build[$this->paramsGroup];
$scope['params'] = $this->paramsGroup === null ? [] : $this->build[$this->paramsGroup];
}

/** @psalm-suppress TooManyArguments */
Expand Down
20 changes: 20 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,26 @@ public function testConfigWithCustomParams(): void
);
}

public function testConfigWithoutParams(): void
{
$config = new Config(
new ConfigPaths(__DIR__ . '/TestAsset/configs/without-params', 'config'),
Options::DEFAULT_ENVIRONMENT,
paramsGroup: null
);

$this->assertSame(
[
'a-web-key' => 'a-web-value',
'a-web-environment-override-key' => 'a-web-override-value',
'b-web-key' => 'b-web-value',
'b-web-environment-override-key' => 'b-web-override-value',
'root-web-key' => 42,
],
$config->get('web')
);
}

public function testNotFoundFile(): void
{
$config = new Config(
Expand Down
31 changes: 31 additions & 0 deletions tests/TestAsset/configs/without-params/config/.merge-plan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

// Do not edit. Content will be replaced.
return [
'/' => [
'params' => [
'package/a' => [
'params.php',
],
'package/b' => [
'params.php',
],
'/' => [
'params.php',
],
],
'web' => [
'package/a' => [
'web.php',
],
'package/b' => [
'web.php',
],
'/' => [
'web.php',
],
],
],
];
7 changes: 7 additions & 0 deletions tests/TestAsset/configs/without-params/config/params.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

return [
'root-params-key' => 'root-params-value',
];
9 changes: 9 additions & 0 deletions tests/TestAsset/configs/without-params/config/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

/** @var array $params */

return [
'root-web-key' => $params['root-params-key'] ?? 42,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

return [
'a-params-key' => 'a-params-value',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

return [
'a-web-key' => 'a-web-value',
'a-web-environment-override-key' => 'a-web-override-value',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

return [
'b-params-key' => 'b-params-value',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

return [
'b-web-key' => 'b-web-value',
'b-web-environment-override-key' => 'b-web-override-value',
];