Skip to content

Commit

Permalink
Add filename to each loaded configuration file, to ease debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosbth committed Aug 3, 2018
1 parent c27f567 commit 674f494
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
15 changes: 11 additions & 4 deletions README.md
Expand Up @@ -94,11 +94,18 @@ The resulting configuration array looks like this, still using "route" as exampl

```php
$config = [
"base" => configuration returned from route.php,
"file" => filename for route.php,
"config" => result returned from route.php,
"items" => [
configuration returned from route/file1.php,
configuration returned from route/file2.php,
]
[
"file" => filename for route/file1.php,
"config" => result returned from route/file1.php,
],
[
"file" => filename for route/file2.php,
"config" => result returned from route/file2.php,
],
].
];
```

Expand Down
37 changes: 27 additions & 10 deletions src/Configure/Configuration.php
Expand Up @@ -57,11 +57,18 @@ public function setBaseDirectories(array $dirs): object
* values from each individual configuration file, like this.
*
* $config = [
* "base" => configuration returned from file.php,
* "file" => filename for file.php,
* "config" => result returned from file.php,
* "items" => [
* "file1.php" => configuration returned from dir/file1.php,
* "file2.php" => configuration returned from dir/file2.php,
* ]
* [
* "file" => filename for dir/file1.php,
* "config" => result returned from dir/file1.php,
* ],
* [
* "file" => filename for dir/file2.php,
* "config" => result returned from dir/file2.php,
* ],
* ].
* ]
*
* The configuration files in the directory are loaded per alphabetical
Expand Down Expand Up @@ -90,7 +97,8 @@ public function load(string $item): array
// The configuration is found in a file
if (is_readable($file) && is_file($file)) {
$found = true;
$config["base"] = require $file;
$config["file"] = $file;
$config["config"] = require $file;
}

// The configuration is found in a directory
Expand Down Expand Up @@ -118,9 +126,16 @@ public function load(string $item): array

/**
* Read configuration a directory, loop through all files and add
* them into the $config array as [
* loaded configuration from dir/file1.php,
* loaded configuration from dir/file2.php,
* them into the $config array as:
* [
* [
* "file" => filename for dir/file1.php,
* "config" => result returned from dir/file1.php,
* ],
* [
* "file" => filename for dir/file2.php,
* "config" => result returned from dir/file2.php,
* ],
* ].
*
* @param string $path is the path to the directory containing config files.
Expand All @@ -131,8 +146,10 @@ public function loadFromDir(string $path): array
{
$config = [];
foreach (glob("$path/*.php") as $file) {
$config[] = require $file;
$config["filename"] = $file;
$config[] = [
"file" => $file,
"config" => require $file,
];
}

return $config;
Expand Down
21 changes: 12 additions & 9 deletions test/Configure/ConfigurationTest.php
Expand Up @@ -29,9 +29,10 @@ public function testConfigFromSingleFile()
$config = $cfg->load("view");

$this->assertInternalType("array", $config);
$this->assertArrayHasKey("base", $config);
$this->assertArrayHasKey("file", $config);
$this->assertArrayHasKey("config", $config);
$this->assertArrayNotHasKey("items", $config);
$this->assertContains("a view", $config["base"]);
$this->assertContains("a view", $config["config"]);
}


Expand All @@ -46,10 +47,11 @@ public function testConfigFromDirectory()
$config = $cfg->load("response");

$this->assertInternalType("array", $config);
$this->assertArrayNotHasKey("base", $config);
$this->assertArrayNotHasKey("file", $config);
$this->assertArrayNotHasKey("config", $config);
$this->assertArrayHasKey("items", $config);
$this->assertContains("part1", $config["items"][0]);
$this->assertContains("part2", $config["items"][1]);
$this->assertContains("part1", $config["items"][0]["config"]);
$this->assertContains("part2", $config["items"][1]["config"]);
}


Expand All @@ -64,10 +66,11 @@ public function testConfigFromFileAndDirectory()
$config = $cfg->load("route");

$this->assertInternalType("array", $config);
$this->assertArrayHasKey("base", $config);
$this->assertContains("a route", $config["base"]);
$this->assertArrayHasKey("file", $config);
$this->assertArrayHasKey("config", $config);
$this->assertContains("a route", $config["config"]);
$this->assertArrayHasKey("items", $config);
$this->assertContains("a 404 route", $config["items"][0]);
$this->assertContains("an internal route", $config["items"][1]);
$this->assertContains("a 404 route", $config["items"][0]["config"]);
$this->assertContains("an internal route", $config["items"][1]["config"]);
}
}

0 comments on commit 674f494

Please sign in to comment.