From 2dbb698f2da9339190cc1e3d2e8bfb1f803ebc84 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 25 May 2015 17:44:48 -0400 Subject: [PATCH] Cache templates with the full template path rather than just the name. This allows multiple TemplateLoaders to operate on different template_paths but with the same template_cache. --- testing/tests/views/template_loader_test.php | 12 ++++++++---- views/template_loader.php | 13 ++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/testing/tests/views/template_loader_test.php b/testing/tests/views/template_loader_test.php index 8dfc66d..d903f46 100644 --- a/testing/tests/views/template_loader_test.php +++ b/testing/tests/views/template_loader_test.php @@ -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. diff --git a/views/template_loader.php b/views/template_loader.php index 84d461e..4a08b4a 100644 --- a/views/template_loader.php +++ b/views/template_loader.php @@ -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; -- 2.22.5