Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created February 18, 2011 16:04
Show Gist options
  • Select an option

  • Save jmikola/833874 to your computer and use it in GitHub Desktop.

Select an option

Save jmikola/833874 to your computer and use it in GitHub Desktop.
<?php
namespace Symfony\Bundle\TwigBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
/**
* TwigExtension configuration structure.
*
* @author Jeremy Mikola <[email protected]>
*/
class Configuration
{
/**
* Generates the configuration tree.
*
* @return \Symfony\Component\Config\Definition\NodeInterface
*/
public function getConfigTree()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('twig', 'array');
$rootNode
->scalarNode('cache_warmer')->end()
;
$this->addExtensionsSection($rootNode);
$this->addFormSection($rootNode);
$this->addGlobalsSection($rootNode);
$this->addTwigOptions($rootNode);
return $treeBuilder->buildTree();
}
private function addExtensionsSection(NodeBuilder $rootNode)
{
$rootNode
->fixXmlConfig('extension')
->arrayNode('extensions')
->prototype('scalar')
->beforeNormalization()
->ifTrue(function($v) { return is_array($v) && isset($v['id']); })
->then(function($v){ return $v['id']; })
->end()
->end()
->end()
;
}
private function addFormSection(NodeBuilder $rootNode)
{
$rootNode
->arrayNode('form')
->addDefaultsIfNotSet()
->fixXmlConfig('resource')
->arrayNode('resources')
->addDefaultsIfNotSet()
->defaultValue(array('TwigBundle::form.html.twig'))
->validate()
->always()
->then(function($v){
return array_merge(array('TwigBundle::form.html.twig'), $v);
})
->end()
->prototype('scalar')->end()
->end()
->end()
;
}
private function addGlobalsSection(NodeBuilder $rootNode)
{
$rootNode
->fixXmlConfig('global')
->arrayNode('globals')
->useAttributeAsKey('key')
->prototype('array')
->beforeNormalization()
->ifTrue(function($v){ return is_scalar($v); })
->then(function($v){
return ('@' === substr($v, 0, 1))
? array('id' => substr($v, 1), 'type' => 'service')
: array('value' => $v, 'type' => 'value');
})
->end()
->scalarNode('id')->end()
->scalarNode('type')
->addDefaultsIfNotSet()
->defaultValue('value')
->validate()
->ifNotInArray(array('service', 'value'))
->thenInvalid('The %s type is not supported')
->end()
->end()
->scalarNode('value')->end()
->end()
->end()
;
}
private function addTwigOptions(NodeBuilder $rootNode)
{
$rootNode
->scalarNode('autoescape')->end()
->scalarNode('base_template_class')->end()
->scalarNode('cache')
->addDefaultsIfNotSet()
->defaultValue('%kernel.cache_dir%/twig')
->end()
->scalarNode('charset')
->addDefaultsIfNotSet()
->defaultValue('%kernel.charset%')
->end()
->scalarNode('debug')
->addDefaultsIfNotSet()
->defaultValue('%kernel.debug%')
->end()
->scalarNode('strict_variables')->end()
->scalarNode('auto_reload')->end()
;
}
}
array(6) {
["extensions"]=>
array(0) {
}
["form"]=>
array(1) {
["resources"]=>
array(1) {
[0]=>
string(26) "TwigBundle::form.html.twig"
}
}
["globals"]=>
array(0) {
}
["cache"]=>
string(23) "%kernel.cache_dir%/twig"
["charset"]=>
string(16) "%kernel.charset%"
["debug"]=>
string(14) "%kernel.debug%"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment