Pimple Config Service Provider

Pimple Config Service Provider based on Illuminate Config package for Silex Microframework or any Pimple Container project’s.

Tip

The Service Provider is installable with Composer:

composer require nunopress/pimple-config-service-provider

Parameters

config.path

Path defined to find every php files inside for loading.

config.environment (optional)

Search before in the defined path and then in the environment path (config.path/config.environment format). The service use array_replace_recursive for help the developers to change only what you need in the different environment instead to write again all the configuration set.

Here a simple example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

// config/view.php
return [
    'twig' => [
        'path' => realpath(__DIR__ . '/../views'),
        'options' => [
            'debug' => false,
            'cache' => realpath(__DIR__ . '/../../storage/cache/twig')
        ]
    ]
];

// config/development/view.php
return [
    'twig' => [
        'options' => [
            'debug' => true,
            'cache' => false
        ]
    ]
];

// RESULT
[
    'twig' => [
        'path' => realpath(__DIR__ . '/../views'),
        'options' => [
            'debug' => true,
            'cache' => false
        ]
    ]
]

config.merge_factory (optional)

You can configure your merge method instead to use the default merge factory array_replace_recursive:

1
2
3
4
5
<?php

$app['config.merge_factory'] = $app->share($app->protect('config.merge_factory', function (array $old, array $new) {
    return array_merge($old, $new);
}));

Services

For access to config keys you need to use the filename (without extension) before every config keys, example:

<?php

// config/view.php
return [
    'test' => 'yep'
];

// Access to test key
$app['config']->get('view.test'); // Result: yep

config

The Illuminate\Config\Repository instance. The main way to interact with Config.

Registering

1
2
3
4
5
6
<?php

$app->register(new NunoPress\Pimple\Config\Provider\ConfigServiceProvider(), [
    'config.path' => __DIR__ . '/config',
    'config.environment' => ($app['debug']) ? 'dev' : 'prod'
]);

Usage

The Config provider provides a config service:

1
2
3
4
5
6
7
<?php

$app->get('/hello', function () use ($app) {
    $name = $app['config']->get('app.name', 'NunoPress');

    return 'Hello ' . $name . '!!';
});

Note

Read the Config reference for the Illuminate Config document to learn more about the various Config functions.

Traits

NunoPress\Pimple\Config\Application\ConfigTrait adds the following shortcuts:

config

Access to Config object for retrieve the key requested, for the second param you can define a default value.

<?php

$name = $app->config('app.name', 'NunoPress');

Define this trait in your Application class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php

class App extends \Silex\Application
{
    use \NunoPress\Pimple\Config\Application\ConfigTrait;
}

$app = new App();

$name = $app->config('app.name', 'NunoPress');

Customization

You can configure the Config object before using it by extending the config service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php

$app['config'] = $app->share($app->extend('config', function ($config, $app) {
    // Instead to have separate the config items you can share it in the current container
    $items = $config->all();

    foreach ($items as $name => $item) {
        $app[$name] = $item;
    }

    return $config;
}));