Skip to content

Commit

Permalink
Refactor config loader
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Feb 19, 2024
1 parent eadd9ad commit 4003304
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 44 deletions.
30 changes: 7 additions & 23 deletions src/Config/Loader/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

class ConfigLoader implements ConfigLoaderInterface
{
private ConfigInterface $config;

/**
* @var string[]
*/
Expand All @@ -25,32 +23,18 @@ public function __construct(private FileLocatorInterface $fileLocator)
/**
* @inheritdoc
*/
public function load(string $fileName): void
public function load(string $fileName, ConfigInterface $config): void
{
if (!isset($this->config)) {
throw new ConfigException('The configuration object must be set.');
}

$fileName = $this->fileLocator->locate($fileName);
$this->loadFile($fileName);
}

/**
* @inheritdoc
*/
public function setConfig(ConfigInterface $config): self
{
$this->config = $config;

return $this;
$this->loadFile($fileName, $config);
}

/**
* Load a configuration file.
*
* @throws ConfigException
*/
private function loadFile(string $fileName): void
private function loadFile(string $fileName, ConfigInterface $config): void
{
$input = file_get_contents($fileName);
if ($input === false) {
Expand All @@ -71,11 +55,11 @@ private function loadFile(string $fileName): void
if (isset($data['extends'])) {
$fileNames = (array) $data['extends'];
$currentDirectory = dirname($fileName);
$this->loadParentFiles($fileNames, $currentDirectory);
$this->loadParentFiles($fileNames, $config, $currentDirectory);
unset($data['extends']);
}

$this->config->merge($data);
$config->merge($data);
}

/**
Expand All @@ -84,14 +68,14 @@ private function loadFile(string $fileName): void
* @param string[] $fileNames
* @throws ConfigException
*/
private function loadParentFiles(array $fileNames, string $currentDirectory): void
private function loadParentFiles(array $fileNames, ConfigInterface $config, string $currentDirectory): void
{
foreach ($fileNames as $fileName) {
$fileName = $this->fileLocator->locate($fileName, $currentDirectory);

// Load the parent file if it was not already loaded
if (!in_array($fileName, $this->loadedTemplates, true)) {
$this->loadFile($fileName);
$this->loadFile($fileName, $config);
$this->loadedTemplates[] = $fileName;
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/Config/Loader/ConfigLoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@ interface ConfigLoaderInterface
*
* @throws ConfigException
*/
public function load(string $fileName): void;

/**
* Set the config object.
*/
public function setConfig(ConfigInterface $config): self;
public function load(string $fileName, ConfigInterface $config): void;
}
3 changes: 1 addition & 2 deletions src/Console/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ private function loadConfig(InputInterface $input): ConfigInterface
$config = new Config();

// Load config files
$this->configLoader->setConfig($config);
foreach ($input->getArgument('config_file') as $configFile) {
$this->configLoader->load($configFile);
$this->configLoader->load($configFile, $config);
}

// Compile the config
Expand Down
3 changes: 1 addition & 2 deletions tests/functional/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ protected static function getConfig(): ConfigInterface

/** @var ConfigLoaderInterface $loader */
$loader = self::getContainer()->get('config.loader');
$loader->setConfig(self::$config)
->load(self::getResource('config/templates/test.yaml'));
$loader->load(self::getResource('config/templates/test.yaml'), self::$config);

/** @var CompilerInterface $compiler */
$compiler = self::getContainer()->get('config.compiler');
Expand Down
19 changes: 8 additions & 11 deletions tests/unit/Config/Loader/ConfigLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class ConfigLoaderTest extends TestCase
public function testLoad(): void
{
$config = new Config(['version' => '2.0.0']);
$configLoader = $this->createConfigLoader($config);
$configLoader->load($this->getResource('config/templates/test.yaml'));
$configLoader = $this->createConfigLoader();
$configLoader->load($this->getResource('config/templates/test.yaml'), $config);

$expectedSubset = ['output' => '%env(DUMP_OUTPUT)%'];
$this->assertArraySubset($expectedSubset, $config->get('dump'));
Expand Down Expand Up @@ -51,10 +51,10 @@ public function testLoad(): void
public function testFileNotFoundException(): void
{
$config = new Config();
$configLoader = $this->createConfigLoader($config);
$configLoader = $this->createConfigLoader();

$this->expectException(FileNotFoundException::class);
$configLoader->load('not_exists.yaml');
$configLoader->load('not_exists.yaml', $config);
}

/**
Expand All @@ -63,10 +63,10 @@ public function testFileNotFoundException(): void
public function testDataIsNotAnArray(): void
{
$config = new Config();
$configLoader = $this->createConfigLoader($config);
$configLoader = $this->createConfigLoader();

$this->expectException(ParseException::class);
$configLoader->load($this->getResource('config/templates/invalid_data.yaml'));
$configLoader->load($this->getResource('config/templates/invalid_data.yaml'), $config);
}

/**
Expand Down Expand Up @@ -102,13 +102,10 @@ private function isArraySubset(array $subset, array $array): bool
/**
* Create a config loader object.
*/
private function createConfigLoader(Config $config): ConfigLoader
private function createConfigLoader(): ConfigLoader
{
$templatesDirectory = $this->getResource('config/templates');

$configLoader = new ConfigLoader(new FileLocator($templatesDirectory));
$configLoader->setConfig($config);

return $configLoader;
return new ConfigLoader(new FileLocator($templatesDirectory));
}
}

0 comments on commit 4003304

Please sign in to comment.