Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af09cde

Browse files
[Cache] Add CacheItem::validateKey utility method
1 parent 2824db3 commit af09cde

File tree

4 files changed

+33
-47
lines changed

4 files changed

+33
-47
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Psr\Log\LoggerAwareInterface;
1616
use Psr\Log\LoggerAwareTrait;
1717
use Symfony\Component\Cache\CacheItem;
18-
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1918

2019
/**
2120
* @author Nicolas Grekas <[email protected]>
@@ -31,7 +30,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
3130

3231
protected function __construct($namespace = '', $defaultLifetime = 0)
3332
{
34-
$this->namespace = $this->getId($namespace, true);
33+
$this->namespace = '' === $namespace ? '' : $this->getId($namespace);
3534
$this->createCacheItem = \Closure::bind(
3635
function ($key, $value, $isHit) use ($defaultLifetime) {
3736
$item = new CacheItem();
@@ -336,19 +335,9 @@ public function __destruct()
336335
}
337336
}
338337

339-
private function getId($key, $ns = false)
338+
private function getId($key)
340339
{
341-
if (!is_string($key)) {
342-
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
343-
}
344-
if (!isset($key[0]) && !$ns) {
345-
throw new InvalidArgumentException('Cache key length must be greater than zero');
346-
}
347-
if (isset($key[strcspn($key, '{}()/\@:')])) {
348-
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
349-
}
350-
351-
return $this->namespace.$key;
340+
return $this->namespace.CacheItem::validateKey($key);
352341
}
353342

354343
private function generateItems($items, &$keys)

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Psr\Log\LoggerAwareInterface;
1616
use Psr\Log\LoggerAwareTrait;
1717
use Symfony\Component\Cache\CacheItem;
18-
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1918

2019
/**
2120
* @author Nicolas Grekas <[email protected]>
@@ -74,7 +73,7 @@ public function getItem($key)
7473
public function getItems(array $keys = array())
7574
{
7675
foreach ($keys as $key) {
77-
$this->validateKey($key);
76+
CacheItem::validateKey($key);
7877
}
7978

8079
return $this->generateItems($keys, time());
@@ -85,7 +84,7 @@ public function getItems(array $keys = array())
8584
*/
8685
public function hasItem($key)
8786
{
88-
return isset($this->expiries[$this->validateKey($key)]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
87+
return isset($this->expiries[CacheItem::validateKey($key)]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
8988
}
9089

9190
/**
@@ -103,7 +102,7 @@ public function clear()
103102
*/
104103
public function deleteItem($key)
105104
{
106-
unset($this->values[$this->validateKey($key)], $this->expiries[$key]);
105+
unset($this->values[CacheItem::validateKey($key)], $this->expiries[$key]);
107106

108107
return true;
109108
}
@@ -169,21 +168,6 @@ public function commit()
169168
return true;
170169
}
171170

172-
private function validateKey($key)
173-
{
174-
if (!is_string($key)) {
175-
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
176-
}
177-
if (!isset($key[0])) {
178-
throw new InvalidArgumentException('Cache key length must be greater than zero');
179-
}
180-
if (isset($key[strcspn($key, '{}()/\@:')])) {
181-
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
182-
}
183-
184-
return $key;
185-
}
186-
187171
private function generateItems(array $keys, $now)
188172
{
189173
$f = $this->createCacheItem;

src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Cache\CacheItemPoolInterface;
1616
use Symfony\Component\Cache\CacheItem;
17-
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1817

1918
/**
2019
* @author Nicolas Grekas <[email protected]>
@@ -31,7 +30,7 @@ class ProxyAdapter implements AdapterInterface
3130
public function __construct(CacheItemPoolInterface $pool, $namespace = '', $defaultLifetime = 0)
3231
{
3332
$this->pool = $pool;
34-
$this->namespace = $this->getId($namespace, true);
33+
$this->namespace = '' === $namespace ? '' : $this->getId($namespace);
3534
$this->namespaceLen = strlen($namespace);
3635
$this->createCacheItem = \Closure::bind(
3736
function ($key, $value, $isHit) use ($defaultLifetime) {
@@ -192,18 +191,8 @@ public function getMisses()
192191
return $this->misses;
193192
}
194193

195-
private function getId($key, $ns = false)
194+
private function getId($key)
196195
{
197-
if (!is_string($key)) {
198-
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
199-
}
200-
if (!isset($key[0]) && !$ns) {
201-
throw new InvalidArgumentException('Cache key length must be greater than zero');
202-
}
203-
if (isset($key[strcspn($key, '{}()/\@:')])) {
204-
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
205-
}
206-
207-
return $this->namespace.$key;
196+
return $this->namespace.CacheItem::validateKey($key);
208197
}
209198
}

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,30 @@ public function expiresAfter($time)
9999
return $this;
100100
}
101101

102+
/**
103+
* Validates a cache key according to PSR-6.
104+
*
105+
* @param string $key The key to validate.
106+
*
107+
* @return string $key if it is valid.
108+
*
109+
* @throws InvalidArgumentException When $key is not valid.
110+
*/
111+
public static function validateKey($key)
112+
{
113+
if (!is_string($key)) {
114+
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
115+
}
116+
if (!isset($key[0])) {
117+
throw new InvalidArgumentException('Cache key length must be greater than zero');
118+
}
119+
if (isset($key[strcspn($key, '{}()/\@:')])) {
120+
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
121+
}
122+
123+
return $key;
124+
}
125+
102126
/**
103127
* Internal logging helper.
104128
*

0 commit comments

Comments
 (0)