Procházet zdrojové kódy

[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.
Jeremy Mikola před 14 roky
rodič
revize
f4c0af76e7

+ 15 - 6
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()
         ;

+ 27 - 0
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(