From 7d9cac18c8605ea3f80b7c11467ea4d41f5cd64f Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Fri, 12 Mar 2021 22:11:21 +0100 Subject: [PATCH] Allow a string at the configuration parameter in ArrayAdapter, to include a PHP file that returns an array --- CHANGELOG.md | 8 +++++++- src/Adapter/ArrayAdapter.php | 21 +++++++++++++++++++-- tests/Adapter/ArrayAdapterTest.php | 20 +++++++++++++++++++- tests/Adapter/config.php | 12 ++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 tests/Adapter/config.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e6156a8..0905f28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file. This projec to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). -## [2.0.0-alpha1] +## [2.0.0-alpha2] - 2021-03-12 + +### Changed + +- Allow a string at the configuration parameter in ArrayAdapter, to include a PHP file that returns an array + +## [2.0.0-alpha1] - 2021-03-11 ### Added diff --git a/src/Adapter/ArrayAdapter.php b/src/Adapter/ArrayAdapter.php index 7b4ca6f..4eb9937 100644 --- a/src/Adapter/ArrayAdapter.php +++ b/src/Adapter/ArrayAdapter.php @@ -14,6 +14,8 @@ namespace Berlioz\Config\Adapter; +use Berlioz\Config\Exception\ConfigException; + /** * Class ArrayAdapter. */ @@ -22,12 +24,27 @@ class ArrayAdapter extends AbstractAdapter /** * ArrayAdapter constructor. * - * @param array $configuration + * @param array|string $configuration * @param int $priority + * + * @throws ConfigException */ - public function __construct(array $configuration, int $priority = 0) + public function __construct(array|string $configuration, int $priority = 0) { parent::__construct($priority); + + if (is_string($configuration)) { + $file = $configuration; + + ob_start(); + $configuration = @include($file); + ob_end_clean(); + + if (!is_array($configuration)) { + throw new ConfigException(sprintf('Not a valid PHP array in "%s" file', $file)); + } + } + $this->configuration = $configuration; } } \ No newline at end of file diff --git a/tests/Adapter/ArrayAdapterTest.php b/tests/Adapter/ArrayAdapterTest.php index c232c2f..731f22b 100644 --- a/tests/Adapter/ArrayAdapterTest.php +++ b/tests/Adapter/ArrayAdapterTest.php @@ -13,11 +13,12 @@ namespace Berlioz\Config\Tests\Adapter; use Berlioz\Config\Adapter\ArrayAdapter; +use Berlioz\Config\Exception\ConfigException; use PHPUnit\Framework\TestCase; class ArrayAdapterTest extends TestCase { - public function test() + public function testLoadArray() { $adapter = new ArrayAdapter( [ @@ -40,4 +41,21 @@ public function test() $this->assertFalse($adapter->has('baz')); $this->assertTrue($adapter->has('section.foo')); } + + public function testLoadFile() + { + $adapter = new ArrayAdapter(__DIR__ . '/config.php'); + + $this->assertEquals('value1', $adapter->get('qux')); + $this->assertEquals('value', $adapter->get('section.foo')); + $this->assertEquals('value2', $adapter->get('section.qux')); + $this->assertEquals(['bar' => 'value3'], $adapter->get('section2')); + } + + public function testLoadFileFailed() + { + $this->expectException(ConfigException::class); + + new ArrayAdapter(__DIR__ . '/config-failed.php'); + } } diff --git a/tests/Adapter/config.php b/tests/Adapter/config.php new file mode 100644 index 0000000..a71672c --- /dev/null +++ b/tests/Adapter/config.php @@ -0,0 +1,12 @@ + "value1", + "section" => [ + "foo" => "value", + "qux" => "value2" + ], + "section2" => [ + "bar" => "value3" + ], +]; \ No newline at end of file