Skip to content

Commit

Permalink
Compatibility with psr/simple-cache ^2.0 and ^3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Oct 18, 2024
1 parent a83a5dd commit c7e03fa
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 79 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [2.4.0] - 2024-10-18

### Added

- Compatibility with `psr/simple-cache` ^2.0 and ^3.0

## [2.3.0] - 2023-09-04

### Changed
Expand Down
6 changes: 3 additions & 3 deletions src/Cache/AbstractCacheDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class AbstractCacheDriver implements CacheInterface
* @inheritDoc
* @throws CacheException
*/
public function getMultiple($keys, $default = null): array
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
if (!is_iterable($keys)) {
throw new InvalidArgumentCacheException('First argument must be iterable');
Expand All @@ -53,7 +53,7 @@ public function getMultiple($keys, $default = null): array
* @inheritDoc
* @throws CacheException
*/
public function setMultiple($values, $ttl = null): bool
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
if (!is_iterable($values)) {
throw new InvalidArgumentCacheException('First argument must be iterable');
Expand All @@ -75,7 +75,7 @@ public function setMultiple($values, $ttl = null): bool
* @inheritDoc
* @throws CacheException
*/
public function deleteMultiple($keys): bool
public function deleteMultiple(iterable $keys): bool
{
if (!is_iterable($keys)) {
throw new InvalidArgumentCacheException('First argument must be iterable');
Expand Down
15 changes: 8 additions & 7 deletions src/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Berlioz\Core\Cache;

use Berlioz\Core\Directories\DirectoriesInterface;
use DateInterval;
use Psr\SimpleCache\CacheInterface;

/**
Expand Down Expand Up @@ -62,23 +63,23 @@ public function getClass(): string|false
/**
* @inheritDoc
*/
public function get($key, $default = null): mixed
public function get(string $key, mixed $default = null): mixed
{
return $this->cache->get($key, $default);
}

/**
* @inheritDoc
*/
public function set($key, $value, $ttl = null): bool
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
return $this->cache->set($key, $value, $ttl);
}

/**
* @inheritDoc
*/
public function delete($key): bool
public function delete(string $key): bool
{
return $this->cache->delete($key);
}
Expand All @@ -94,31 +95,31 @@ public function clear(): bool
/**
* @inheritDoc
*/
public function getMultiple($keys, $default = null): iterable
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
return $this->cache->getMultiple($keys, $default);
}

/**
* @inheritDoc
*/
public function setMultiple($values, $ttl = null): bool
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
return $this->cache->setMultiple($values, $ttl);
}

/**
* @inheritDoc
*/
public function deleteMultiple($keys): bool
public function deleteMultiple(iterable $keys): bool
{
return $this->cache->deleteMultiple($keys);
}

/**
* @inheritDoc
*/
public function has($key)
public function has(string $key): bool
{
return $this->cache->has($key);
}
Expand Down
9 changes: 5 additions & 4 deletions src/Cache/FileCacheDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Berlioz\Core\Directories\DirectoriesInterface;
use Berlioz\Core\Exception\CacheException;
use DateInterval;
use Exception;
use Psr\SimpleCache\CacheInterface;

Expand All @@ -39,7 +40,7 @@ public function __construct(protected DirectoriesInterface $directories)
* @inheritDoc
* @throws CacheException
*/
public function has($key): bool
public function has(string $key): bool
{
$this->controlKey($key);
$cacheFilename = $this->getFilename($key);
Expand All @@ -55,7 +56,7 @@ public function has($key): bool
* @inheritDoc
* @throws CacheException
*/
public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
$this->controlKey($key);
$cacheFilename = $this->getFilename($key);
Expand Down Expand Up @@ -85,7 +86,7 @@ public function get($key, $default = null)
* @inheritDoc
* @throws CacheException
*/
public function set($key, $value, $ttl = null): bool
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
$this->controlKey($key);
$cacheFilename = $this->getFilename($key);
Expand Down Expand Up @@ -116,7 +117,7 @@ public function set($key, $value, $ttl = null): bool
* @inheritDoc
* @throws CacheException
*/
public function delete($key): bool
public function delete(string $key): bool
{
$this->controlKey($key);
$cacheFilename = $this->getFilename($key);
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/KeyControlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait KeyControlTrait
*
* @throws InvalidArgumentCacheException
*/
protected function controlKey($key)
protected function controlKey($key): void
{
if (!is_string($key)) {
throw new InvalidArgumentCacheException('Invalid key name for cache, must be string');
Expand Down
9 changes: 5 additions & 4 deletions src/Cache/MemoryCacheDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Berlioz\Core\Cache;

use Berlioz\Core\Exception\CacheException;
use DateInterval;
use Psr\SimpleCache\CacheInterface;

/**
Expand All @@ -27,7 +28,7 @@ class MemoryCacheDriver extends AbstractCacheDriver implements CacheInterface
/**
* @inheritDoc
*/
public function has($key): bool
public function has(string $key): bool
{
$this->controlKey($key);

Expand All @@ -41,7 +42,7 @@ public function has($key): bool
/**
* @inheritDoc
*/
public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
$this->controlKey($key);

Expand All @@ -56,7 +57,7 @@ public function get($key, $default = null)
* @inheritDoc
* @throws CacheException
*/
public function set($key, $value, $ttl = null): bool
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
$this->controlKey($key);

Expand All @@ -71,7 +72,7 @@ public function set($key, $value, $ttl = null): bool
/**
* @inheritDoc
*/
public function delete($key): bool
public function delete(string $key): bool
{
$this->controlKey($key);

Expand Down
17 changes: 9 additions & 8 deletions src/Cache/NullCacheDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Berlioz\Core\Cache;

use Berlioz\Core\Exception\InvalidArgumentCacheException;
use DateInterval;
use Psr\SimpleCache\CacheInterface;

/**
Expand All @@ -27,7 +28,7 @@ class NullCacheDriver implements CacheInterface
/**
* @inheritDoc
*/
public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
$this->controlKey($key);

Expand All @@ -37,7 +38,7 @@ public function get($key, $default = null)
/**
* @inheritDoc
*/
public function set($key, $value, $ttl = null)
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
$this->controlKey($key);

Expand All @@ -47,7 +48,7 @@ public function set($key, $value, $ttl = null)
/**
* @inheritDoc
*/
public function delete($key)
public function delete(string $key): bool
{
$this->controlKey($key);

Expand All @@ -57,15 +58,15 @@ public function delete($key)
/**
* @inheritDoc
*/
public function clear()
public function clear(): bool
{
return true;
}

/**
* @inheritDoc
*/
public function getMultiple($keys, $default = null)
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
if (!is_iterable($keys)) {
throw new InvalidArgumentCacheException('First argument must be iterable');
Expand All @@ -79,7 +80,7 @@ public function getMultiple($keys, $default = null)
/**
* @inheritDoc
*/
public function setMultiple($values, $ttl = null)
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
if (!is_iterable($values)) {
throw new InvalidArgumentCacheException('First argument must be iterable');
Expand All @@ -93,7 +94,7 @@ public function setMultiple($values, $ttl = null)
/**
* @inheritDoc
*/
public function deleteMultiple($keys)
public function deleteMultiple(iterable $keys): bool
{
if (!is_iterable($keys)) {
throw new InvalidArgumentCacheException('First argument must be iterable');
Expand All @@ -107,7 +108,7 @@ public function deleteMultiple($keys)
/**
* @inheritDoc
*/
public function has($key)
public function has(string $key): bool
{
$this->controlKey($key);

Expand Down
23 changes: 0 additions & 23 deletions tests/Cache/AbstractCacheDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public function providerSet()
return [
['key', 'value', true],
['key-key', new stdClass(), true],
[['array'], 'value', false],
[new stdClass(), 'value', false],
['', 'value', false]
];
}
Expand Down Expand Up @@ -141,13 +139,6 @@ public function testSetMultiple($dataSet, $ttl)
$this->assertTrue($this->getCacheDriver()->setMultiple($dataSet, $ttl));
}

public function testSetMultiple_withNonIterable()
{
$this->expectException(InvalidArgumentCacheException::class);

$this->getCacheDriver()->setMultiple('foo');
}

/**
* @dataProvider providerMultiple
*/
Expand All @@ -166,13 +157,6 @@ public function testGetMultiple($dataSet)
$this->assertEquals($result, $values);
}

public function testGetMultiple_withNonIterable()
{
$this->expectException(InvalidArgumentCacheException::class);

$this->getCacheDriver()->getMultiple('foo');
}

/**
* @dataProvider providerMultiple
*/
Expand All @@ -189,11 +173,4 @@ public function testDeleteMultiple($dataSet)
$values = $this->getCacheDriver()->getMultiple($keys);
$this->assertEquals([], array_filter($values));
}

public function testDeleteMultiple_withNonIterable()
{
$this->expectException(InvalidArgumentCacheException::class);

$this->getCacheDriver()->deleteMultiple('foo');
}
}
24 changes: 0 additions & 24 deletions tests/Cache/NullCacheDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,44 +53,20 @@ public function testGetMultiple()
$this->assertEquals(['bar' => 'qux', 'foo' => 'qux'], $nullDriver->getMultiple(['bar', 'foo'], 'qux'));
}

public function testGetMultiple_withNonIterable()
{
$this->expectException(InvalidArgumentCacheException::class);

$nullDriver = new NullCacheDriver();
$nullDriver->getMultiple('foo');
}

public function testSetMultiple()
{
$nullDriver = new NullCacheDriver();

$this->assertTrue($nullDriver->setMultiple(['bar' => 'qux', 'foo' => 'qux']));
}

public function testSetMultiple_withNonIterable()
{
$this->expectException(InvalidArgumentCacheException::class);

$nullDriver = new NullCacheDriver();
$nullDriver->setMultiple('foo');
}

public function testDeleteMultiple()
{
$nullDriver = new NullCacheDriver();

$this->assertTrue($nullDriver->deleteMultiple(['bar', 'foo']));
}

public function testDeleteMultiple_withNonIterable()
{
$this->expectException(InvalidArgumentCacheException::class);

$nullDriver = new NullCacheDriver();
$nullDriver->deleteMultiple('foo');
}

public function testClear()
{
$nullDriver = new NullCacheDriver();
Expand Down
Loading

0 comments on commit c7e03fa

Please sign in to comment.