Explorar o código

merged branch vicb/twig_cfg/2.0 (PR #3545)

Commits
-------

eee5065 [TwigBundle] Workaround a flaw in the design of the configuration (normalization)

Discussion
----------

[TwigBundle] Workaround a flaw in the design of the configuration (norma...

...lization)

see #2823

@Seldaek please comment.

---------------------------------------------------------------------------

by Seldaek at 2012-03-09T20:52:47Z

It seems fine at first glance. I don't have time to look at it in detail right now sorry.
Fabien Potencier %!s(int64=13) %!d(string=hai) anos
pai
achega
5840d05f13

+ 13 - 0
src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

@@ -36,6 +36,19 @@ class TwigExtension extends Extension
         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
         $loader->load('twig.xml');
         $loader->load('twig.xml');
 
 
+        foreach ($configs as &$config) {
+            if (isset($config['globals'])) {
+                foreach ($config['globals'] as $name => $value) {
+                    if (is_array($value) && isset($value['key'])) {
+                        $config['globals'][$name] = array(
+                            'key'   => $name,
+                            'value' => $config['globals'][$name]
+                        );
+                    }
+                }
+            }
+        }
+
         $configuration = new Configuration();
         $configuration = new Configuration();
         $config = $this->processConfiguration($configuration, $configs);
         $config = $this->processConfiguration($configuration, $configs);
 
 

+ 1 - 0
src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/php/full.php

@@ -9,6 +9,7 @@ $container->loadFromExtension('twig', array(
      'globals' => array(
      'globals' => array(
          'foo' => '@bar',
          'foo' => '@bar',
          'pi'  => 3.14,
          'pi'  => 3.14,
+         'bad' => array('key' => 'foo'),
      ),
      ),
      'auto_reload'         => true,
      'auto_reload'         => true,
      'autoescape'          => true,
      'autoescape'          => true,

+ 2 - 1
src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

@@ -3,8 +3,9 @@ twig:
         resources:
         resources:
             - MyBundle::form.html.twig
             - MyBundle::form.html.twig
     globals:
     globals:
-        foo: @bar
+        foo: "@bar"
         pi:  3.14
         pi:  3.14
+        bad: {key: foo}
     auto_reload:         true
     auto_reload:         true
     autoescape:          true
     autoescape:          true
     base_template_class: stdClass
     base_template_class: stdClass

+ 6 - 0
src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

@@ -64,6 +64,12 @@ class TwigExtensionTest extends TestCase
         $this->assertEquals('pi', $calls[1][1][0], '->load() registers variables as Twig globals');
         $this->assertEquals('pi', $calls[1][1][0], '->load() registers variables as Twig globals');
         $this->assertEquals(3.14, $calls[1][1][1], '->load() registers variables as Twig globals');
         $this->assertEquals(3.14, $calls[1][1][1], '->load() registers variables as Twig globals');
 
 
+        // Yaml and Php specific configs
+        if (in_array($format, array('yml', 'php'))) {
+            $this->assertEquals('bad', $calls[2][1][0], '->load() registers variables as Twig globals');
+            $this->assertEquals(array('key' => 'foo'), $calls[2][1][1], '->load() registers variables as Twig globals');
+        }
+
         // Twig options
         // Twig options
         $options = $container->getParameter('twig.options');
         $options = $container->getParameter('twig.options');
         $this->assertTrue($options['auto_reload'], '->load() sets the auto_reload option');
         $this->assertTrue($options['auto_reload'], '->load() sets the auto_reload option');