2017 © Pedro Peláez
 

library silex-config

Silex 2.0+ service provider for lightweight framework configuration

image

lokhman/silex-config

Silex 2.0+ service provider for lightweight framework configuration

  • Wednesday, March 8, 2017
  • by lokhman
  • Repository
  • 2 Watchers
  • 7 Stars
  • 6,940 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 8 Versions
  • 8 % Grown

The README.md

silex-config

Build Status StyleCI codecov Downloads Packagist License, (*1)

Lightweight configuration service provider for Silex 2.0+ micro-framework., (*2)

This project is a part of silex-tools library., (*3)

Installation

You can install silex-config with Composer:, (*4)

composer require lokhman/silex-config

Documentation

Simple and lightweight configuration provider, which uses JSON files to manage application configuration. Library supports different environments via setting a global environment variable., (*5)

use Lokhman\Silex\Provider\ConfigServiceProvider;

$app->register(new ConfigServiceProvider(), [
    'config.dir' => __DIR__ . '/../app/config',
]);

File structure

First off, create a config folder in your application directory and add configuration JSON files one per intended environment (default is local):, (*6)

/
  app/
    config/
      dev.json
      local.json
      prod.json
      staging.json
    logs/
  src/
  tests/
  vendor/
  web/
    index.php
  composer.json
  ...

Config files

Next, add all your defaults to the config files, e.g.:, (*7)

{
    "env": "%__ENV__%",
    "debug": true,
    "dbs.options": {
        "default": {
            "driver": "pdo_mysql",
            "host": "localhost",
            "dbname": "database",
            "user": "root",
            "password": "",
            "charset": "utf8"
        }
    },
    "any": {
        "other": "constant"
    }
}

Register

Now register service provider in your Silex application:, (*8)

use Lokhman\Silex\Provider as ToolsProviders;

$app->register(new ToolsProviders\ConfigServiceProvider(), [
    'config.dir' => __DIR__ . '/../app/config',
]);

config.dir parameter refers to a configuration folder path with JSON files., (*9)

Global environment variable

Finally, you can set up your web server to add support of different deployment environments. In order to do this, you have to set a global environmental variable., (*10)

nginx + PHP-FPM

fastcgi_param SILEX_ENV prod

Apache

SetEnv SILEX_ENV prod

CLI

$ SILEX_ENV=prod bash -c "php bin/console migrations:status"

If you use Console Application together with ConfigServiceProvider you can pass --env (-e in short) option to all registered commands:, (*11)

$ php bin/console migrations:status --env=prod

Parameters

ConfigServiceProvider supports the following parameters:, (*12)

Parameter Description Default
config.dir Folder path with JSON files. null
config.params Array of replacement tokens to use in configuration. []
config.env Environment to use strictly on provider registration (ignores global environment variable). "local"
config.varname Name of global environment variable. "SILEX_ENV"

By default, service provider embeds tokens __DIR__ and __ENV__, as well as all PHP environment variables (e.g. REMOTE_ADDR, SERVER_NAME, etc)., (*13)

Dynamic tokens

You can define tokens dynamically in the JSON files using property $params:, (*14)

local.json
{
    "$params": {
        "SECRET": "3ecd45ff71c87269569e682f2f6b2ec4"
    },
    "settings": {
        "prop1": "%SECRET%",
        "prop2": "%secret%",
        "prop3": "%SeCrEt%"
    }
}

N.B.: All tokens are case insensitive., (*15)

Extending

You can extend JSON configuration (include one JSON file into another) simply using root property $extends, that points to the file to extend (file extension can be omitted). For example:, (*16)

local.json
{
    "env": "%__ENV__%",
    "debug": true,
    "locale": "en"
}

prod.json
{
    "$extends": "local",
    "debug": false
}

License

Library is available under the MIT license. The included LICENSE file describes this in detail., (*17)

The Versions

08/03 2017

dev-master

9999999-dev

Silex 2.0+ service provider for lightweight framework configuration

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alexander Lokhman

config silex tools

08/03 2017

2.0.2

2.0.2.0

Silex 2.0+ service provider for lightweight framework configuration

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alexander Lokhman

config silex tools

15/02 2017

2.0.1

2.0.1.0

Silex 2.0+ service provider for lightweight framework configuration

  Sources   Download

MIT

The Requires

 

by Alexander Lokhman

config silex tools

24/01 2017

2.0.0

2.0.0.0

Silex 2.0+ service provider for lightweight framework configuration

  Sources   Download

MIT

The Requires

 

by Alexander Lokhman

config silex tools

29/07 2015

v1.x-dev

1.9999999.9999999.9999999-dev

Simple JSON configuration provider for Silex micro-framework

  Sources   Download

MIT

The Requires

 

by Alexander Lokhman

json config silex

29/07 2015

1.0

1.0.0.0

Simple JSON configuration provider for Silex micro-framework

  Sources   Download

MIT

The Requires

 

by Alexander Lokhman

json config silex

17/07 2015

0.2

0.2.0.0

Simple JSON configuration provider for Silex micro-framework

  Sources   Download

MIT

The Requires

 

by Alexander Lokhman

json config silex

16/07 2015

0.1

0.1.0.0

Simple JSON configuration provider for Silex micro-framework

  Sources   Download

MIT

The Requires

 

by Alexander Lokhman

json config silex