[Cache] Add CacheItem::validateKey utility method
This commit is contained in:
parent
2824db3da8
commit
af09cdef49
@ -15,7 +15,6 @@ use Psr\Cache\CacheItemInterface;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use Symfony\Component\Cache\CacheItem;
|
||||
use Symfony\Component\Cache\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
@ -31,7 +30,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
|
||||
|
||||
protected function __construct($namespace = '', $defaultLifetime = 0)
|
||||
{
|
||||
$this->namespace = $this->getId($namespace, true);
|
||||
$this->namespace = '' === $namespace ? '' : $this->getId($namespace);
|
||||
$this->createCacheItem = \Closure::bind(
|
||||
function ($key, $value, $isHit) use ($defaultLifetime) {
|
||||
$item = new CacheItem();
|
||||
@ -336,19 +335,9 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function getId($key, $ns = false)
|
||||
private function getId($key)
|
||||
{
|
||||
if (!is_string($key)) {
|
||||
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
|
||||
}
|
||||
if (!isset($key[0]) && !$ns) {
|
||||
throw new InvalidArgumentException('Cache key length must be greater than zero');
|
||||
}
|
||||
if (isset($key[strcspn($key, '{}()/\@:')])) {
|
||||
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
|
||||
}
|
||||
|
||||
return $this->namespace.$key;
|
||||
return $this->namespace.CacheItem::validateKey($key);
|
||||
}
|
||||
|
||||
private function generateItems($items, &$keys)
|
||||
|
@ -15,7 +15,6 @@ use Psr\Cache\CacheItemInterface;
|
||||
use Psr\Log\LoggerAwareInterface;
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
use Symfony\Component\Cache\CacheItem;
|
||||
use Symfony\Component\Cache\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
@ -74,7 +73,7 @@ class ArrayAdapter implements AdapterInterface, LoggerAwareInterface
|
||||
public function getItems(array $keys = array())
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$this->validateKey($key);
|
||||
CacheItem::validateKey($key);
|
||||
}
|
||||
|
||||
return $this->generateItems($keys, time());
|
||||
@ -85,7 +84,7 @@ class ArrayAdapter implements AdapterInterface, LoggerAwareInterface
|
||||
*/
|
||||
public function hasItem($key)
|
||||
{
|
||||
return isset($this->expiries[$this->validateKey($key)]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
|
||||
return isset($this->expiries[CacheItem::validateKey($key)]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,7 +102,7 @@ class ArrayAdapter implements AdapterInterface, LoggerAwareInterface
|
||||
*/
|
||||
public function deleteItem($key)
|
||||
{
|
||||
unset($this->values[$this->validateKey($key)], $this->expiries[$key]);
|
||||
unset($this->values[CacheItem::validateKey($key)], $this->expiries[$key]);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -169,21 +168,6 @@ class ArrayAdapter implements AdapterInterface, LoggerAwareInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
private function validateKey($key)
|
||||
{
|
||||
if (!is_string($key)) {
|
||||
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
|
||||
}
|
||||
if (!isset($key[0])) {
|
||||
throw new InvalidArgumentException('Cache key length must be greater than zero');
|
||||
}
|
||||
if (isset($key[strcspn($key, '{}()/\@:')])) {
|
||||
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
|
||||
}
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
private function generateItems(array $keys, $now)
|
||||
{
|
||||
$f = $this->createCacheItem;
|
||||
|
@ -14,7 +14,6 @@ namespace Symfony\Component\Cache\Adapter;
|
||||
use Psr\Cache\CacheItemInterface;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use Symfony\Component\Cache\CacheItem;
|
||||
use Symfony\Component\Cache\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
@ -31,7 +30,7 @@ class ProxyAdapter implements AdapterInterface
|
||||
public function __construct(CacheItemPoolInterface $pool, $namespace = '', $defaultLifetime = 0)
|
||||
{
|
||||
$this->pool = $pool;
|
||||
$this->namespace = $this->getId($namespace, true);
|
||||
$this->namespace = '' === $namespace ? '' : $this->getId($namespace);
|
||||
$this->namespaceLen = strlen($namespace);
|
||||
$this->createCacheItem = \Closure::bind(
|
||||
function ($key, $value, $isHit) use ($defaultLifetime) {
|
||||
@ -192,18 +191,8 @@ class ProxyAdapter implements AdapterInterface
|
||||
return $this->misses;
|
||||
}
|
||||
|
||||
private function getId($key, $ns = false)
|
||||
private function getId($key)
|
||||
{
|
||||
if (!is_string($key)) {
|
||||
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
|
||||
}
|
||||
if (!isset($key[0]) && !$ns) {
|
||||
throw new InvalidArgumentException('Cache key length must be greater than zero');
|
||||
}
|
||||
if (isset($key[strcspn($key, '{}()/\@:')])) {
|
||||
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
|
||||
}
|
||||
|
||||
return $this->namespace.$key;
|
||||
return $this->namespace.CacheItem::validateKey($key);
|
||||
}
|
||||
}
|
||||
|
@ -99,6 +99,30 @@ final class CacheItem implements CacheItemInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a cache key according to PSR-6.
|
||||
*
|
||||
* @param string $key The key to validate.
|
||||
*
|
||||
* @return string $key if it is valid.
|
||||
*
|
||||
* @throws InvalidArgumentException When $key is not valid.
|
||||
*/
|
||||
public static function validateKey($key)
|
||||
{
|
||||
if (!is_string($key)) {
|
||||
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
|
||||
}
|
||||
if (!isset($key[0])) {
|
||||
throw new InvalidArgumentException('Cache key length must be greater than zero');
|
||||
}
|
||||
if (isset($key[strcspn($key, '{}()/\@:')])) {
|
||||
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
|
||||
}
|
||||
|
||||
return $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal logging helper.
|
||||
*
|
||||
|
Reference in New Issue
Block a user