serafim/config

Configuration helper library

1.0.0 2019-08-05 06:36 UTC

README

Travis CI 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39616334363961663764363737383435333662302f746573745f636f766572616765 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39616334363961663764363737383435333662302f6d61696e7461696e6162696c697479 68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5365726166696d417274732f436f6e6669672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572

PHP 7.1+ PHP 7.1+ Latest Stable Version Latest Stable Version Total Downloads License MIT

Introduction

Config library provides a simple way for reading and validating data structures from different file formats.

Requirements

  • PHP 7.2+

Configuration Loaders

  • railt/json for Serafim\Config\Loader\Json5Loader
  • ext-json for Serafim\Config\Loader\JsonLoader
  • nette/neon for Serafim\Config\Loader\NeonLoader
  • ext-xmlreader for Serafim\Config\Loader\XmlLoader
  • symfony/yaml for Serafim\Config\Loader\YamlLoader

Installation

Config library is available as composer repository and can be installed using the following command in the root of your project:

$ composer require serafim/config

In order to access configs make sure to include vendor/autoload.php:

require __DIR__ . '/vendor/autoload.php';

Quick Start

For example, we have such a configuration file config/example.php:

<?php
return [
    'key' => 'value'
];

Let's try to load it:

<?php
use Serafim\Config\Factory;
use Serafim\Config\Loader\PhpLoader;

$factory = new Factory(new PhpLoader(__DIR__ . '/config'));

$config = $factory->get('example');

var_dump($config->toArray());

//
// >> array(1) {
// >>   'key' => string(5) "value"
// >> }
//

Multiple Loaders

We can use several loaders. The right one will be selected on the basis of the file extension:

<?php
use Serafim\Config\Factory;
use Serafim\Config\Loader\{PhpLoader, JsonLoader, YamlLoader};

$factory = new Factory([
    new YamlLoader(__DIR__ . '/config'),
    new JsonLoader(__DIR__ . '/config'),
    new PhpLoader(__DIR__ . '/config'),
]);

var_dump($factory->get('example')->toArray());
// Same. PhpLoader will be selected.

Validation

We can also check the correctness of the config using the JSON Schema Specification.

<?php

$factory = new \Serafim\Config\Factory(...);

$factory->get('example')
    ->validate([
        'type' => 'object',
        'properties' => [
            'key' => [
                'type' => 'string'
            ]
        ]
    ])
    ->toArray();

Cache

<?php

use Serafim\Config\Factory;
use Serafim\Config\Loader\YamlLoader;
use Serafim\Config\Loader\JsonLoader;
use Serafim\Config\Loader\CachedLoader;

$cache = ...; // Instance of PSR-16 cache driver

$loaders = [
   new YamlLoader([__DIR__ . '/config', __DIR__ . '/fallback']),
   new JsonLoader(__DIR__ . '/config'),
];

$factory = new Factory(
    new CachedLoader(
        $cache, /** Instance of PSR-16 cache driver */ 
        $loaders
        // $cachePrefix (optional)
        // $tti (optional)
    )
);

$factory->get('...'); //