From 49e1b5050720e2e2a3bdfaa5c07a09345e33addc Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 7 Jan 2016 12:25:39 +0100 Subject: [PATCH] fix #1 --- src/FilesystemCachePool.php | 12 +++++++++--- tests/FilesystemCachePoolTest.php | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tests/FilesystemCachePoolTest.php diff --git a/src/FilesystemCachePool.php b/src/FilesystemCachePool.php index 3f77ec6..315a766 100644 --- a/src/FilesystemCachePool.php +++ b/src/FilesystemCachePool.php @@ -12,6 +12,7 @@ namespace Cache\Adapter\Filesystem; use Cache\Adapter\Common\AbstractCachePool; +use Cache\Adapter\Common\Exception\InvalidArgumentException; use League\Flysystem\FileNotFoundException; use League\Flysystem\Filesystem; use Psr\Cache\CacheItemInterface; @@ -74,12 +75,17 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl) } /** - * @param $key + * @param string $key * - * @return mixed + * @return string + * @throws InvalidArgumentException */ private function getFilePath($key) { - return sprintf('%s/%s', self::CACHE_PATH, urlencode(base64_encode($key))); + if (!preg_match('|^[a-zA-Z0-9_\.: ]+$|', $key)) { + throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid keys must match [a-zA-Z0-9_\.:].', $key)); + } + + return sprintf('%s/%s', self::CACHE_PATH, $key); } } diff --git a/tests/FilesystemCachePoolTest.php b/tests/FilesystemCachePoolTest.php new file mode 100644 index 0000000..eff9023 --- /dev/null +++ b/tests/FilesystemCachePoolTest.php @@ -0,0 +1,23 @@ + + */ +class FilesystemCachePoolTest extends \PHPUnit_Framework_TestCase +{ + /** + * @expectedException \Psr\Cache\InvalidArgumentException + */ + public function testInvalidKey() + { + $pool = new FilesystemCachePool(new Filesystem(new Local(__DIR__.'/'))); + + $pool->getItem('test%string'); + } +} \ No newline at end of file