diff --git a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php index ca44fccad5d98..de534e343b213 100644 --- a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php @@ -15,7 +15,6 @@ use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; use Symfony\Component\Cache\CacheItem; -use Symfony\Component\Cache\Exception\InvalidArgumentException; /** * @author Nicolas Grekas
@@ -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 @@ public function __destruct() } } - 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) diff --git a/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php b/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php index d00488911c838..b6ca68b3ecfa5 100644 --- a/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php @@ -15,7 +15,6 @@ use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; use Symfony\Component\Cache\CacheItem; -use Symfony\Component\Cache\Exception\InvalidArgumentException; /** * @author Nicolas Grekas
@@ -74,7 +73,7 @@ public function getItem($key) 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 @@ public function getItems(array $keys = array()) */ 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 @@ public function clear() */ 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 @@ public function commit() 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; diff --git a/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php b/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php index e8befafac91f5..332638f76cfdd 100644 --- a/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/ProxyAdapter.php @@ -14,7 +14,6 @@ use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Cache\CacheItem; -use Symfony\Component\Cache\Exception\InvalidArgumentException; /** * @author Nicolas Grekas
@@ -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 @@ public function getMisses() 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); } } diff --git a/src/Symfony/Component/Cache/CacheItem.php b/src/Symfony/Component/Cache/CacheItem.php index 4ec571ac83d38..ce50b8e2ced0e 100644 --- a/src/Symfony/Component/Cache/CacheItem.php +++ b/src/Symfony/Component/Cache/CacheItem.php @@ -99,6 +99,30 @@ public function expiresAfter($time) 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. *