diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php index 07f2a12046..c133165afe 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php @@ -78,12 +78,21 @@ class Configuration ->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') @@ -92,7 +101,7 @@ class Configuration ->thenInvalid('The %s type is not supported') ->end() ->end() - ->scalarNode('value')->end() + ->variableNode('value')->end() ->end() ->end() ; diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php index 9abe53ebfa..c8b0f2e0c4 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php @@ -86,6 +86,33 @@ class TwigExtensionTest extends TestCase $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(