Skip to content

Instantly share code, notes, and snippets.

@magickatt
Created August 15, 2014 09:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magickatt/bb7108d276bce430cf35 to your computer and use it in GitHub Desktop.
Save magickatt/bb7108d276bce430cf35 to your computer and use it in GitHub Desktop.
Example of how to use the Symfony Config component
<?php
// Load application-specific configuration
try {
$basepath = __DIR__ . '/config';
$configuration = Yaml::parse($basepath . '/config.yml');
} catch (\InvalidArgumentException $exception) {
exit("Are you sure the configuration files exist?");
}
// Use a Symfony ConfigurationInterface object to specify the *.yml format
$yamlConfiguration = new \Configuration();
// Process the configuration files (merge one-or-more *.yml files)
$processor = new Processor();
$configuration = $processor->processConfiguration(
$yamlConfiguration,
array($configuration) // As many *.yml files as required
);
/*
* The class to handle the *.yml configuration should look
* something like this...
*/
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
class Configuration
{
/**
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('arbitary');
$rootNode->children()
->scalarNode('host')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('username')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('password')
->isRequired()
->cannotBeEmpty()
->end()
->booleanNode('bindRequiresDn')
->defaultTrue()
->end();
return $treeBuilder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment