*/
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) {
} 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()