Berlioz Configuration is a PHP library to manage your configuration files.
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitattributes
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
composer.json
composer.lock
phpunit.xml.dist

README.md

Berlioz Configuration

Berlioz Configuration is a PHP library to manage your configuration files.

Installation

Composer

You can install Berlioz Configuration with Composer, it's the recommended installation.

$ composer require berlioz/config

Dependencies

  • PHP >= 7.1
  • Packages:
    • berlioz/utils
    • psr/log

Usage

Create configuration object

You can create configuration like this:

// Using files
$config = new JsonConfig('/path/of-project/config/config.json', true);
$config = new ExtendedJsonConfig('/path/of-project/config/config.json', true);

// Using data
$config = new JsonConfig('{"config": "test"}');
$config = new ExtendedJsonConfig('{"config": "test"}');

Second parameter of constructor is if the first parameter is an URL.

Get value of key

Configuration file:

{
  "var1": "value1",
  "var2": {
    "var3": "value3"
  }
}

To get value, you must do like this:

$config->get('var1'); // returns string 'value1'
$config->get('var2'); // returns array ['var3' => 'value3']
$config->get('var2.var3'); // returns 'value3'
$config->get('var3'); // returns NULL
$config->get('var3', true); // returns TRUE (default value given)

If you get an unknown value, the method return the default value given in second parameter else NULL.

You can also test if a key exists, like this:

$config->has('var1'); // returns true
$config->has('var2'); // returns true
$config->has('var4'); // returns false

Variables

In values of JSON keys, you can add this syntax to use variables: %var1.var2%, that's get key var1.var2 in replacement of value.

Some variables are available by default:

  • php_version: the value of constant PHP_VERSION
  • php_version_id: the value of constant PHP_VERSION_ID
  • php_major_version: the value of constant PHP_MAJOR_VERSION
  • php_minor_version: the value of constant PHP_MINOR_VERSION
  • php_release_version: the value of constant PHP_RELEASE_VERSION
  • php_sapi: the value of constant PHP_SAPI
  • system_os: the value of constant PHP_OS
  • system_os_family: the value of constant PHP_OS_FAMILY

You can also define your own variables with the methods:

  • setVariable(string $name, mixed $value)
  • setVariables(array $variables)

WARNING: Priority is given to the user defined variable in the config object instead of JSON path.

Extended JSON format

We created an extended format of the JSON format. Just to include or extends JSON files.

Syntax

  • Include another file: %include:filename.json%
  • Extends files: %extends:filename.json, filename2.json, filename3.json%
  • 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).

Extends configurations files

You can extends the current configuration file with another with special key @extends:

{
  // Extends
  "@extends": "another.json",
  // Keys
  "key": "value"
}

Example

File config.json:

{
  "@extends": "config.another.json",
  "var1": "value1",
  "var2": {
    "var3": "value3"
  },
  "var4": "%include:config3.json%",
  "var5": "%extends:config3.json, config2.json%"
}

File config.another.json:

{
  "var.another": "value",
  "var1": "valueX"
}

File config2.json:

{
  "var6": "value1",
  "var7": false
}

File config3.json:

{
  "var7": true
}

The final config file is:

{
  "var.another": "value",
  "var1": "value1",
  "var2": {
    "var3": "value3"
  },
  "var4": {
    "var7": true
  },
  "var5": {
    "var6": "value1",
    "var7": false
  }
}