Skip to content

Commit

Permalink
Allow a string at the configuration parameter in ArrayAdapter, to inc…
Browse files Browse the repository at this point in the history
…lude a PHP file that returns an array
  • Loading branch information
ElGigi committed Mar 12, 2021
1 parent 9e8b981 commit 7d9cac1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 19 additions & 2 deletions src/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace Berlioz\Config\Adapter;

use Berlioz\Config\Exception\ConfigException;

/**
* Class ArrayAdapter.
*/
Expand All @@ -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;
}
}
20 changes: 19 additions & 1 deletion tests/Adapter/ArrayAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -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');
}
}
12 changes: 12 additions & 0 deletions tests/Adapter/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
"qux" => "value1",
"section" => [
"foo" => "value",
"qux" => "value2"
],
"section2" => [
"bar" => "value3"
],
];

0 comments on commit 7d9cac1

Please sign in to comment.