From bd4b5b5ac4d6d6a3cf8dbbaa1ec98189ff5d7524 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 6 Aug 2011 17:34:45 -0400 Subject: [PATCH] Fix more bugs in TemplateLoader and a singleton helper --- testing/tests/views/template_loader_test.php | 17 +++++++++++++++-- views/template_loader.php | 10 ++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/testing/tests/views/template_loader_test.php b/testing/tests/views/template_loader_test.php index a602573..a784493 100644 --- a/testing/tests/views/template_loader_test.php +++ b/testing/tests/views/template_loader_test.php @@ -47,8 +47,8 @@ class TemplateLoaderTest extends \PHPUnit_Framework_TestCase protected function _SetUpPaths() { - $this->fixture->set_template_path(dirname(__FILE__) . '/'); - $this->fixture->set_cache_path($this->fixture->template_path() . 'cache/'); + $this->fixture->set_template_path(dirname(__FILE__) . '/%s.tpl'); + $this->fixture->set_cache_path(dirname(__FILE__) . '/cache/'); } public function testTemplatePath() @@ -135,4 +135,17 @@ class TemplateLoaderTest extends \PHPUnit_Framework_TestCase $actual = file_get_contents($this->fixture->cache_path() . '/cache_test.phpi'); $this->assertEquals($expected, $actual); } + + public function testSingleton() + { + $fixture = views\TemplateLoader::GetInstance(); + $this->assertNotSame($fixture, $this->fixture); + + views\TemplateLoader::SetInstance($this->fixture); + $this->assertSame(views\TemplateLoader::GetInstance(), $this->fixture); + + $this->_SetUpPaths(); + $template = views\TemplateLoader::Fetch('cache_test'); + $this->assertEquals('This file should be cached.', $template->template()); + } } diff --git a/views/template_loader.php b/views/template_loader.php index 11ae296..65c59b5 100644 --- a/views/template_loader.php +++ b/views/template_loader.php @@ -51,7 +51,7 @@ class TemplateLoader $class = get_called_class(); self::$instance = new $class(); } - return self::$instance(); + return self::$instance; } /*! Sets the singleton instance. */ static public function SetInstance($instance) { self::$instance = $instance; } @@ -90,6 +90,12 @@ class TemplateLoader return clone $template; } + /*! Convenience function for loading templates. */ + static public function Fetch($name) + { + return self::GetInstance()->Load($name); + } + /*! Loads a cached filesystem template if it is up-to-date. @@ -129,7 +135,7 @@ class TemplateLoader $data = @file_get_contents($tpl_path); if ($data === FALSE) - throw new TemplateLoaderException('Could not load template ' . $this->template_name); + throw new TemplateLoaderException('Could not load template ' . $name); $template = Template::NewWithData($data); -- 2.22.5