diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cf621c..c4a4baa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ 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.2.0] - 2023-06-26 + +### Added + +- New `file` function to get file contents in configuration + ## [2.1.0] - 2022-02-05 ### Added @@ -92,7 +98,7 @@ use [Keep a Changelog] (http://keepachangelog.com/). ### Removed -- Remove usage of `@extends` spacial key in configuration +- Remove usage of `@extends` special key in configuration - Remove merging of configurations, replaced by multiple config objects prioritized ## [1.2.0] - 2020-11-05 diff --git a/README.md b/README.md index 8b3c97a..6707da8 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ [![Latest Version](https://img.shields.io/packagist/v/berlioz/config.svg?style=flat-square)](https://github.com/BerliozFramework/Config/releases) [![Software license](https://img.shields.io/github/license/BerliozFramework/Config.svg?style=flat-square)](https://github.com/BerliozFramework/Config/blob/2.x/LICENSE) -[![Build Status](https://img.shields.io/github/workflow/status/BerliozFramework/Config/Tests/2.x.svg?style=flat-square)](https://github.com/BerliozFramework/Config/actions/workflows/tests.yml?query=branch%3A2.x) -[![Quality Grade](https://img.shields.io/codacy/grade/f290647a1f5143ec8299ecea9b83d6b1/2.x.svg?style=flat-square)](https://www.codacy.com/manual/BerliozFramework/Config) +[![Build Status](https://img.shields.io/github/actions/workflow/status/BerliozFramework/Config/tests.yml?branch=2.x&style=flat-square)](https://github.com/BerliozFramework/Config/actions/workflows/tests.yml?query=branch%3A2.x) +[![Quality Grade](https://img.shields.io/codacy/grade/f290647a1f5143ec8299ecea9b83d6b1/2.x.svg?style=flat-square)](https://www.codacy.com/gh/BerliozFramework/Config) [![Total Downloads](https://img.shields.io/packagist/dt/berlioz/config.svg?style=flat-square)](https://packagist.org/packages/berlioz/config) **Berlioz Configuration** is a PHP library to manage your configuration files. @@ -92,6 +92,7 @@ Defaults functions: - `constant`: replace value by a constant - `env`: replace value by environment variable - `var`: replace value by variable value +- `file`: replace value by file contents Examples: diff --git a/src/Config.php b/src/Config.php index 6b40113..e923103 100644 --- a/src/Config.php +++ b/src/Config.php @@ -49,6 +49,7 @@ public function __construct( new ConfigFunction\ConfigFunction($this), new ConfigFunction\ConstantFunction(), new ConfigFunction\EnvFunction(), + new ConfigFunction\FileFunction(), new ConfigFunction\VarFunction($this), ] ); diff --git a/src/ConfigFunction/FileFunction.php b/src/ConfigFunction/FileFunction.php new file mode 100644 index 0000000..de877c4 --- /dev/null +++ b/src/ConfigFunction/FileFunction.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code, to the root. + */ + +declare(strict_types=1); + +namespace Berlioz\Config\ConfigFunction; + +use LogicException; + +/** + * Class FileFunction. + */ +class FileFunction implements ConfigFunctionInterface +{ + /** + * @inheritDoc + */ + public function getName(): string + { + return 'file'; + } + + /** + * @inheritDoc + */ + public function execute(string $str): array|string + { + $contents = @file_get_contents($str); + + if (false === $contents) { + throw new LogicException(sprintf('File "%s" does not exists in configuration', $str)); + } + + return $contents; + } +} \ No newline at end of file diff --git a/tests/ConfigFunction/FileFunctionTest.php b/tests/ConfigFunction/FileFunctionTest.php new file mode 100644 index 0000000..d2b1b51 --- /dev/null +++ b/tests/ConfigFunction/FileFunctionTest.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code, to the root. + */ + +namespace Berlioz\Config\Tests\ConfigFunction; + +use Berlioz\Config\ConfigFunction\EnvFunction; +use Berlioz\Config\ConfigFunction\FileFunction; +use LogicException; +use PHPUnit\Framework\TestCase; + +class FileFunctionTest extends TestCase +{ + public function testGetName() + { + $function = new FileFunction(); + + $this->assertEquals('file', $function->getName()); + } + + public function testExecute() + { + $function = new FileFunction(); + + $this->assertEquals('FOO', $function->execute(__DIR__ . '/file')); + } + + public function testExecuteFailed() + { + $this->expectException(LogicException::class); + + $function = new FileFunction(); + $function->execute('file_unknown'); + } +} diff --git a/tests/ConfigFunction/file b/tests/ConfigFunction/file new file mode 100644 index 0000000..d96c7ef --- /dev/null +++ b/tests/ConfigFunction/file @@ -0,0 +1 @@ +FOO \ No newline at end of file diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 3562c71..7058d7a 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -53,11 +53,11 @@ public function testAddFunction() { $config = new FakeConfig(); - $this->assertCount(4, $config->getFunctions()->all()); + $this->assertCount(5, $config->getFunctions()->all()); $config->addFunction(new FakeFunction(), new EnvFunction()); - $this->assertCount(5, $config->getFunctions()->all()); + $this->assertCount(6, $config->getFunctions()->all()); } public function testAddConfig()