Config Service Provider
Config Service Provider based on Illuminate Config package for Silex Microframework., (*1)
The Service Provider is installable with Composer:, (*2)
composer require nunopress/silex-config-service-provider
Parameters
config.path
Path defined to find every php files inside for loading., (*3)
config.environment (optional)
Search before in the defined path and then in the environment path (config.path/config.environment format). The service use config.merge_factory for help the developers to change only what you need in the different environment instead to write again all the configuration set., (*4)
Here a simple example:, (*5)
// 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:, (*6)
$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:, (*7)
// 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., (*8)
Registering
$app->register(new NunoPress\Silex\Config\Provider\ConfigServiceProvider(), [
'config.path' => __DIR__ . '/config',
'config.environment' => ($app['debug']) ? 'dev' : 'prod'
]);
Usage
The Config provider provides a config service:, (*9)
$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., (*10)
Traits
NunoPress\Silex\Config\Application\ConfigTrait adds the following shortcuts:, (*11)
config
Access to Config object for retrieve the key requested, for the second param you can define a default value., (*12)
$name = $app->config('app.name', 'NunoPress');
You can use that for set your configuration at runtime:, (*13)
$app->config([
'newvar' => 'newvalue'
]);
echo $app->config('newvar'); // print newvalue
Define this trait in your Application class:, (*14)
class App extends \Silex\Application
{
use \NunoPress\Silex\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:, (*15)
$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;
}));