Skip to content

Instantly share code, notes, and snippets.

@drm
Created December 31, 2012 14:45
Show Gist options
  • Save drm/4420281 to your computer and use it in GitHub Desktop.
Save drm/4420281 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Gerard van Helden <gerard@zicht.nl>
* @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