Skip to content

Commit

Permalink
New file function to get file contents in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Jun 26, 2023
1 parent c3ae0c2 commit 912de43
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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/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.
Expand Down Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function __construct(
new ConfigFunction\ConfigFunction($this),
new ConfigFunction\ConstantFunction(),
new ConfigFunction\EnvFunction(),
new ConfigFunction\FileFunction(),
new ConfigFunction\VarFunction($this),
]
);
Expand Down
45 changes: 45 additions & 0 deletions src/ConfigFunction/FileFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/*
* This file is part of Berlioz framework.
*
* @license https://opensource.org/licenses/MIT MIT License
* @copyright 2021 Ronan GIRON
* @author Ronan GIRON <https://github.com/ElGigi>
*
* 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;
}
}
43 changes: 43 additions & 0 deletions tests/ConfigFunction/FileFunctionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/*
* This file is part of Berlioz framework.
*
* @license https://opensource.org/licenses/MIT MIT License
* @copyright 2021 Ronan GIRON
* @author Ronan GIRON <https://github.com/ElGigi>
*
* 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');
}
}
1 change: 1 addition & 0 deletions tests/ConfigFunction/file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FOO
4 changes: 2 additions & 2 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 912de43

Please sign in to comment.