From c6e737ec31668d0d5461ffa1707c8da48d45d6a6 Mon Sep 17 00:00:00 2001 From: Jarred Stelfox Date: Tue, 16 Jul 2024 10:51:06 -0700 Subject: [PATCH 1/3] Add PSR16 adapter --- library/iFixit/Matryoshka/PSR16Adapter.php | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 library/iFixit/Matryoshka/PSR16Adapter.php diff --git a/library/iFixit/Matryoshka/PSR16Adapter.php b/library/iFixit/Matryoshka/PSR16Adapter.php new file mode 100644 index 0000000..85c9260 --- /dev/null +++ b/library/iFixit/Matryoshka/PSR16Adapter.php @@ -0,0 +1,63 @@ +scope = new Scope($backend, $scopeName); + } + + public function get(string $key, mixed $default = null): mixed { + return $this->scope->get($key) ?: $default; + } + + public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool { + return $this->scope->set($key, $value, $this->getSeconds($ttl)); + } + + public function delete(string $key): bool { + return $this->scope->delete($key); + } + + public function clear(): bool { + return $this->scope->deleteScope(); + } + + public function getMultiple(iterable $keys, mixed $default = null): iterable { + [$found, $missed] = $this->scope->getMultiple(iterator_to_array($keys)); + $missedWithDefault = []; + + foreach ($missed as $key => $_value) { + $missedWithDefault[$key] = $default; + } + + return [ + ...$found, + ...$missedWithDefault, + ]; + } + + public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool { + return $this->scope->setMultiple(iterator_to_array($values), $this->getSeconds($ttl)); + } + + public function deleteMultiple(iterable $keys): bool { + return $this->scope->deleteMultiple(iterator_to_array($keys)); + } + + public function has(string $key): bool { + $isMiss = $this->scope->get($key) === Backend::MISS; + return !$isMiss; + } + + private function getSeconds(null|int|DateInterval $secs) { + $seconds = $secs instanceof DateInterval ? (new DateTime('@0'))->add($secs)->getTimestamp() : $secs; + return $seconds ?: 0; + } +} From d4c3389eb4eee18762ac9869de7ca0bce0503bf9 Mon Sep 17 00:00:00 2001 From: Jarred Stelfox Date: Tue, 16 Jul 2024 11:02:02 -0700 Subject: [PATCH 2/3] composer require psr/simple-cache --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index aaeb4dc..d05dd40 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ } ], "require": { - "php": "^8.0" + "php": "^8.0", + "psr/simple-cache": "^3.0" }, "autoload": { "psr-0": {"iFixit": "library/"} From 08585b2df2e667ce1cf7ce36146b5bdb99cbfbad Mon Sep 17 00:00:00 2001 From: Jarred Stelfox Date: Tue, 16 Jul 2024 11:13:38 -0700 Subject: [PATCH 3/3] Psalm: Appease --- library/iFixit/Matryoshka/PSR16Adapter.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/library/iFixit/Matryoshka/PSR16Adapter.php b/library/iFixit/Matryoshka/PSR16Adapter.php index 85c9260..0783f80 100644 --- a/library/iFixit/Matryoshka/PSR16Adapter.php +++ b/library/iFixit/Matryoshka/PSR16Adapter.php @@ -30,25 +30,26 @@ public function clear(): bool { } public function getMultiple(iterable $keys, mixed $default = null): iterable { - [$found, $missed] = $this->scope->getMultiple(iterator_to_array($keys)); + /** + * @var array $missed + * @var array $found + */ + [$found, $missed] = $this->scope->getMultiple([...$keys]); $missedWithDefault = []; foreach ($missed as $key => $_value) { $missedWithDefault[$key] = $default; } - return [ - ...$found, - ...$missedWithDefault, - ]; + return array_merge($missedWithDefault, $found); } public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool { - return $this->scope->setMultiple(iterator_to_array($values), $this->getSeconds($ttl)); + return $this->scope->setMultiple([...$values], $this->getSeconds($ttl)); } public function deleteMultiple(iterable $keys): bool { - return $this->scope->deleteMultiple(iterator_to_array($keys)); + return $this->scope->deleteMultiple([...$keys]); } public function has(string $key): bool {