Created
August 15, 2014 09:50
-
-
Save magickatt/bb7108d276bce430cf35 to your computer and use it in GitHub Desktop.
Example of how to use the Symfony Config component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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