• Stars
    star
    216
  • Rank 183,179 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A config ServiceProvider for Silex with support for php, json and yaml.

ConfigServiceProvider

A config ServiceProvider for Silex with support for php, json, yaml, and toml.

Usage

Passing a config file

Pass the config file's path to the service provider's constructor. This is the recommended way of doing it, allowing you to define multiple environments.

$env = getenv('APP_ENV') ?: 'prod';
$app->register(new Igorw\Silex\ConfigServiceProvider(__DIR__."/../config/$env.json"));

Now you can specify a prod and a dev environment.

config/prod.json

{
    "debug": false
}

config/dev.json

{
    "debug": true
}

To switch between them, just set the APP_ENV environment variable. In apache that would be:

SetEnv APP_ENV dev

Or in nginx with fcgi:

fastcgi_param APP_ENV dev

Replacements

Also, you can pass an array of replacement patterns as second argument.

$app->register(new Igorw\Silex\ConfigServiceProvider(__DIR__."/../config/services.json", array(
    'data_path' => __DIR__.'/data',
)));

Now you can use the pattern in your configuration file.

/config/services.json

{
    "xsl.path": "%data_path%/xsl"
}

You can also specify replacements inside the config file by using a key with %foo% notation:

{
    "%root_path%": "../..",
    "xsl.path": "%root_path%/xsl"
}

Using Yaml

To use Yaml instead of JSON, just pass a file that ends on .yml:

$app->register(new Igorw\Silex\ConfigServiceProvider(__DIR__."/../config/services.yml"));

Note, you will have to require the ~2.1 of the symfony/yaml package.

Using TOML

To use TOML instead of any of the other supported formats, just pass a file that ends on .toml:

$app->register(new Igorw\Silex\ConfigServiceProvider(__DIR__."/../config/services.toml"));

Note, you will have to require the ~0.1 of the jamesmoss/toml package and you are using a bleeding edge configuration format, as the spec of TOML is still subject to change.

Using plain PHP

If reading the config file on every request becomes a performance problem in production, you can use a plain PHP file instead, and it will get cached by APC.

You'll have to rewrite your config to be a PHP file that returns the array of config data, and also make sure it ends with .php:

$app->register(new Igorw\Silex\ConfigServiceProvider(__DIR__."/../config/prod.php"));

Multiple config files

You can use multiple config files, e. g. one for a whole application and a specific one for a task by calling $app->register() several times, each time passing another instance of Igorw\Silex\ConfigServiceProvider.

Register order

Make sure you register ConfigServiceProvider last with your application. If you do not do this, the default values of other Providers will override your configuration.

More Repositories

1

evenement

Γ‰vΓ©nement is a very simple event dispatching library for PHP.
PHP
1,239
star
2

retry

A tiny library for retrying failing operations.
PHP
542
star
3

get-in

Functions for for hash map (assoc array) traversal.
PHP
370
star
4

yolo

The microframework with swag.
PHP
201
star
5

IgorwFileServeBundle

Symfony2 Bundle for serving protected files.
PHP
167
star
6

doucheswag

Swag for douchebags.
PHP
135
star
7

EventSource

A PHP 5.3 library for creating an EventSource stream.
PHP
106
star
8

naegleria

A brainfuck compiler written in PHP. Also, a brain-eating amoeba.
PHP
89
star
9

compose

Function composition.
PHP
84
star
10

reasoned-php

A miniKanren in PHP.
Hack
74
star
11

trashbin

simple pastebin written in PHP
PHP
72
star
12

galapagos

Evolutionary language transformation.
PHP
72
star
13

CgiHttpKernel

Adapter from HttpKernelInterface to CGI.
PHP
60
star
14

composer-yaml

Tool to convert from composer.yml to composer.json.
PHP
58
star
15

silex-examples

Examples of Silex apps.
PHP
58
star
16

smaug

Here be dragons.
PHP
44
star
17

edn

Extensible Data Notation Parser.
PHP
42
star
18

stack-oauth

OAuth stack middleware.
PHP
38
star
19

turing-php

Turing machine emulator in PHP.
PHP
30
star
20

balrog

The tiny static site generator written in lisphp.
PHP
29
star
21

lambda-php

Lambda calculus interpreter in PHP.
PHP
22
star
22

ilias

Naive LISP implementation in PHP.
PHP
22
star
23

SocketServer

Stream-powered library for creating a socket server in PHP.
PHP
21
star
24

fab-symfony-console

Make symfony console output FABULOUS!
PHP
21
star
25

chicken-php

Chicken VM written in PHP.
PHP
21
star
26

whitespace-php

It's the little machine that you cannot see!
PHP
19
star
27

middleware

Demo of HttpKernel middlewares.
PHP
18
star
28

conway-php

Conway's Game of Life in PHP.
PHP
18
star
29

phpunit-phar

package phpunit as a standalone phar file.
PHP
17
star
30

webserver-zceu

PHP
17
star
31

websockets-talk

Samples from the WebSockets talk.
JavaScript
17
star
32

dev-zero

/dev/zero emulator, provides a large supply of zeroes.
PHP
12
star
33

FcgiHttpKernel

Adapter from HttpKernelInterface to FastCGI.
PHP
12
star
34

brainfuck-php

Brainfuck implementation.
PHP
8
star
35

dnsresolv

Foobar DnsResolv, based on Silex.
PHP
8
star
36

befunge-php

Befunge-98 implementation.
PHP
7
star
37

cute-little-interpreters

ROBOTS! IN YOUR COMPUTER!
PHP
7
star
38

clusterstream

A stream of the universe.
PHP
6
star
39

weblog

Weblog on igor.io.
CSS
5
star
40

matelight-blm

Bring back the old Blinkenlights to Mate Light.
PHP
5
star
41

rpn

Calculator.
PHP
4
star
42

wsm

Whitespace Assembly Language
PHP
4
star
43

composer-version-invalidation

A test project for composer version invalidation.
4
star
44

lusp

A lil' Lisp.
PHP
4
star
45

realtime-examples

Examples of real-time apps using websockets and eventstream.
PHP
4
star
46

webpaper

paper on the web
JavaScript
4
star
47

git-search

Index a number of git repositories into elasticsearch, view via web interface.
PHP
3
star
48

modcasts

phpbb modding screencasts
PHP
3
star
49

silencium

Multiplayer kick-ass taboo
Ruby
3
star
50

pip-phar

Compiles pip into a phar archive that can serve Silex apps.
PHP
3
star
51

playground

Shell
2
star
52

phpbb

Popular open-source bulletin board written in PHP
PHP
2
star
53

whyyy.computer

2
star
54

notponies

idea collection system based on phpBB
PHP
2
star
55

NelmioHelloBundle

PHP
2
star
56

polysolv

Recursive implementation of Newton's method for finding polynomial roots.
Java
2
star
57

craplog

This is one of the crappiest blogs you will ever find.
PHP
2
star
58

webserver-talk

PHP
2
star
59

ascraeus-foobar-extension

Foobar extension for the upcoming phpBB 3.1.
PHP
2
star
60

phpbb-github_profile_link

A phpBB 3.0 modification to let users specify their GitHub profile.
PHP
2
star
61

nbsp

2
star
62

septopus

Septopus is a sucky vocab trainer written in java.
Java
1
star
63

automata-php

Kleine automaten.
PHP
1
star
64

dotfiles

geeky unix shell config.
Vim Script
1
star
65

slave

automated phpBB installation, that's right.
PHP
1
star
66

createjs-presence-server

Sample implementation of a presence server for createjs.
1
star
67

bitserv

git-based wiki built on sinatra
Ruby
1
star
68

clj-polysolv

Recursive implementation of Newton's method for finding polynomial roots.
Clojure
1
star
69

ultragraph

Java application to simulate basic graph theory algorithms.
Java
1
star
70

bitserv-example

example wiki repo for bitserv
1
star
71

bertiebot

A ruby implementation of BertieBot.
Ruby
1
star
72

genders

phpBB Genders MOD
PHP
1
star
73

buster-docs

Ruby
1
star