Commit: 008c04ebe308d8c5f5f915e263ea5f0b5d5c31a9

Author: Nate Abele | Date: 2010-03-08 18:31:57 -0500
Refactoring `\template\view\Compiler::template()` to simplify code branching. Adding heirarchy information to cache file names, automatically removing expired templates.
diff --git a/libraries/lithium/template/view/Compiler.php b/libraries/lithium/template/view/Compiler.php index a9758ed..f160fa1 100644 --- a/libraries/lithium/template/view/Compiler.php +++ b/libraries/lithium/template/view/Compiler.php @@ -38,22 +38,28 @@ class Compiler extends \lithium\core\StaticObject { $options += $defaults; $stats = stat($file); - $oname = basename($file, '.php'); + $dir = dirname($file); + $oname = basename(dirname($dir)) . '_' . basename($dir) . '_' . basename($file, '.php'); $template = "template_{$oname}_{$stats['ino']}_{$stats['mtime']}_{$stats['size']}.php"; $template = "{$options['path']}/{$template}"; - if (!file_exists($template)) { - $compiled = static::compile(file_get_contents($file)); - $success = $options['fallback'] && !is_writable(dirname($template)) - ? false : (file_put_contents($template, $compiled) !== false); + if (file_exists($template)) { + return $template; + } + $compiled = static::compile(file_get_contents($file)); - if (!$success && $options['fallback']) { - return $file; - } elseif (!$success && !$options['fallback']) { - throw new Exception('Could not write compiled template to cache'); + if (is_writable($cachePath) && file_put_contents($template, $compiled) !== false) { + foreach (glob("{$options['path']}/template_{$oname}_*.php") as $expired) { + if ($expired !== $template) { + unlink($expired); + } } + return $template; + } + if ($options['fallback']) { + return $file; } - return $template; + throw new Exception("Could not write compiled template {$template} to cache"); } public static function compile($string) { diff --git a/libraries/lithium/tests/cases/template/view/CompilerTest.php b/libraries/lithium/tests/cases/template/view/CompilerTest.php index 474034c..d263ff9 100644 --- a/libraries/lithium/tests/cases/template/view/CompilerTest.php +++ b/libraries/lithium/tests/cases/template/view/CompilerTest.php @@ -98,13 +98,33 @@ class CompilerTest extends \lithium\test\Unit { )); $this->assertEqual("{$this->_path}/{$this->_file}", $result); - $this->expectException('/Could not write compiled template to cache/'); + $this->expectException('/Could not write compiled template/'); $this->expectException('/failed to open stream/'); $result = Compiler::template("{$this->_path}/{$this->_file}", array( 'path' => LITHIUM_APP_PATH . '/foo', 'fallback' => false )); } + + public function testTemplateCacheHit() { + $path = LITHIUM_APP_PATH . '/resources/tmp/cache/templates'; + $original = Compiler::template("{$this->_path}/{$this->_file}", compact('path')); + $cache = glob("{$path}/*"); + clearstatcache(); + + $cached = Compiler::template("{$this->_path}/{$this->_file}", compact('path')); + $this->assertEqual($original, $cached); + $this->assertEqual($cache, glob("{$path}/*")); + + file_put_contents("{$this->_path}/{$this->_file}", "Updated"); + clearstatcache(); + $updated = Compiler::template("{$this->_path}/{$this->_file}", compact('path')); + $newCache = glob("{$path}/*"); + + $this->assertNotEqual($cache, $updated); + $this->assertEqual(count($cache), count($newCache)); + $this->assertNotEqual($cache, $newCache); + } } ?> \ No newline at end of file