Skip to content

Commit

Permalink
Cleaning up loading process by having resolve() method handle loading…
Browse files Browse the repository at this point in the history
… missing data.

Being more explicit on calling loader->load() - better SA.
  • Loading branch information
brentscheffler committed Oct 16, 2019
1 parent 90dfe93 commit 70ccb47
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Config.php
Expand Up @@ -95,7 +95,12 @@ public function setThrowIfNotFound(bool $enable): void
*/
protected function resolve(string $index, ?string $path = null)
{
// Set the pointer at the specified key.
// Attempt to load the data if it's not found.
if( empty($this->items[$index]) ){
$this->load($index);
}

// Set the pointer at the specified key.
$pointer = &$this->items[$index] ?? null;

if( empty($pointer) ){
Expand Down Expand Up @@ -154,11 +159,6 @@ public function get($key)
{
list($index, $path) = $this->parseKey($key);

// If the key does not exist, try loading.
if( $this->has($key) === false ){
$this->load($index);
}

try {

$value = $this->resolve($index, $path);
Expand Down Expand Up @@ -197,8 +197,12 @@ public function set(string $key, $value): void
*/
private function load(string $index): void
{
/** @var LoaderInterface $loader */
foreach( $this->loaders as $loader ){
if( ($items = \call_user_func([$loader, 'load'], $index)) ){

$items = $loader->load($index);

if( $items !== null ){
$this->items[$index] = $items;
break;
}
Expand All @@ -209,7 +213,8 @@ private function load(string $index): void
* Parse a key into index and path values.
*
* @param string $key
* @return array
* @throws ConfigException
* @return array<string>
*/
protected function parseKey(string $key): array
{
Expand All @@ -223,6 +228,6 @@ protected function parseKey(string $key): array
return [$match[1], $match[2]];
}

return [null, null];
throw new ConfigException("Invalid key {$key}.");
}
}

0 comments on commit 70ccb47

Please sign in to comment.