diff --git a/composer.json b/composer.json index 9047c04..e7beb37 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ } ], "require": { - "php": ">=8.1.0" + "php": ">=8.1.0", + "psr/simple-cache": "^2.0 || ^3.0" }, "require-dev": { "lcobucci/coding-standard": "^11.0", diff --git a/composer.lock b/composer.lock index 8b80dd9..fcb4329 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,60 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "15caf1db569069edcb6962bf2579bf95", - "packages": [], + "content-hash": "33ff31143ee093ff78a64d215f60180e", + "packages": [ + { + "name": "psr/simple-cache", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "764e0b3939f5ca87cb904f570ef9be2d78a07865" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/764e0b3939f5ca87cb904f570ef9be2d78a07865", + "reference": "764e0b3939f5ca87cb904f570ef9be2d78a07865", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "support": { + "source": "https://github.com/php-fig/simple-cache/tree/3.0.0" + }, + "time": "2021-10-29T13:26:27+00:00" + } + ], "packages-dev": [ { "name": "dealerdirect/phpcodesniffer-composer-installer", diff --git a/src/Cache/Psr16Cache.php b/src/Cache/Psr16Cache.php new file mode 100644 index 0000000..5e2cad7 --- /dev/null +++ b/src/Cache/Psr16Cache.php @@ -0,0 +1,34 @@ +cache->get($key); + + if (is_array($result)) { + // @phpstan-ignore-next-line because we won´t be able to validate the array shape in a performant way + return $result; + } + + $data = $loader(); + $this->cache->set($key, $data); + + return $data; + } +} diff --git a/test/Cache/Psr16CacheTest.php b/test/Cache/Psr16CacheTest.php new file mode 100644 index 0000000..43e5b03 --- /dev/null +++ b/test/Cache/Psr16CacheTest.php @@ -0,0 +1,55 @@ + ['/' => ['test', []]]], []]; + + $adapter = new Psr16Cache($this->createDummyCache($data)); + $result = $adapter->get('test', static fn () => $generatedData); + + self::assertSame($generatedData, $result); + self::assertSame($generatedData, $data['test']); + + // Try again, now with a different callback + $result = $adapter->get('test', static fn () => [['POST' => ['/' => ['test', []]]], []]); + + self::assertSame($generatedData, $result); + } + + /** @param array $data */ + private function createDummyCache(array &$data): CacheInterface + { + $cache = $this->createMock(CacheInterface::class); + + $cache->method('get') + ->willReturnCallback( + static function (string $key, mixed $default) use (&$data): mixed { + return $data[$key] ?? $default; + }, + ); + + $cache->method('set') + ->willReturnCallback( + static function (string $key, mixed $value) use (&$data): bool { + $data[$key] = $value; + + return true; + }, + ); + + return $cache; + } +}