Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.