Deprecated: Assigning the return value of new by reference is deprecated in /home/bluestat/public_html/source/index.php on line 477
hoplite - Commitdiff - ViewGit - Blue Static

Fix more bugs in TemplateLoader and a singleton helper

Robert Sesek [2011-08-06 21:34]
Fix more bugs in TemplateLoader and a singleton helper
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);