2017 © Pedro Peláez
 

library config-service-provider

A service provider to configurate silex ~2.0 with yaml, json and php files

image

hello-motto/config-service-provider

A service provider to configurate silex ~2.0 with yaml, json and php files

  • Tuesday, March 7, 2017
  • by hello-motto
  • Repository
  • 1 Watchers
  • 1 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

ConfigServiceProvider for Silex 2

This is a Service Provider that permits to use YAML, JSON and PHP config files in a Silex app., (*1)

Requirements

  • silex/silex: 2.X

Suggested

  • symfony/yaml: 3.X

Installation

Install with Composer:, (*2)

$ composer require hello-motto/config-service-provider dev-master

Or add to composer.json, (*3)

``` json "require": { "hello-motto/config-service-provider": "dev-master" }, (*4)


# Usage This Provider will parse your YAML, JSON and PHP files to set config variables. Those config variables are available through `$app['config']['myVariable']`. Some special variables you can define (parameters variable) also are available through `$app['parameters']`. All the variables that are contained in the several config files are recursively merged in a array. ##Add files You can pass one or several files to the ServiceProvider in the 'config.files' argument. No matter if it's a json, yaml or php file ``` php $app->register(new HelloMotto\Silex\Config\ConfigServiceProvider(), [ 'config.files' => [ PATH_TO_FIRST_FILE, PATH_TO_SECOND_FILE, PATH_TO_THIRD_FILE, etc. ] ]);

where PATH_TO_CONFIG_FILE is location of YML, JSON or PHP file with configuration for example, (*5)

__DIR__. "/config/config.yml",
__DIR__. "/config/config.json",
__DIR__. "/config/config.php",

Files example :

config/config.php, (*6)

<?php
return [
    'parameters' => [
        'dbhost' => 'localhost',
        'dbuser' => 'user',
        'dbpass' => 't@rt1fl3tt3',
        'dbport' => '~' // tilde will be replaced by null value
    ],
    'twig' => [
        'twig.form.templates' => [
            'formage.html.twig'
        ]
    ]
];

config/config.json, (*7)

{
    "parameters": {
        "dbhost": "localhost",
        "dbuser": "user",
        "dbpass": "t@rt1fl3tt3",
        "dbport": "~"
    },
    "twig": {
        "twig.form.templates": [
            "formage.html.twig"
        ]
    }
}

config/config.yml, (*8)

parameters:
    dbhost: "localhost"
    dbuser: "user"
    dbpass: "t@rt1fl3tt3"
    dbport: "~" #tilde will be replaced by null value
twig:
    twig.form.templates:
        -"formage.html.twig"

Add constants

You can also pass to the ServiceProvider constants parameters to replace in your config files. It permits to use the PHP functions within the configuration files. Constants name must begin and end with % character. You can set the constants in the 'config.constants' array :, (*9)

$app->register(new HelloMotto\Silex\Config\ConfigServiceProvider(), [
    'config.files' => [],
    'config.constants' => [
        '%web.dir%' => __DIR__.'/../web',
        '%need%' => 'tartiflette',
        etc.
    ]
]);

config/config.json ``` json { "mySubDirectory": "%web.dir%/reblochon", "ourMotto": "In %need% we trust" }, (*10)


**config/config.yml** ``` yaml mySubDirectory: "%web.dir%/reblochon", ourMotto: "In %need% we trust"

The app['config']['ourMotto'] variable will contain In tartiflette we trust., (*11)

Add closures

YAML and JSON files don't allow to use dynamic code. But some Providers like Security Provider are more powerful with closure parameters. That's why it's possible to add some closures with the 'config.closures' parameter. To use the closure parameter, your array must have the same tree structure., (*12)

$app->register(new HelloMotto\Silex\Config\ConfigServiceProvider(), [
    'config.files' => [],
    'config.constants' => [],
    'config.closures' => [
        'security' => [
            'security.firewalls' => [
                'main' => [
                    'users' => function() {
                        return new UserProvider();
                    }
                ]
            ]
        ]
    ]
]);

Import files

It's possible to import files from a YAML or JSON config file. You just need to use the object "import" and set an array of resources. The files are always imported with a relative path from the current file. Notice that PHP config files don't support the import method., (*13)

config/config_dev.json ``` json { "imports": [ { "resource": "config.json" }, { "resource": "parameters.json" } ] }, (*14)


**config/config_dev.yml** ``` yaml imports: - { resource: config.yml } - { resource: parameters.yml } # config.yml and parameters.yml are in the same directory as config_dev.yml

Traits

config() method

Adding this trait to your Application, you can use the config variables as object. $app['config'] will be available using $app->config(). You can also access to the first dimension of the array passing the parameter to the method. $app['config']['twig'] is available through $app->config('twig')., (*15)

parameters() method

This shortcut also exists for the parameters variables. $app['parameters']['dbhost'] will be the same that $app->parameters('dbhost'), (*16)

loadFile() method

This shortcut use the ConfigLoader to parse a JSON, YAML or PHP file into an array, replacing the constants that are already set., (*17)

loadManyFiles() method

This method does the same as loadFile() with an array of files as argument., (*18)

Licence

GPL 3.0, (*19)

Message from the President

Beef..., (*20)

The Versions

07/03 2017

dev-master

9999999-dev

A service provider to configurate silex ~2.0 with yaml, json and php files

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

silex yaml serviceprovider

07/03 2017

1.0

1.0.0.0

A service provider to configurate silex ~2.0 with yaml, json and php files

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

silex yaml serviceprovider