From 5abad160816a177b0e49dfda53d2df3af58633ea Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 15 Jan 2013 20:48:33 +0100 Subject: [PATCH] relaxed globals management to avoid a BC break (closes #965) --- CHANGELOG | 1 + doc/deprecated.rst | 7 +++++++ lib/Twig/Environment.php | 4 +++- test/Twig/Tests/EnvironmentTest.php | 2 ++ 4 files changed, 13 insertions(+), 1 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5165e76..83656dd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.12.1 (2013-XX-XX) + * relaxed globals management to avoid a BC break * added support for {{ some_string[:2] }} * 1.12.0 (2013-01-08) diff --git a/doc/deprecated.rst b/doc/deprecated.rst index 3b2df29..f0a3a0f 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -89,3 +89,10 @@ Interfaces * ``Twig_ParserInterface`` (use ``Twig_Parser`` instead) * ``Twig_ExistsLoaderInterface`` (merged with ``Twig_LoaderInterface``) * ``Twig_TemplateInterface`` (use ``Twig_Template`` instead) + +Globals +------- + +* As of Twig 2.x, the ability to register a global variable after the runtime + or the extensions have been initialized is not possible anymore (but + changing the value of an already registered global is possible). diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index ea2b98f..90f4aa3 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -996,10 +996,12 @@ class Twig_Environment if (null === $this->globals) { $this->initGlobals(); } - + + /* This condition must be uncommented in Twig 2.0 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 5ec28d6..fc2cef8 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -71,6 +71,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase $globals = $twig->getGlobals(); $this->assertEquals('bar', $globals['foo']); + /* to be uncomment in Twig 2.0 // globals cannot be added after runtime init $twig = new Twig_Environment(new Twig_Loader_String()); $twig->addGlobal('foo', 'foo'); @@ -117,6 +118,7 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase } catch (LogicException $e) { $this->assertFalse(array_key_exists('bar', $twig->getGlobals())); } + */ } public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate() -- 1.7.2.5