From 339eb082ed0fbbe3bc877c04f4b81611e40579e4 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 11 Dec 2010 21:53:08 -0500 Subject: [PATCH] Adding Twig_Environment::clearCacheFiles() to clear cache files that twig makes. Refactoring test cases in previous commit. Adding more tests for clearing cache files. Fixes #192 --- lib/Twig/Environment.php | 20 +++++++++++++++++++- test/Twig/Tests/FileCachingTest.php | 20 +++++++++++++++----- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index b1686ac..97e1f1b 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -32,6 +32,7 @@ class Twig_Environment protected $strictVariables; protected $unaryOperators; protected $binaryOperators; + protected $templateClassPrefix = '__TwigTemplate_'; /** * Constructor. @@ -187,7 +188,7 @@ class Twig_Environment */ public function getTemplateClass($name) { - return '__TwigTemplate_'.md5($this->loader->getCacheKey($name)); + return $this->templateClassPrefix.md5($this->loader->getCacheKey($name)); } /** @@ -229,6 +230,23 @@ class Twig_Environment $this->loadedTemplates = array(); } + /** + * Clears the template cache files on the filesystem. + * + * @return boolean success + */ + public function clearCacheFiles() + { + if ($this->cache) { + foreach(new DirectoryIterator($this->cache) as $fileInfo) { + if (strpos($fileInfo->getFilename(), $this->templateClassPrefix) === 0) { + @unlink($fileInfo->getPathname()); + } + } + } + return true; + } + public function getLexer() { if (null === $this->lexer) { diff --git a/test/Twig/Tests/FileCachingTest.php b/test/Twig/Tests/FileCachingTest.php index 17bacc5..f7f5993 100644 --- a/test/Twig/Tests/FileCachingTest.php +++ b/test/Twig/Tests/FileCachingTest.php @@ -4,6 +4,7 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase { protected $fileName; protected $tmpDir; + protected $env; function setUp() { @@ -11,21 +12,30 @@ class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase if (!is_writable($this->tmpDir)) { $this->markTestSkipped(sprintf('Cannot write to %s, cannot test file caching.', $this->tmpDir)); } + $this->env = new Twig_Environment(new Twig_Loader_String(), array('cache' => $this->tmpDir)); parent::setUp(); } function testWritingCacheFiles() { - $loader = new Twig_Loader_String(); - $env = new Twig_Environment($loader, array('cache' => $this->tmpDir)); - $name = 'This is just text.'; - $template = $env->loadTemplate($name); - $cacheFileName = $env->getCacheFilename($name); + $template = $this->env->loadTemplate($name); + $cacheFileName = $this->env->getCacheFilename($name); $this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.'); $this->fileName = $cacheFileName; } + + function testClearingCacheFiles() + { + $name = 'I will be deleted.'; + $template = $this->env->loadTemplate($name); + $cacheFileName = $this->env->getCacheFilename($name); + + $this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.'); + $this->assertTrue($this->env->clearCacheFiles()); + $this->assertFalse(file_exists($cacheFileName), 'Cache file was not cleared.'); + } function tearDown() { -- 1.7.2.5