dehare/config-loader-service

Service for loading multi-format configurations into Symfony/Silex

v1.1.1 2014-07-18 07:27 UTC

README

Another configuration service for Silex/Symfony supporting multiple file formats and imports. Also supports cross-format loading.

Latest Stable Version

Installation

Add following line to your composer.json:

"require": {
    [...]
    "dehare/config-loader-service": "1.1.*@dev"
    [...]
}

Usage

Register as a service within Silex

Since this is a service, you should use the share method in any way you see fit.

    use Dehare\Symfony\ConfigLoaderService\ConfigLoaderService;

    $app['service.config'] = $app->share(function()
    {
        return new ConfigLoaderService('/dir/to/config', 'yml');
    });

Register within Symfony

services:
    service.config:
        class: Dehare\Symfony\ConfigLoaderService\ConfigLoaderService
        arguments: [%kernel.root_dir%/config, %default_format%]

Using the service

Now you can use it througout your application using:

    // silex
    $app['service.config']->get('config_database');

    // symfony
    $this->get('service.config')->get('config_database');

Be sure to read the source for more hints on using the service.

File formats

The service allows configurations in the following formats:

  • JSON
  • PHP
  • YAML/YML

The default format used is YML.

The service is forgiving and will prefer the requested file extesion over the format. See examples for detailed info.

Imports

Symfony's Import-resource modal is also supported (in all formats). Beware that the results will be merged. So be carefull when defining keys.

You can supply different file formats within an import!

Examples

YAML

host: localhost
user: myuser
password: mypassword

JSON

{
    "variable1": "value",
    "array": {
        { "variable": "value" },
        { "variable": "value" }
    }
}

PHP

<?php
return array(
    'variable' => 'value',
    'array' => array(
        array('variable', 'variable1'),
    ),
)

Imports (works with any format)

imports:
    - { resource: 'file' }
    - { resource: 'file.php' }
    - { resource: 'file.json' }

variables:
    variable1: value

Parsing configurations

index.php (Silex):

    [...]
    $app = new Silex\Application();

    $app['service.config'] = $app->share(function()
    {
        return new ConfigLoaderService('/dir/to/config', 'yml');
    });
    [...]

example.class.php:

    [...]
    $file = 'params1';
    $format = 'yaml';
    $dir = '/examples'; // => /dir/to/config/examples

    $app['service.config']->get($example_params, $format, $dir);
    [...]

Forgiving

The following will search for config.yml rather than config.php / config.yml.php.

$app['service.config']->get('config.yml', 'php');

License

Copyright (c) 2014 Valentijn Verhallen contact@valentijn.co

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.