Skip to content

Commit 7ecb7b4

Browse files
committed
Merge pull request #56 from php-cache/normalizer
Added a key normalizer
2 parents 157079e + 1416d11 commit 7ecb7b4

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

src/KeyNormalizer.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\CacheBundle;
13+
14+
/**
15+
* A class to normalize cache keys.
16+
*
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class KeyNormalizer
20+
{
21+
/**
22+
* Remove all characters that is not supported by PSR6.
23+
*
24+
* @param $key
25+
*/
26+
public static function onlyValid($key)
27+
{
28+
return preg_replace('|[^A-Za-z0-9_\.]|', '', $key);
29+
}
30+
31+
/**
32+
* Remove all characters that are marked as reserved in PSR6.
33+
*
34+
* @param string $key
35+
*/
36+
public static function noInvalid($key)
37+
{
38+
return preg_replace('|[\{\}\(\)/\\\@\:]|', '', $key);
39+
}
40+
}

src/Routing/CachingRouter.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Cache\CacheBundle\Routing;
1313

14+
use Cache\CacheBundle\KeyNormalizer;
1415
use Cache\Taggable\TaggableItemInterface;
1516
use Psr\Cache\CacheItemPoolInterface;
1617
use Symfony\Component\Routing\RequestContext;
@@ -125,7 +126,7 @@ private function getCacheItemMatch($pathinfo)
125126
{
126127
/** @type RequestContext $c */
127128
$c = $this->getContext();
128-
$key = sprintf('%s__%s__%s__%s', $c->getHost(), str_replace('/', '.', $pathinfo), $c->getMethod(), $c->getQueryString());
129+
$key = sprintf('%s__%s__%s__%s', $c->getHost(), $pathinfo, $c->getMethod(), $c->getQueryString());
129130

130131
return $this->getCacheItemFromKey($key, 'match');
131132
}
@@ -163,7 +164,7 @@ public function __call($method, $args)
163164
*/
164165
private function getCacheItemFromKey($key, $tag)
165166
{
166-
$item = $this->cache->getItem($key);
167+
$item = $this->cache->getItem(KeyNormalizer::noInvalid($key));
167168

168169
if ($item instanceof TaggableItemInterface) {
169170
$item->setTags(['router', $tag]);

tests/KeyNormalizerTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\cache-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\CacheBundle\Tests;
13+
14+
use Cache\CacheBundle\KeyNormalizer;
15+
16+
class KeyNormalizerTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testOnlyValid()
19+
{
20+
$input = '%foo!bar-';
21+
$expected = 'foobar';
22+
$this->assertEquals($expected, KeyNormalizer::onlyValid($input));
23+
}
24+
25+
public function testNoInvalid()
26+
{
27+
$input = '{foo@bar}';
28+
$expected = 'foobar';
29+
$this->assertEquals($expected, KeyNormalizer::noInvalid($input));
30+
}
31+
}

0 commit comments

Comments
 (0)