Skip to content

Commit

Permalink
[config] Change to use ArrayConfig as the backing storage for file co…
Browse files Browse the repository at this point in the history
…nfigs

This is to standardize how we can access stuff in the config array
  • Loading branch information
sunkan committed Mar 21, 2024
1 parent 50b0dbc commit d451834
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
13 changes: 6 additions & 7 deletions src/FileCollectionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@ final class FileCollectionConfig implements Config

/** @var string[] */
private array $files = [];
/** @var array<string, mixed> */
private array $configData;
private ArrayConfig $loadedConfig;

public function __construct(
private readonly string $configPath,
) {}

public function addFile(string $file): void
{
if (isset($this->configData)) {
if (isset($this->loadedConfig)) {
throw new ConfigLocked("Can't add more files after config have been loaded");
}
$this->files[] = $file;
}

public function getRawValue(string $key): mixed
{
if (isset($this->configData)) {
return $this->configData[$key] ?? null;
if (isset($this->loadedConfig)) {
return $this->loadedConfig->getRawValue($key);
}

$configs = [];
Expand All @@ -40,8 +39,8 @@ public function getRawValue(string $key): mixed
$configs[] = require $this->configPath . $configFile;
}
$this->files = [];
$this->configData = array_merge(...$configs);
$this->loadedConfig = new ArrayConfig(array_merge(...$configs));

return $this->configData[$key] ?? null;
return $this->loadedConfig->getRawValue($key);
}
}
11 changes: 5 additions & 6 deletions src/FileConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@ final class FileConfig implements Config
{
use GetConfigTrait;

/** @var array<string, mixed> */
private array $configData;
private ArrayConfig $loadedConfig;

public function __construct(
private readonly string $configFile,
) {}

public function getRawValue(string $key): mixed
{
if (isset($this->configData)) {
return $this->configData[$key] ?? null;
if (isset($this->loadedConfig)) {
return $this->loadedConfig->getRawValue($key);
}

if (!file_exists($this->configFile)) {
throw new FileNotFound('Configuration not found: ' . $this->configFile);
}
$this->configData = require $this->configFile;
$this->loadedConfig = ArrayConfig::fromArray(require $this->configFile);

return $this->configData[$key] ?? null;
return $this->loadedConfig->getRawValue($key);
}
}

0 comments on commit d451834

Please sign in to comment.