Cache templates with the full template path rather than just the name.
authorRobert Sesek <rsesek@bluestatic.org>
Mon, 25 May 2015 21:44:48 +0000 (17:44 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Mon, 25 May 2015 21:44:48 +0000 (17:44 -0400)
This allows multiple TemplateLoaders to operate on different template_paths
but with the same template_cache.

testing/tests/views/template_loader_test.php
views/template_loader.php

index 8dfc66d785ee3bb63d5dd55613c51721425b0514..d903f4628f45a1fbbd540fe62759817d81057be2 100644 (file)
@@ -40,16 +40,18 @@ class TemplateLoaderTest extends \PHPUnit_Framework_TestCase
 
   public function testCacheMiss()
   {
+    $file_path = sprintf($this->fixture->template_path(), 'cache_test');
+
     $this->cache->expects($this->once())
                 ->method('GetTemplateDataForName')
-                ->with($this->equalTo('cache_test'))
+                ->with($this->equalTo($file_path))
                 ->will($this->returnValue(NULL));
 
-    $expected = file_get_contents(sprintf($this->fixture->template_path(), 'cache_test'));
+    $expected = file_get_contents($file_path);
 
     $this->cache->expects($this->once())
                 ->method('StoreCompiledTemplate')
-                ->with($this->equalTo('cache_test'),
+                ->with($this->equalTo($file_path),
                        $this->greaterThan(0),
                        $this->equalTo($expected));
 
@@ -59,10 +61,12 @@ class TemplateLoaderTest extends \PHPUnit_Framework_TestCase
 
   public function testCacheHit()
   {
+    $file_path = sprintf($this->fixture->template_path(), 'cache_test');
+
     $expected = 'Cache hit!';
     $this->cache->expects($this->once())
                 ->method('GetTemplateDataForName')
-                ->with($this->equalTo('cache_test'))
+                ->with($this->equalTo($file_path))
                 ->will($this->returnValue($expected));
 
     // The cache backend is only consulted once.
index 84d461e1f0be21cb2df261a675bcbd1c31b2cc2c..4a08b4abc5dad42bfb4f4a64404c3947c6aaba73 100644 (file)
@@ -114,20 +114,23 @@ class TemplateLoader
     if (!$this->cache_backend)
       return;
 
-    $fetch_templates = array();
+    $fetch_templates = [];
+    $paths_to_names = [];
     foreach ($templates AS $name) {
       // Do not re-cache templates that have already been cached.
       if (isset($this->cache[$name]))
         continue;
 
       $tpl_path = $this->_TemplatePath($name);
-      $fetch_templates[$name] = filemtime($tpl_path);
+      $fetch_templates[$tpl_path] = filemtime($tpl_path);
+      $paths_to_names[$tpl_path] = $name;
     }
 
     $profile = Profiling::IsProfilingEnabled();
 
     $cached_templates = $this->cache_backend->GetMultipleTemplates($fetch_templates);
-    foreach ($cached_templates AS $name => $data) {
+    foreach ($cached_templates AS $tpl_path => $data) {
+      $name = $paths_to_names[$tpl_path];
       $this->cache[$name] = Template::NewWithCompiledData($name, $data);
       if ($profile)
         $this->usage[$name] = 0;
@@ -162,7 +165,7 @@ class TemplateLoader
     if (!$this->cache_backend)
       return NULL;
 
-    $data = $this->cache_backend->GetTemplateDataForName($name, filemtime($tpl_path));
+    $data = $this->cache_backend->GetTemplateDataForName($tpl_path, filemtime($tpl_path));
     if (!$data)
       return NULL;
 
@@ -189,7 +192,7 @@ class TemplateLoader
 
     if ($this->cache_backend) {
       $this->cache_backend->StoreCompiledTemplate(
-          $name, filemtime($tpl_path), $template->template());
+          $tpl_path, filemtime($tpl_path), $template->template());
     }
 
     return $template;