diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d65013..0fad999 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,16 +3,28 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). +## [1.3.0] - 2024-12-12 + +### Added + +- "file" action to include content of file in config + ## [1.2.0] - 2020-11-05 + ### Added + - PHP 8 compatibility ## [1.1.1] - 2020-09-23 + ### Changed + - Fix variable replacement by null with empty string ## [1.1.0] - 2020-04-17 + ### Added + - New `const` action to get constant value ## [1.0.0] - 2020-02-17 diff --git a/README.md b/README.md index a4dd574..88ca996 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/1.x/LICENSE) -[![Build Status](https://img.shields.io/github/workflow/status/BerliozFramework/Config/Tests/1.x.svg?style=flat-square)](https://github.com/BerliozFramework/Config/actions/workflows/tests.yml?query=branch%3A1.x) -[![Quality Grade](https://img.shields.io/codacy/grade/f290647a1f5143ec8299ecea9b83d6b1/1.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=1.x&style=flat-square)](https://github.com/BerliozFramework/Config/actions/workflows/tests.yml?query=branch%3A1.x) +[![Quality Grade](https://img.shields.io/codacy/grade/f290647a1f5143ec8299ecea9b83d6b1/1.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. @@ -108,6 +108,7 @@ Just to do actions like include or extends JSON files. * Extends a file: `%extends:filename.json, filename2.json, filename3.json%` * Replace by an env variable: `%env:VAR_NAME%` * Replace by a constant: `%const:VAR_NAME%` or `%constant:VAR_NAME%` +* Replace by a file contents: `%file:/my/file.txt%` * Allow inline comments : `// My comment` (comment must be alone on a line) You can define your own actions with static method `ExtendedJsonConfig::addAction(string $name, callable $callback)`. diff --git a/src/ExtendedJsonConfig.php b/src/ExtendedJsonConfig.php index 726071d..60c13fe 100644 --- a/src/ExtendedJsonConfig.php +++ b/src/ExtendedJsonConfig.php @@ -15,6 +15,7 @@ namespace Berlioz\Config; use Berlioz\Config\Exception\ConfigException; +use Berlioz\Config\Exception\NotFoundException; use Exception; /** @@ -117,7 +118,10 @@ public function doActions(&$value, $key, string $baseDirectory) $matches = []; if (preg_match( - sprintf('/^\s*%1$s(?[\w\-.]+)\:(?[\w\-_.:,\\\\\s]+)%1$s\s*$/i', preg_quote(self::TAG)), + sprintf( + '/^\s*%1$s(?[\w\-.]+)\:(?[\w\-_.:,\/\\\\\s]+)%1$s\s*$/i', + preg_quote(self::TAG) + ), $value, $matches ) != 1) { @@ -144,6 +148,15 @@ function ($file) use ($baseDirectory) { case 'env': $value = getenv($matches['var']); break; + case 'file': + $currentDir = getcwd(); + chdir($baseDirectory); + $value = @file_get_contents($matches['var']); + if (false === $value) { + throw new NotFoundException(sprintf('Included file "%s" not found', $matches['var'])); + } + chdir($currentDir); + break; case 'const': case 'constant': $value = constant($matches['var']); diff --git a/tests/ExtendedJsonConfigTest.php b/tests/ExtendedJsonConfigTest.php index bc3fc12..0805c12 100644 --- a/tests/ExtendedJsonConfigTest.php +++ b/tests/ExtendedJsonConfigTest.php @@ -124,6 +124,8 @@ function ($value) { } ); $config = new ExtendedJsonConfig(sprintf('%s%s', __DIR__, '/files/config.extended2.json'), true); + $this->assertEquals(10, $config->get('action')); + $this->assertEquals('BAR', $config->get('file')); } } diff --git a/tests/files/config.extended2.json b/tests/files/config.extended2.json index dc6bb83..8be7628 100644 --- a/tests/files/config.extended2.json +++ b/tests/files/config.extended2.json @@ -2,5 +2,6 @@ "@extends": "config.json", "debug": true, "log": "alert", - "action": "%actionTest:5%" + "action": "%actionTest:5%", + "file": "%file:./file_contents.txt%" } \ No newline at end of file diff --git a/tests/files/file_contents.txt b/tests/files/file_contents.txt new file mode 100644 index 0000000..add8373 --- /dev/null +++ b/tests/files/file_contents.txt @@ -0,0 +1 @@ +BAR \ No newline at end of file