Upgrade problem
authordantleech <dan.t.leech@gmail.com>
Mon, 31 Dec 2012 14:04:50 +0000 (14:04 +0000)
committerFabien Potencier <fabien.potencier@gmail.com>
Wed, 2 Jan 2013 15:57:55 +0000 (16:57 +0100)
lib/Twig/Environment.php
test/Twig/Tests/EnvironmentTest.php

index d8e700e..562df4f 100644 (file)
@@ -972,8 +972,14 @@ class Twig_Environment
      */
     public function addGlobal($name, $value)
     {
-        if (($this->extensionInitialized || $this->runtimeInitialized) && !array_key_exists($name, $this->globals)) {
-            throw new LogicException(sprintf('Unable to add global "%s" as the runtime or the extensions have already been initialized.', $name));
+        if ($this->extensionInitialized || $this->runtimeInitialized) {
+            if (null === $this->globals) {
+                $this->initGlobals();
+            }
+            
+            if (!array_key_exists($name, $this->globals)) {
+                throw new LogicException(sprintf('Unable to add global "%s" as the runtime or the extensions have already been initialized.', $name));
+            }
         }
 
         if ($this->extensionInitialized || $this->runtimeInitialized) {
index 76f787d..5ec28d6 100644 (file)
@@ -107,6 +107,16 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
         } catch (LogicException $e) {
             $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
         }
+
+        // test adding globals after initRuntime without call to getGlobals
+        $twig = new Twig_Environment(new Twig_Loader_String());
+        $twig->initRuntime();
+        try {
+            $twig->addGlobal('bar', 'bar');
+            $this->fail();
+        } catch (LogicException $e) {
+            $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
+        }
     }
 
     public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()