Skip to content

Commit

Permalink
Merge pull request #44 from gacela-project/refactor/improving-config
Browse files Browse the repository at this point in the history
Refactoring Config::setConfigReaders()
  • Loading branch information
JesusValera committed Jul 14, 2021
2 parents e425e2a + 64282cc commit 391939f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/CodeGenerator/CodeGeneratorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private function getCommandTemplateContent(string $filename): string
*/
public function getComposerJsonContentAsArray(): array
{
$filename = Config::getApplicationRootDir() . '/composer.json';
$filename = Config::getInstance()->getApplicationRootDir() . '/composer.json';
if (!file_exists($filename)) {
throw new LogicException('composer.json file not found but it is required');
}
Expand Down
46 changes: 24 additions & 22 deletions src/Framework/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ final class Config
{
private const GACELA_CONFIG_FILENAME = 'gacela.json';

private static string $applicationRootDir = '';

private static ?self $instance = null;

private string $applicationRootDir = '';

private array $config = [];

/** @var array<string, ConfigReaderInterface> */
Expand Down Expand Up @@ -55,23 +55,9 @@ public static function resetInstance(): void
/**
* @param array<string, ConfigReaderInterface> $configReaders
*/
public function setConfigReaders(array $configReaders = []): void
{
$this->configReaders = $configReaders;
}

public static function setApplicationRootDir(string $dir): void
public static function setConfigReaders(array $configReaders = []): void
{
self::$applicationRootDir = $dir;
}

public static function getApplicationRootDir(): string
{
if (empty(self::$applicationRootDir)) {
self::$applicationRootDir = getcwd() ?: '';
}

return self::$applicationRootDir;
self::$instance = new self($configReaders);
}

/**
Expand All @@ -84,7 +70,7 @@ public static function getApplicationRootDir(): string
public function get(string $key, $default = null)
{
if (empty($this->config)) {
$this->init();
$this->init($this->getApplicationRootDir());
}

if ($default !== null && !$this->hasValue($key)) {
Expand All @@ -101,20 +87,36 @@ public function get(string $key, $default = null)
/**
* @throws ConfigException
*/
public function init(): void
public function init(string $applicationRootDir): void
{
$this->setApplicationRootDir($applicationRootDir);

$this->config = (new ConfigInit(
self::getApplicationRootDir(),
$this->getApplicationRootDir(),
$this->createGacelaJsonConfigCreator(),
$this->createPathFinder(),
$this->configReaders
))->readAll();
}

public function setApplicationRootDir(string $dir): void
{
$this->applicationRootDir = $dir;
}

public function getApplicationRootDir(): string
{
if (empty($this->applicationRootDir)) {
$this->applicationRootDir = getcwd() ?: '';
}

return $this->applicationRootDir;
}

private function createGacelaJsonConfigCreator(): GacelaJsonConfigFactoryInterface
{
return new GacelaJsonConfigFactory(
self::$applicationRootDir,
$this->getApplicationRootDir(),
self::GACELA_CONFIG_FILENAME
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class IntegrationTest extends TestCase
{
public function setUp(): void
{
Config::setApplicationRootDir(__DIR__);
Config::getInstance()->init();
Config::getInstance()->init(__DIR__);
}

public function test_remove_key_from_container(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class IntegrationTest extends TestCase
{
public function setUp(): void
{
Config::setApplicationRootDir(__DIR__);
Config::getInstance()->init();
Config::getInstance()->init(__DIR__);
}

public function test_remove_key_from_container(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class IntegrationTest extends TestCase
{
public function setUp(): void
{
Config::setApplicationRootDir(__DIR__);
Config::getInstance()->init();
Config::getInstance()->init(__DIR__);
}

public function test_remove_key_from_container(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class IntegrationTest extends TestCase
{
public function setUp(): void
{
Config::setApplicationRootDir(__DIR__);
Config::getInstance()->init();
Config::getInstance()->init(__DIR__);
}

public function test_remove_key_from_container(): void
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/Framework/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function test_get_default_value_from_undefined_key(): void

public function test_get_using_custom_reader(): void
{
$this->config->setConfigReaders([
Config::setConfigReaders([
Config\GacelaJsonConfigItem::DEFAULT_TYPE => new class() implements ConfigReaderInterface {
public function read(string $absolutePath): array
{
Expand All @@ -50,8 +50,6 @@ public function canRead(string $absolutePath): bool
},
]);

$this->config->init();

self::assertSame('value', $this->config->get('key'));
self::assertSame('value', Config::getInstance()->get('key'));
}
}

0 comments on commit 391939f

Please sign in to comment.