Skip to content

Commit

Permalink
Add "file" action to include content of file in config
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Dec 12, 2024
1 parent c3fcb97 commit 2941250
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)`.
Expand Down
15 changes: 14 additions & 1 deletion src/ExtendedJsonConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Berlioz\Config;

use Berlioz\Config\Exception\ConfigException;
use Berlioz\Config\Exception\NotFoundException;
use Exception;

/**
Expand Down Expand Up @@ -117,7 +118,10 @@ public function doActions(&$value, $key, string $baseDirectory)

$matches = [];
if (preg_match(
sprintf('/^\s*%1$s(?<action>[\w\-.]+)\:(?<var>[\w\-_.:,\\\\\s]+)%1$s\s*$/i', preg_quote(self::TAG)),
sprintf(
'/^\s*%1$s(?<action>[\w\-.]+)\:(?<var>[\w\-_.:,\/\\\\\s]+)%1$s\s*$/i',
preg_quote(self::TAG)
),
$value,
$matches
) != 1) {
Expand All @@ -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']);
Expand Down
2 changes: 2 additions & 0 deletions tests/ExtendedJsonConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
3 changes: 2 additions & 1 deletion tests/files/config.extended2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"@extends": "config.json",
"debug": true,
"log": "alert",
"action": "%actionTest:5%"
"action": "%actionTest:5%",
"file": "%file:./file_contents.txt%"
}
1 change: 1 addition & 0 deletions tests/files/file_contents.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BAR

0 comments on commit 2941250

Please sign in to comment.