From 9f5fa532ef352ccc5881a791df9abaeb0ee57a83 Mon Sep 17 00:00:00 2001 From: dantleech Date: Mon, 31 Dec 2012 14:04:50 +0000 Subject: [PATCH] Upgrade problem --- lib/Twig/Environment.php | 10 ++++++++-- test/Twig/Tests/EnvironmentTest.php | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index d8e700e..562df4f 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -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) { diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 76f787d..5ec28d6 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -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() -- 1.7.2.5