Skip to content

Commit f4c0af7

Browse files
committed
[TwigBundle] Allow arbitrary variables to be accepted as values for globals
This fixes a regression introduced when TwigExtension was refactored to utilize the Config component.
1 parent 608e443 commit f4c0af7

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,21 @@ private function addGlobalsSection(NodeBuilder $rootNode)
7878
->useAttributeAsKey('key')
7979
->prototype('array')
8080
->beforeNormalization()
81-
->ifTrue(function($v){ return is_scalar($v); })
82-
->then(function($v){
83-
return ('@' === substr($v, 0, 1))
84-
? array('id' => substr($v, 1), 'type' => 'service')
85-
: array('value' => $v);
81+
->ifTrue(function($v){ return is_string($v) && '@' === substr($v, 0, 1); })
82+
->then(function($v){ return array('id' => substr($v, 1), 'type' => 'service'); })
83+
->end()
84+
->beforeNormalization()
85+
->ifTrue(function($v){
86+
if (is_array($v)) {
87+
$keys = array_keys($v);
88+
sort($keys);
89+
90+
return $keys !== array('id', 'type') && $keys !== array('value');
91+
}
92+
93+
return true;
8694
})
95+
->then(function($v){ return array('value' => $v); })
8796
->end()
8897
->scalarNode('id')->end()
8998
->scalarNode('type')
@@ -92,7 +101,7 @@ private function addGlobalsSection(NodeBuilder $rootNode)
92101
->thenInvalid('The %s type is not supported')
93102
->end()
94103
->end()
95-
->scalarNode('value')->end()
104+
->variableNode('value')->end()
96105
->end()
97106
->end()
98107
;

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,33 @@ public function testLoadFullConfiguration($format)
8686
$this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
8787
}
8888

89+
public function testGlobalsWithDifferentTypesAndValues()
90+
{
91+
$globals = array(
92+
'array' => array(),
93+
'false' => false,
94+
'float' => 2.0,
95+
'integer' => 3,
96+
'null' => null,
97+
'object' => new \stdClass(),
98+
'string' => 'foo',
99+
'true' => true,
100+
);
101+
102+
$container = $this->createContainer();
103+
$container->registerExtension(new TwigExtension());
104+
$container->loadFromExtension('twig', array('globals' => $globals));
105+
$this->compileContainer($container);
106+
107+
$calls = $container->getDefinition('twig')->getMethodCalls();
108+
109+
foreach ($calls as $call) {
110+
list($name, $value) = each($globals);
111+
$this->assertEquals($name, $call[1][0]);
112+
$this->assertSame($value, $call[1][1]);
113+
}
114+
}
115+
89116
public function getFormats()
90117
{
91118
return array(

0 commit comments

Comments
 (0)