Removed ability to use class paths as Cache DSN protocols.

This commit is contained in:
Pachira 2024-10-16 01:26:08 +00:00
parent bc407f4a8a
commit b4ff61ad96
3 changed files with 10 additions and 23 deletions

View file

@ -1 +1 @@
0.2410.160117
0.2410.160124

View file

@ -1,7 +1,7 @@
<?php
// CacheTools.php
// Created: 2024-04-10
// Updated: 2024-10-02
// Updated: 2024-10-16
namespace Index\Cache;
@ -17,9 +17,9 @@ use RuntimeException;
* the rest of the URL is described in the documentation of the parseDsn implementation of the respective backend.
*
* The scheme can be a PHP classpath to an implementation of CacheBackend, or any of these aliases:
* - `array`, `null`: maps to `ArrayCache\ArrayCacheBackend` or `Index-Cache-ArrayCache-ArrayCacheBackend`, uses a simple array backed cache that doesn't get saved.
* - `memcached`, `memcache`: maps to `Memcached\MemcachedBackend` or `Index-Cache-Memcached-MemcachedBackend`, provides a backend based on the memcached or memcache extension.
* - `valkey`, `keydb`, `redis`: maps to `Valkey\ValkeyBackend` or `Index-Cache-Valkey-ValkeyBackend`, provides a backend based on the phpredis extension.
* - `array`, `null`: maps to `ArrayCache\ArrayCacheBackend`, uses a simple array backed cache that doesn't get saved.
* - `memcached`, `memcache`: maps to `Memcached\MemcachedBackend`, provides a backend based on the memcached or memcache extension.
* - `valkey`, `keydb`, `redis`: maps to `Valkey\ValkeyBackend`, provides a backend based on the phpredis extension.
*
* Short names are currently hardcoded and cannot be expanded.
*/
@ -52,24 +52,14 @@ final class CacheTools {
$backend = $backends[$scheme] ?? null;
if(!($backend instanceof CacheBackend)) {
$backend = null;
if(array_key_exists($scheme, self::CACHE_PROTOS))
$name = self::CACHE_PROTOS[$scheme];
else
$name = str_replace('-', '\\', (string)$scheme);
if(class_exists($name) && is_subclass_of($name, CacheBackend::class)) {
$backend = new $name;
$name = get_class($backend);
}
if($backend === null)
if(!array_key_exists($scheme, self::CACHE_PROTOS))
throw new RuntimeException('No implementation is available for the specified scheme.');
$backend = new (self::CACHE_PROTOS[$scheme]);
if(!$backend->isAvailable())
throw new RuntimeException('Requested cache backend is not available, likely due to missing dependencies.');
$backends[$name] = $backend;
$backends[$scheme] = $backend;
}
return $backend;

View file

@ -1,7 +1,7 @@
<?php
// CacheToolsTest.php
// Created: 2024-04-10
// Updated: 2024-10-02
// Updated: 2024-10-16
declare(strict_types=1);
@ -25,9 +25,6 @@ final class CacheToolsTest extends TestCase {
public function testBasicDSN(): void {
$arrayCache = CacheTools::create('array:');
$this->assertInstanceOf(ArrayCacheProvider::class, $arrayCache);
$arrayCache = CacheTools::create('Index-Cache-ArrayCache-ArrayCacheBackend:');
$this->assertInstanceOf(ArrayCacheProvider::class, $arrayCache);
}
public function testMemcachedDSN(): void {