Skip to content

Commit

Permalink
bug #1953 rtrim cache dir (SpacePossum)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x branch.

Discussion
----------

rtrim cache dir

@see #1951

Commits
-------

c3ec2fb Make file cache tolerant for trailing (back)slashes on directory configuration.
  • Loading branch information
fabpot committed Jan 11, 2016
2 parents dba78c8 + c3ec2fb commit 4474392
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Twig/Cache/Filesystem.php
Expand Up @@ -27,7 +27,7 @@ class Twig_Cache_Filesystem implements Twig_CacheInterface
*/
public function __construct($directory, $options = 0)
{
$this->directory = $directory;
$this->directory = rtrim($directory, '\/').'/';
$this->options = $options;
}

Expand All @@ -38,7 +38,7 @@ public function generateKey($name, $className)
{
$hash = hash('sha256', $className);

return $this->directory.'/'.$hash[0].$hash[1].'/'.$hash.'.php';
return $this->directory.$hash[0].$hash[1].'/'.$hash.'.php';
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/Twig/Tests/Cache/FilesystemTest.php
Expand Up @@ -159,6 +159,31 @@ public function testGetTimestampMissingFile()
$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()
{
return strtr('<?php class {{classname}} {}', array(
Expand Down

0 comments on commit 4474392

Please sign in to comment.