From: dantleech Date: Mon, 31 Dec 2012 14:04:50 +0000 (+0000) Subject: Upgrade problem X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=9f5fa532ef352ccc5881a791df9abaeb0ee57a83;p=web%2Fkonrad%2Ftwig.git Upgrade problem --- 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()