Make file cache tolerant for trailing (back)slashes on directory configuration.

This commit is contained in:
Possum
2016-01-05 23:14:09 +01:00
parent 23e47c5242
commit c3ec2fb1de
2 changed files with 27 additions and 2 deletions
+2 -2
View File
@@ -27,7 +27,7 @@ class Twig_Cache_Filesystem implements Twig_CacheInterface
*/ */
public function __construct($directory, $options = 0) public function __construct($directory, $options = 0)
{ {
$this->directory = $directory; $this->directory = rtrim($directory, '\/').'/';
$this->options = $options; $this->options = $options;
} }
@@ -38,7 +38,7 @@ class Twig_Cache_Filesystem implements Twig_CacheInterface
{ {
$hash = hash('sha256', $className); $hash = hash('sha256', $className);
return $this->directory.'/'.$hash[0].$hash[1].'/'.$hash.'.php'; return $this->directory.$hash[0].$hash[1].'/'.$hash.'.php';
} }
/** /**
+25
View File
@@ -159,6 +159,31 @@ class Twig_Tests_Cache_FilesystemTest extends PHPUnit_Framework_TestCase
$this->assertSame(0, $this->cache->getTimestamp($key)); $this->assertSame(0, $this->cache->getTimestamp($key));
} }
/**
* Test file cache is tolerant towards trailing (back)slashes on the configured cache directory.
*
* @dataProvider provideDirectories
*/
public function testGenerateKey($expected, $input)
{
$cache = new Twig_Cache_Filesystem($input);
$this->assertRegExp($expected, $cache->generateKey('_test_', get_class($this)));
}
public function provideDirectories()
{
$pattern = '#a/b/[a-zA-Z0-9]+/[a-zA-Z0-9]+.php$#';
return array(
array($pattern, 'a/b'),
array($pattern, 'a/b/'),
array($pattern, 'a/b\\'),
array($pattern, 'a/b\\/'),
array($pattern, 'a/b\\//'),
array('#/'.substr($pattern, 1), '/a/b'),
);
}
private function generateSource() private function generateSource()
{ {
return strtr('<?php class {{classname}} {}', array( return strtr('<?php class {{classname}} {}', array(