Config ServiceProvider for Silex
A configuration ServiceProvider for Silex based on
Symfony Config component with support for
YAML, TOML (0.2.0) and JSON., (*1)
, (*2)
Installation
Use Composer to install Yosyfmony ConfigServiceProvider package:, (*3)
Add the following to your composer.json and run composer update., (*4)
"require": {
"yosymfony/config-serviceprovider": "1.2-dev"
}
More informations about the package on
Packagist., (*5)
Usage
Register the ServiceProvider:, (*6)
$app->register(new ConfigServiceProvider());
You can set a collection of locations where it should look for files and
reference it only with the file's name, (*7)
$app->register(new ConfigServiceProvider(array(
__dir__.'/config',
'/var/www/general-config'
)));
Load a configuration file:
$repository = $app['configuration']->load('user.yml');
// or load with absolute path:
$repository = $app['configuration']->load('/var/config/user1.yml');
.dist files
This library have support to .dist files. The location of a file follow the next hierachy:, (*8)
- filename.ext
- filename.ext.dist (if filename.ext not exists)
Load inline configuration:
use Yosymfony\Silex\ConfigServiceProvider\Config;
$repository = $app['configuration']->load('server: "mail.yourname.com"', Config::TYPE_YAML);
// or
$repository = $app['configuration']->load('server = "mail.yourname.com"', Config::TYPE_TOML);
Repository
The configuration file is loaded to a repository. A repository is a wrapper with
array access interface that supply methods for validating configurations values
and merge with other repositories., (*9)
$repository->get('name', 'noname'); // If 'name' not exists return 'noname'
$repository['name']; // Get the element in 'name' key
Validating configurations
The values and the structure can be validated using a definition class from
Symfony Config component.
For example, for this configuratión file:, (*10)
# Yaml file
port: 25
server: "mail.yourname.com"
you can create the below definition:, (*11)
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
class MyConfigDefinitions implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root(0);
$rootNode->children()
->integerNode('port')
->end()
->scalarNode('server')
->end()
->end();
return $treeBuilder;
}
}
and check your repository: $repository->validateWith(new MyConfigDefinitions());
An exception will be thrown if any definition constraints are violated., (*12)
Operations
The operation mergeWith was deprecated since version 1.2.0 and replaced by
union method., (*13)
Unions
You can get the union of repository A with other B with C as result:
$resultC = $repositoryA->union($repositoryB);.
The values of $repositoryB have less priority than $repositoryA., (*14)
Intersections
You can get the intersection of repository A with other B with C as result:
$resultC = $repositoryA->intersection($repositoryB);.
The values of $repositoryB have less priority than $repositoryA., (*15)
Create a blank repository
Create a blank repository is too easy. You only need create a instance of
ConfigRepository and use the array interface or set method to insert new values:, (*16)
use Yosymfony\Silex\ConfigServiceProvider\ConfigRepository;
//...
$repository = new ConfigRepository();
$repository->set('key1', 'value1');
// or
$repository['key1'] = 'value1';
and the next, you merge with others or validate it., (*17)
Unit tests
You can run the unit tests with the following command:, (*18)
$ cd your-path/vendor/yosymfony/config-serviceprovider
$ composer.phar install --dev
$ phpunit