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

Skip to content

Commit 4297933

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

File tree

4 files changed

+33
-44
lines changed

4 files changed

+33
-44
lines changed

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
3131

3232
protected function __construct($namespace = '', $defaultLifetime = 0)
3333
{
34-
$this->namespace = $this->getId($namespace, true);
34+
$this->namespace = '' === $namespace ? '' : $this->getId($namespace);
3535
$this->createCacheItem = \Closure::bind(
3636
function ($key, $value, $isHit) use ($defaultLifetime) {
3737
$item = new CacheItem();
@@ -336,19 +336,9 @@ public function __destruct()
336336
}
337337
}
338338

339-
private function getId($key, $ns = false)
339+
private function getId($key)
340340
{
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;
341+
return $this->namespace.CacheItem::validateKey($key);
352342
}
353343

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

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getItem($key)
7474
public function getItems(array $keys = array())
7575
{
7676
foreach ($keys as $key) {
77-
$this->validateKey($key);
77+
CacheItem::validateKey($key);
7878
}
7979

8080
return $this->generateItems($keys, time());
@@ -85,7 +85,7 @@ public function getItems(array $keys = array())
8585
*/
8686
public function hasItem($key)
8787
{
88-
return isset($this->expiries[$this->validateKey($key)]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
88+
return isset($this->expiries[CacheItem::validateKey($key)]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
8989
}
9090

9191
/**
@@ -103,7 +103,7 @@ public function clear()
103103
*/
104104
public function deleteItem($key)
105105
{
106-
unset($this->values[$this->validateKey($key)], $this->expiries[$key]);
106+
unset($this->values[CacheItem::validateKey($key)], $this->expiries[$key]);
107107

108108
return true;
109109
}
@@ -169,21 +169,6 @@ public function commit()
169169
return true;
170170
}
171171

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-
187172
private function generateItems(array $keys, $now)
188173
{
189174
$f = $this->createCacheItem;

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ProxyAdapter implements AdapterInterface
3131
public function __construct(CacheItemPoolInterface $pool, $namespace = '', $defaultLifetime = 0)
3232
{
3333
$this->pool = $pool;
34-
$this->namespace = $this->getId($namespace, true);
34+
$this->namespace = '' === $namespace ? '' : $this->getId($namespace);
3535
$this->namespaceLen = strlen($namespace);
3636
$this->createCacheItem = \Closure::bind(
3737
function ($key, $value, $isHit) use ($defaultLifetime) {
@@ -192,18 +192,8 @@ public function getMisses()
192192
return $this->misses;
193193
}
194194

195-
private function getId($key, $ns = false)
195+
private function getId($key)
196196
{
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;
197+
return $this->namespace.CacheItem::validateKey($key);
208198
}
209199
}

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)