diff --git a/caches/NullCache.php b/caches/NullCache.php new file mode 100644 index 00000000..8e5c9e35 --- /dev/null +++ b/caches/NullCache.php @@ -0,0 +1,30 @@ +folder = $folder; - // create cache names - foreach (scandir($this->folder) as $file) { - if (preg_match('/^([^.]+)Cache\.php$/U', $file, $m)) { - $this->cacheNames[] = $m[1]; - } - } - } - - /** - * @param string|null $name The name of the cache e.g. "File", "Memcached" or "SQLite" - */ public function create(string $name = null): CacheInterface { $name ??= Configuration::getConfig('cache', 'type'); - $name = $this->sanitizeCacheName($name) . 'Cache'; - - if (! preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) { - throw new \InvalidArgumentException('Cache name invalid!'); + if (!$name) { + throw new \Exception('No cache type configured'); } - - $filePath = $this->folder . $name . '.php'; - if (!file_exists($filePath)) { - throw new \Exception('Invalid cache'); + $cacheNames = []; + foreach (scandir(PATH_LIB_CACHES) as $file) { + if (preg_match('/^([^.]+)Cache\.php$/U', $file, $m)) { + $cacheNames[] = $m[1]; + } } - $className = '\\' . $name; - return new $className(); - } - - protected function sanitizeCacheName(string $name) - { // Trim trailing '.php' if exists if (preg_match('/(.+)(?:\.php)/', $name, $matches)) { $name = $matches[1]; } - // Trim trailing 'Cache' if exists if (preg_match('/(.+)(?:Cache)$/i', $name, $matches)) { $name = $matches[1]; } - - if (in_array(strtolower($name), array_map('strtolower', $this->cacheNames))) { - $index = array_search(strtolower($name), array_map('strtolower', $this->cacheNames)); - return $this->cacheNames[$index]; + if (in_array(strtolower($name), array_map('strtolower', $cacheNames))) { + $index = array_search(strtolower($name), array_map('strtolower', $cacheNames)); + $name = $cacheNames[$index]; + } else { + throw new \InvalidArgumentException(sprintf('Invalid cache name: "%s"', $name)); } - return null; + if (! preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) { + throw new \InvalidArgumentException(sprintf('Invalid cache name: "%s"', $name)); + } + $className = $name . 'Cache'; + if (!file_exists(PATH_LIB_CACHES . $className . '.php')) { + throw new \Exception('Unable to find the cache file'); + } + return new $className(); } } diff --git a/tests/Bridges/MastodonBridgeTest.php b/tests/Bridges/MastodonBridgeTest.php new file mode 100644 index 00000000..23baf5b6 --- /dev/null +++ b/tests/Bridges/MastodonBridgeTest.php @@ -0,0 +1,17 @@ + ['type' => 'null']]); + $b = new \MastodonBridge(); + // https://bird.makeup/users/asmongold/remote_follow + $b->setDatas(['canusername' => '@asmongold@bird.makeup']); + $b->collectData(); + } +}