Skip to content

Commit

Permalink
[TwigBundle] Allow arbitrary variables to be accepted as values for g…
Browse files Browse the repository at this point in the history
…lobals

This fixes a regression introduced when TwigExtension was refactored to utilize the Config component.
  • Loading branch information
jmikola committed Feb 20, 2011
1 parent 608e443 commit f4c0af7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Expand Up @@ -78,12 +78,21 @@ private function addGlobalsSection(NodeBuilder $rootNode)
->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);
->ifTrue(function($v){ return is_string($v) && '@' === substr($v, 0, 1); })
->then(function($v){ return array('id' => substr($v, 1), 'type' => 'service'); })
->end()
->beforeNormalization()
->ifTrue(function($v){
if (is_array($v)) {
$keys = array_keys($v);
sort($keys);

return $keys !== array('id', 'type') && $keys !== array('value');
}

return true;
})
->then(function($v){ return array('value' => $v); })
->end()
->scalarNode('id')->end()
->scalarNode('type')
Expand All @@ -92,7 +101,7 @@ private function addGlobalsSection(NodeBuilder $rootNode)
->thenInvalid('The %s type is not supported')
->end()
->end()
->scalarNode('value')->end()
->variableNode('value')->end()
->end()
->end()
;
Expand Down
Expand Up @@ -86,6 +86,33 @@ public function testLoadFullConfiguration($format)
$this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
}

public function testGlobalsWithDifferentTypesAndValues()
{
$globals = array(
'array' => array(),
'false' => false,
'float' => 2.0,
'integer' => 3,
'null' => null,
'object' => new \stdClass(),
'string' => 'foo',
'true' => true,
);

$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$container->loadFromExtension('twig', array('globals' => $globals));
$this->compileContainer($container);

$calls = $container->getDefinition('twig')->getMethodCalls();

foreach ($calls as $call) {
list($name, $value) = each($globals);
$this->assertEquals($name, $call[1][0]);
$this->assertSame($value, $call[1][1]);
}
}

public function getFormats()
{
return array(
Expand Down

0 comments on commit f4c0af7

Please sign in to comment.