Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jaxwilko/simple-config

A simple fluent interface for creating php config files
https://github.com/jaxwilko/simple-config

configuration-management php php7

Last synced: 3 months ago
JSON representation

A simple fluent interface for creating php config files

Awesome Lists containing this project

README

        

> Archived: use [winter/laravel-config-writer](https://github.com/wintercms/laravel-config-writer) instead.

# SimpleConfig

This package aims to provide a simple interface for creating php config files.

### Installation

Add the package to your project via:

```
composer require jaxwilko/simple-config
```

### Usage

```php
title = 'Database';
$section->key = 'database';
$section->comment = 'This is where your database config goes.';
$section->value = ['mysql' => ['host' => 'localhost']];
$compiler->addSection($section);
echo $compiler->render();
```

Would return:

```php
[
'mysql' => [
'host' => 'localhost',
],
],
];
```

A simpler way of achieving the same thing would be:

```php
addSection(new Section([
'title' => 'Database',
'key' => 'database',
'comment' => 'This is where your database config goes.',
'value' => ['mysql' => ['host' => 'localhost']],
]));
echo $compiler->render();
```

Everything can be omitted, for example just having a comment and title:

```php
$compiler = new Compiler();
$compiler->addSection(new Section([
'title' => 'Database',
'comment' => 'This is where your database config goes.',
]));
echo $compiler->render();
```

Will return:

```php
addSection(new Section([
'key' => 'database',
'value' => ['mysql' => ['host' => 'localhost']],
]));
echo $compiler->render();
```

Will return:

```php
[
'mysql' => [
'host' => 'localhost',
],
],
];
```

Multiple sections can be added via chaining:

```php
addSection(new Section([
'title' => 'Section one',
'key' => 'section_one',
'comment' => 'This is a section',
'value' => [
'foo' => 'bar',
'bar' => [
'foo'
]
]
]))
->addSection(new Section([
'title' => 'Section two',
'key' => 'section_two',
'comment' => 'This is another section',
'value' => [
'bar' => 'foo',
'foo' => [
'bar'
]
]
]));
```

Functions can be passed in by prefixing the method with `@@`. E.g.

```php
addSection(new Section([
'title' => 'Section',
'key' => 'section',
'comment' => 'This is a section',
'value' => [
'foo' => '@@env(\'VALUE\')',
]
]));
```

Will result in:

```php
[
'foo' => env('VALUE'),
],
];
```

Compiler options can be set via:

```php
option('length', 80);
// use spaces over tabs
$compiler->option('tabs', false);
// define the indentation length
$compiler->option('indent', 4);
```