protected $strictVariables;
protected $unaryOperators;
protected $binaryOperators;
+ protected $templateClassPrefix = '__TwigTemplate_';
/**
* Constructor.
*/
public function getTemplateClass($name)
{
- return '__TwigTemplate_'.md5($this->loader->getCacheKey($name));
+ return $this->templateClassPrefix.md5($this->loader->getCacheKey($name));
}
/**
$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) {
{
protected $fileName;
protected $tmpDir;
+ protected $env;
function setUp()
{
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()
{