mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 19:06:40 +00:00
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
This commit is contained in:
committed by
Fabien Potencier
parent
d660ec6619
commit
339eb082ed
@@ -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) {
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user