Created
December 31, 2012 14:45
-
-
Save drm/4420281 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* @author Gerard van Helden <[email protected]> | |
* @copyright Zicht Online <http://zicht.nl> | |
*/ | |
require_once 'vendor/autoload.php'; | |
class MyConf implements \Symfony\Component\Config\Definition\ConfigurationInterface | |
{ | |
/** | |
* Generates the configuration tree builder. | |
* | |
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder | |
*/ | |
public function getConfigTreeBuilder() | |
{ | |
$treeBuilder = new \Symfony\Component\Config\Definition\Builder\TreeBuilder(); | |
$root = $treeBuilder->root('task'); | |
$root | |
->children() | |
->arrayNode('tasks') | |
->prototype('array') | |
->beforeNormalization()->always(function($v) { | |
$params = $v; | |
unset($params['type']); | |
return array( | |
'type' => $v['type'], | |
$v['type'] => $params | |
); | |
})->end() | |
->children() | |
->scalarNode('type')->end() | |
->arrayNode('a') | |
->children() | |
->variableNode('param_valid_for_a')->end() | |
->end() | |
->end() | |
->arrayNode('b') | |
->children() | |
->variableNode('param_valid_for_b')->end() | |
->end() | |
->end() | |
->end() | |
->end() | |
->end() | |
->end() | |
; | |
return $treeBuilder; | |
} | |
} | |
$yml = <<<EOYML | |
tasks: | |
- | |
type: "a" | |
param_valid_for_a: "value1" | |
- | |
type: "a" | |
param_valid_for_a: "value2" | |
# param_valid_for_b: "value2" | |
- | |
type: "b" | |
param_valid_for_b: "value" | |
EOYML; | |
$processor = new \Symfony\Component\Config\Definition\Processor(); | |
$configs = array(); | |
$configs[]= \Symfony\Component\Yaml\Yaml::parse($yml); | |
$result = $processor->processConfiguration(new MyConf(), $configs); | |
print_r($result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment