Browse Source

[TwigBundle] added a way to register Twig globals from configuration

    <twig:config debug="%kernel.debug%" strict-variables="%kernel.debug%">
        <twig:global key="foo" id="request" />
    </twig:config>

    twig.config:
        globals:
          foo: request
Fabien Potencier 14 years ago
parent
commit
7fdc61f272

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

@@ -5,6 +5,7 @@ namespace Symfony\Bundle\TwigBundle\DependencyInjection;
 use Symfony\Component\DependencyInjection\Extension\Extension;
 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
 
 /*
  * This file is part of the Symfony framework.
@@ -44,6 +45,20 @@ class TwigExtension extends Extension
             }
         }
 
+        // globals
+        $def = $container->getDefinition('twig');
+        $globals = $this->fixConfig($config, 'global');
+        if (isset($globals[0])) {
+            foreach ($globals as $global) {
+                $def->addMethodCall('addGlobal', array($global['key'], new Reference($global['id'])));
+            }
+        } else {
+            foreach ($globals as $key => $id) {
+                $def->addMethodCall('addGlobal', array($key, new Reference($id)));
+            }
+        }
+        unset($config['globals'], $config['global']);
+
         // convert - to _
         foreach ($config as $key => $value) {
             $config[str_replace('-', '_', $key)] = $value;
@@ -71,4 +86,21 @@ class TwigExtension extends Extension
     {
         return 'twig';
     }
+
+    protected function fixConfig($config, $key)
+    {
+        $values = array();
+        if (isset($config[$key.'s'])) {
+            $values = $config[$key.'s'];
+        } elseif (isset($config[$key])) {
+            if (is_string($config[$key]) || !is_int(key($config[$key]))) {
+                // only one
+                $values = array($config[$key]);
+            } else {
+                $values = $config[$key];
+            }
+        }
+
+        return $values;
+    }
 }

+ 6 - 0
src/Symfony/Bundle/TwigBundle/Resources/config/schema/twig-1.0.xsd

@@ -10,6 +10,7 @@
     <xsd:complexType name="config">
         <xsd:sequence>
             <xsd:element name="form" type="form" minOccurs="0" maxOccurs="1" />
+            <xsd:element name="global" type="global" minOccurs="0" maxOccurs="1" />
         </xsd:sequence>
 
         <xsd:attribute name="charset" type="xsd:string" />
@@ -26,4 +27,9 @@
             <xsd:element name="resource" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
         </xsd:sequence>
     </xsd:complexType>
+
+    <xsd:complexType name="global">
+        <xsd:attribute name="key" type="xsd:string" />
+        <xsd:attribute name="id" type="xsd:string" />
+    </xsd:complexType>
 </xsd:schema>

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

@@ -30,4 +30,23 @@ class TwigExtensionTest extends TestCase
         $this->assertEquals('ISO-8859-1', $options['charset'], '->configLoad() overrides existing configuration options');
         $this->assertEquals('%kernel.debug%', $options['debug'], '->configLoad() merges the new values with the old ones');
     }
+
+    public function testConfigGlobals()
+    {
+        // XML
+        $container = new ContainerBuilder();
+        $loader = new TwigExtension();
+        $loader->configLoad(array('global' => array(array('key' => 'foo', 'id' => 'bar'))), $container);
+        $config = $container->getDefinition('twig')->getMethodCalls();
+        $this->assertEquals('foo', $config[0][1][0]);
+        $this->assertEquals('bar', (string) $config[0][1][1]);
+
+        // YAML, PHP
+        $container = new ContainerBuilder();
+        $loader = new TwigExtension();
+        $loader->configLoad(array('globals' => array('foo' => 'bar')), $container);
+        $config = $container->getDefinition('twig')->getMethodCalls();
+        $this->assertEquals('foo', $config[0][1][0]);
+        $this->assertEquals('bar', (string) $config[0][1][1]);
+    }
 }