From 5218db779633b2292102863da464896deee0c8cb Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 26 Jan 2013 16:09:24 +0100 Subject: [PATCH] added an exception when trying to render a template when no loader has been set (closes #975) --- lib/Twig/Environment.php | 12 ++++++++---- test/Twig/Tests/EnvironmentTest.php | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index e3384af..bdda8b4 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -263,7 +263,7 @@ class Twig_Environment */ public function getTemplateClass($name, $index = null) { - return $this->templateClassPrefix.md5($this->loader->getCacheKey($name)).(null === $index ? '' : '_'.$index); + return $this->templateClassPrefix.md5($this->getLoader()->getCacheKey($name)).(null === $index ? '' : '_'.$index); } /** @@ -318,10 +318,10 @@ class Twig_Environment if (!class_exists($cls, false)) { if (false === $cache = $this->getCacheFilename($name)) { - eval('?>'.$this->compileSource($this->loader->getSource($name), $name)); + eval('?>'.$this->compileSource($this->getLoader()->getSource($name), $name)); } else { if (!is_file($cache) || ($this->isAutoReload() && !$this->isTemplateFresh($name, filemtime($cache)))) { - $this->writeCacheFile($cache, $this->compileSource($this->loader->getSource($name), $name)); + $this->writeCacheFile($cache, $this->compileSource($this->getLoader()->getSource($name), $name)); } require_once $cache; @@ -356,7 +356,7 @@ class Twig_Environment } } - return $this->loader->isFresh($name, $time); + return $this->getLoader()->isFresh($name, $time); } public function resolveTemplate($names) @@ -553,6 +553,10 @@ class Twig_Environment */ public function getLoader() { + if (null === $this->loader) { + throw new LogicException('You must set a loader first.'); + } + return $this->loader; } diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index fc2cef8..d71e2ec 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -11,6 +11,16 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase { + /** + * @expectedException LogicException + * @expectedExceptionMessage You must set a loader first. + */ + public function testRenderNoLoader() + { + $env = new Twig_Environment(); + $env->render('test'); + } + public function testAutoescapeOption() { $loader = new Twig_Loader_Array(array( -- 1.7.2.5