diff --git a/src/Config/Config.php b/src/Config/Config.php index 9d48a23..3e8329a 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -23,4 +23,9 @@ public static function fromArray(array $config) { return new self($config); } + + public function toArray() : array + { + return $this->getArrayCopy(); + } } diff --git a/src/Config/Loader/CachingConfigLoader.php b/src/Config/Loader/CachingConfigLoader.php index 4243880..13dcfe6 100644 --- a/src/Config/Loader/CachingConfigLoader.php +++ b/src/Config/Loader/CachingConfigLoader.php @@ -19,8 +19,34 @@ */ final class CachingConfigLoader implements ConfigLoaderInterface { + /** + * @var ConfigLoaderInterface + */ + private $configLoader; + + /** + * @var string + */ + private $cachedConfigFile; + + public function __construct(ConfigLoaderInterface $configLoader, string $cachedConfigFile) + { + $this->configLoader = $configLoader; + $this->cachedConfigFile = $cachedConfigFile; + } + public function __invoke() : Config { - // TODO: Implement __invoke() method. + if (is_file($this->cachedConfigFile)) { + return Config::fromArray(include $this->cachedConfigFile); + } + + $configLoader = $this->configLoader; + /* @var $config Config */ + $config = $configLoader(); + + file_put_contents($this->cachedConfigFile, 'toArray(), true) . ';'); + + return $config; } } diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index f74ab8b..d255476 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -47,4 +47,16 @@ public function it_can_accessed_using_array_notation() $this->assertEquals('pdo_mysql', $config['db']['driver']); } + + /** + * @test + */ + public function it_can_converted_to_array() + { + $config = Config::fromArray([ + 'foo' => 'bar', + ]); + + $this->assertEquals(['foo' => 'bar'], $config->toArray()); + } }