Skip to content

Commit 812d524

Browse files
committed
Consolidate trait code
1 parent aec8f22 commit 812d524

4 files changed

Lines changed: 16 additions & 79 deletions

File tree

system/Psr/Cache/Item.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace CodeIgniter\Psr\Cache;
1313

14+
use CodeIgniter\Cache\Handlers\BaseHandler;
1415
use CodeIgniter\I18n\Time;
1516
use DateInterval;
1617
use DateTime;
1718
use DateTimeInterface;
19+
use InvalidArgumentException;
1820
use Psr\Cache\CacheItemInterface;
1921

2022
final class Item implements CacheItemInterface
@@ -54,28 +56,21 @@ final class Item implements CacheItemInterface
5456
/**
5557
* Validates a cache key according to PSR-6.
5658
*
57-
* @param string $key The key to validate
59+
* @param mixed $key The key to validate
5860
*
5961
* @throws CacheArgumentException When $key is not valid
60-
*
61-
* @see https://github.com/symfony/cache/blob/7b024c6726af21fd4984ac8d1eae2b9f3d90de88/CacheItem.php#L158
6262
*/
63-
public static function validateKey($key): string
63+
public static function validateKey($key)
6464
{
65-
if (! is_string($key))
66-
{
67-
throw new CacheArgumentException('Cache key must be a string');
68-
}
69-
if ($key === '')
65+
// Use the framework's Cache key validation
66+
try
7067
{
71-
throw new CacheArgumentException('Cache key cannot be empty.');
68+
BaseHandler::validateKey($key);
7269
}
73-
if (strpbrk($key, self::RESERVED_CHARACTERS) !== false)
70+
catch (InvalidArgumentException $e)
7471
{
75-
throw new CacheArgumentException('Cache key contains reserved characters ' . self::RESERVED_CHARACTERS);
72+
throw new CacheArgumentException($e->getMessage(), $e->getCode(), $e);
7673
}
77-
78-
return $key;
7974
}
8075

8176
/**

system/Psr/Cache/Pool.php

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,12 @@
1414
use CodeIgniter\Cache\CacheInterface;
1515
use CodeIgniter\I18n\Time;
1616
use Config\Cache;
17-
use Config\Services;
1817
use Psr\Cache\CacheItemInterface;
1918
use Psr\Cache\CacheItemPoolInterface;
2019

2120
final class Pool implements CacheItemPoolInterface
2221
{
23-
/**
24-
* The adapter to use.
25-
*
26-
* @var CacheInterface
27-
*/
28-
private $adapter;
22+
use SupportTrait;
2923

3024
/**
3125
* Deferred Items to be saved.
@@ -34,35 +28,6 @@ final class Pool implements CacheItemPoolInterface
3428
*/
3529
private $deferred = [];
3630

37-
/**
38-
* Initializes the underlying adapter
39-
* from an existing instance or from the
40-
* Cache Service (with optional config).
41-
*
42-
* @param object|null $object
43-
*
44-
* @throws CacheArgumentException
45-
*/
46-
public function __construct($object = null)
47-
{
48-
if (is_null($object))
49-
{
50-
$this->adapter = Services::cache();
51-
}
52-
elseif ($object instanceof Cache)
53-
{
54-
$this->adapter = Services::cache($object, false);
55-
}
56-
elseif ($object instanceof CacheInterface)
57-
{
58-
$this->adapter = $object;
59-
}
60-
else
61-
{
62-
throw new CacheArgumentException('Cache Pool constructor only accepts an adapter or configuration');
63-
}
64-
}
65-
6631
/**
6732
* Commits any deferred Items.
6833
*/

system/Psr/Cache/SimpleCache.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class SimpleCache implements CacheInterface
3333
*/
3434
public function get($key, $default = null)
3535
{
36-
self::validateKey($key);
36+
Item::validateKey($key);
3737

3838
$meta = $this->adapter->getMetaData($key);
3939

@@ -62,7 +62,7 @@ public function get($key, $default = null)
6262
*/
6363
public function set($key, $value, $ttl = null)
6464
{
65-
self::validateKey($key);
65+
Item::validateKey($key);
6666

6767
// Get TTL as an integer (seconds)
6868
if (is_null($ttl))
@@ -100,7 +100,7 @@ public function set($key, $value, $ttl = null)
100100
*/
101101
public function delete($key)
102102
{
103-
self::validateKey($key);
103+
Item::validateKey($key);
104104

105105
// Nonexistant keys return true
106106
if (! is_array($this->adapter->getMetaData($key)))
@@ -235,7 +235,7 @@ public function deleteMultiple($keys)
235235
*/
236236
public function has($key)
237237
{
238-
self::validateKey($key);
238+
Item::validateKey($key);
239239

240240
$meta = $this->adapter->getMetaData($key);
241241

system/Psr/Cache/SupportTrait.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111

1212
namespace CodeIgniter\Psr\Cache;
1313

14-
use CodeIgniter\Cache\Handlers\BaseHandler;
1514
use CodeIgniter\Cache\CacheInterface;
1615
use Config\Cache;
17-
use Config\Services;
18-
use InvalidArgumentException;
1916

2017
/**
2118
* Cache Support Trait
@@ -32,26 +29,6 @@ trait SupportTrait
3229
*/
3330
private $adapter;
3431

35-
/**
36-
* Validates a cache key according to PSR-6.
37-
*
38-
* @param mixed $key The key to validate
39-
*
40-
* @throws CacheArgumentException When $key is not valid
41-
*/
42-
public static function validateKey($key)
43-
{
44-
// Use the framework's Cache key validation
45-
try
46-
{
47-
BaseHandler::validateKey($key);
48-
}
49-
catch (InvalidArgumentException $e)
50-
{
51-
throw new CacheArgumentException($e->getMessage(), $e->getCode(), $e);
52-
}
53-
}
54-
5532
/**
5633
* Initializes the underlying adapter
5734
* from an existing instance or from the
@@ -65,11 +42,11 @@ public function __construct($object = null)
6542
{
6643
if (is_null($object))
6744
{
68-
$this->adapter = Services::cache();
45+
$this->adapter = service('cache');
6946
}
7047
elseif ($object instanceof Cache)
7148
{
72-
$this->adapter = Services::cache($object, false);
49+
$this->adapter = service('cache', $object, false);
7350
}
7451
elseif ($object instanceof CacheInterface)
7552
{

0 commit comments

Comments
 (0)