Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/KeyNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of php-cache\cache-bundle package.
*
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Cache\CacheBundle;

/**
* A class to normalize cache keys.
*
* @author Tobias Nyholm <[email protected]>
*/
class KeyNormalizer
{
/**
* Remove all characters that is not supported by PSR6.
*
* @param $key
*/
public static function onlyValid($key)
{
return preg_replace('|[^A-Za-z0-9_\.]|', '', $key);
}

/**
* Remove all characters that are marked as reserved in PSR6.
*
* @param string $key
*/
public static function noInvalid($key)
{
return preg_replace('|[\{\}\(\)/\\\@\:]|', '', $key);
}
}
5 changes: 3 additions & 2 deletions src/Routing/CachingRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Cache\CacheBundle\Routing;

use Cache\CacheBundle\KeyNormalizer;
use Cache\Taggable\TaggableItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Routing\RequestContext;
Expand Down Expand Up @@ -125,7 +126,7 @@ private function getCacheItemMatch($pathinfo)
{
/** @type RequestContext $c */
$c = $this->getContext();
$key = sprintf('%s__%s__%s__%s', $c->getHost(), str_replace('/', '.', $pathinfo), $c->getMethod(), $c->getQueryString());
$key = sprintf('%s__%s__%s__%s', $c->getHost(), $pathinfo, $c->getMethod(), $c->getQueryString());

return $this->getCacheItemFromKey($key, 'match');
}
Expand Down Expand Up @@ -163,7 +164,7 @@ public function __call($method, $args)
*/
private function getCacheItemFromKey($key, $tag)
{
$item = $this->cache->getItem($key);
$item = $this->cache->getItem(KeyNormalizer::noInvalid($key));

if ($item instanceof TaggableItemInterface) {
$item->setTags(['router', $tag]);
Expand Down
31 changes: 31 additions & 0 deletions tests/KeyNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of php-cache\cache-bundle package.
*
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Cache\CacheBundle\Tests;

use Cache\CacheBundle\KeyNormalizer;

class KeyNormalizerTest extends \PHPUnit_Framework_TestCase
{
public function testOnlyValid()
{
$input = '%foo!bar-';
$expected = 'foobar';
$this->assertEquals($expected, KeyNormalizer::onlyValid($input));
}

public function testNoInvalid()
{
$input = '{foo@bar}';
$expected = 'foobar';
$this->assertEquals($expected, KeyNormalizer::noInvalid($input));
}
}