Remove outdated doctrine/cache package and change configuration
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
namespace CloudObjects\SDK\Exceptions;
|
||||
|
||||
/**
|
||||
* An Exception that is thrown when the SDK's configuration isn't valid.
|
||||
*/
|
||||
class InvalidSDKConfigurationException extends \Exception {
|
||||
|
||||
}
|
||||
@@ -6,15 +6,23 @@
|
||||
|
||||
namespace CloudObjects\SDK;
|
||||
|
||||
use Exception;
|
||||
use DateTime, Exception;
|
||||
use ML\IRI\IRI, ML\JsonLD\JsonLD;
|
||||
use Doctrine\Common\Cache\RedisCache;
|
||||
use Psr\Log\LoggerInterface, Psr\Log\LoggerAwareTrait;
|
||||
use GuzzleHttp\ClientInterface, GuzzleHttp\Client, GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Kevinrob\GuzzleCache\CacheMiddleware, Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage;
|
||||
use GuzzleHttp\Psr7\Request,
|
||||
GuzzleHttp\Psr7\Response;
|
||||
use Kevinrob\GuzzleCache\CacheMiddleware;
|
||||
use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy;
|
||||
use CloudObjects\SDK\Exceptions\CoreAPIException;
|
||||
use Kevinrob\GuzzleCache\Storage\CacheStorageInterface,
|
||||
Kevinrob\GuzzleCache\Storage\Psr6CacheStorage,
|
||||
Kevinrob\GuzzleCache\Storage\Psr16CacheStorage,
|
||||
Kevinrob\GuzzleCache\CacheEntry;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use CloudObjects\SDK\Exceptions\CoreAPIException,
|
||||
CloudObjects\SDK\Exceptions\InvalidSDKConfigurationException;
|
||||
use CloudObjects\SDK\AccountGateway\AccountContext;
|
||||
|
||||
/**
|
||||
@@ -30,13 +38,14 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
||||
private $cache;
|
||||
private $objects;
|
||||
private $defaultReader;
|
||||
private $customCacheDefaultRequest;
|
||||
|
||||
const CO_API_URL = 'https://od.coid.link/';
|
||||
|
||||
public function __construct($options = []) {
|
||||
// Merge options with defaults
|
||||
$this->options = array_merge([
|
||||
'cache_provider' => 'none',
|
||||
'cache_storage' => null,
|
||||
'cache_prefix' => 'clobj:',
|
||||
'cache_ttl' => 60,
|
||||
'static_config_path' => null,
|
||||
@@ -48,35 +57,27 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
||||
'connect_timeout' => 5
|
||||
], $options);
|
||||
|
||||
// Set up object cache
|
||||
switch ($this->options['cache_provider']) {
|
||||
case 'none':
|
||||
// no caching
|
||||
$this->cache = null;
|
||||
break;
|
||||
case 'redis':
|
||||
// caching with Redis
|
||||
$redis = new \Redis();
|
||||
$redis->pconnect(
|
||||
isset($this->options['cache_provider.redis.host']) ? $this->options['cache_provider.redis.host'] : '127.0.0.1',
|
||||
isset($this->options['cache_provider.redis.port']) ? $this->options['cache_provider.redis.port'] : 6379);
|
||||
// Check object cache configuration
|
||||
if (isset($this->options['cache_storage'])
|
||||
&& !($this->options['cache_storage'] instanceof CacheStorageInterface)
|
||||
&& !($this->options['cache_storage'] instanceof CacheItemPoolInterface)
|
||||
&& !($this->options['cache_storage'] instanceof CacheInterface)
|
||||
) {
|
||||
throw new InvalidSDKConfigurationException('Invalid cache_storage specified; must be an instance of Kevinrob\GuzzleCache\CacheStorageInterface, Psr\Cache\CacheItemPoolInterface or Psr\SimpleCache\CacheInterface.');
|
||||
}
|
||||
|
||||
$this->cache = new RedisCache();
|
||||
$this->cache->setRedis($redis);
|
||||
break;
|
||||
case 'file':
|
||||
// caching on the filesystem
|
||||
$this->cache = new \Doctrine\Common\Cache\FilesystemCache(
|
||||
isset($this->options['cache_provider.file.directory']) ? $this->options['cache_provider.file.directory'] : sys_get_temp_dir()
|
||||
);
|
||||
break;
|
||||
default:
|
||||
throw new Exception('Valid values for cache_provider are: none, redis, file');
|
||||
if ($this->options['cache_storage'] instanceof CacheStorageInterface) {
|
||||
$this->cache = $this->options['cache_storage'];
|
||||
} elseif ($this->options['cache_storage'] instanceof CacheItemPoolInterface) {
|
||||
$this->cache = new Psr6CacheStorage($this->options['cache_storage']);
|
||||
} elseif ($this->options['cache_storage'] instanceof CacheInterface) {
|
||||
$this->cache = new Psr16CacheStorage($this->options['cache_storage']);
|
||||
}
|
||||
|
||||
// Set up logger
|
||||
if (is_a($this->options['logger'], LoggerInterface::class))
|
||||
if (is_a($this->options['logger'], LoggerInterface::class)) {
|
||||
$this->setLogger($this->options['logger']);
|
||||
}
|
||||
|
||||
// Set up handler stack
|
||||
$stack = HandlerStack::create();
|
||||
@@ -85,11 +86,14 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
||||
if (isset($this->cache)) {
|
||||
$stack->push(
|
||||
new CacheMiddleware(
|
||||
new PrivateCacheStrategy(
|
||||
new DoctrineCacheStorage($this->cache)
|
||||
)
|
||||
new PrivateCacheStrategy($this->cache)
|
||||
)
|
||||
);
|
||||
|
||||
// We also use the cache for storing object descriptions and attachments
|
||||
// without the HTTP request, so we create a dummy request; this way
|
||||
// we can reuse GuzzleCache for maximum compatibility
|
||||
$this->customCacheDefaultRequest = new Request('GET', '/');
|
||||
}
|
||||
|
||||
// Initialize client
|
||||
@@ -127,18 +131,32 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
|
||||
$this->logger->info($message, [ 'elapsed_ms' => round((microtime(true) - $ts) * 1000) ]);
|
||||
}
|
||||
|
||||
private function getCacheKey($id) {
|
||||
return $this->options['cache_prefix'].$this->options['auth_ns'].'/'.$id;
|
||||
public function getCacheKey($id) {
|
||||
return $this->options['cache_prefix'] . $this->options['auth_ns'] . '/' . $id;
|
||||
}
|
||||
|
||||
private function getFromCache($id) {
|
||||
return (isset($this->cache) && $this->cache->contains($this->getCacheKey($id)))
|
||||
? $this->cache->fetch($this->getCacheKey($id)) : null;
|
||||
if (!$this->cache) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$cachedData = $this->cache->fetch($this->getCacheKey($id));
|
||||
|
||||
if (!$cachedData) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $cachedData->getOriginalResponse()->getBody()->getContents();
|
||||
}
|
||||
|
||||
private function putIntoCache($id, $data, $ttl) {
|
||||
if (isset($this->cache))
|
||||
$this->cache->save($this->getCacheKey($id), $data, $ttl);
|
||||
if ($this->cache) {
|
||||
$entry = new CacheEntry($this->customCacheDefaultRequest,
|
||||
new Response(200, [], $data),
|
||||
((new DateTime)->modify('+'.$ttl.' seconds')));
|
||||
|
||||
$this->cache->save($this->getCacheKey($id), $entry, $ttl);
|
||||
}
|
||||
}
|
||||
|
||||
public function getFromCacheCustom($id) {
|
||||
|
||||
63
CloudObjects/SDK/TestHelpers/InMemoryLogger.php
Normal file
63
CloudObjects/SDK/TestHelpers/InMemoryLogger.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
namespace CloudObjects\SDK\TestHelpers;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class InMemoryLogger implements LoggerInterface {
|
||||
|
||||
private $logs = [];
|
||||
|
||||
public function getLogs() : array {
|
||||
return $this->logs;
|
||||
}
|
||||
|
||||
public function getLastLogMessage() : string {
|
||||
if (empty($this->logs)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return end($this->logs)['message'];
|
||||
}
|
||||
|
||||
public function emergency(string|\Stringable $message, array $context = []): void {
|
||||
$this->logs[] = [ 'level' => 'emergency', 'message' => (string)$message, 'context' => $context ];
|
||||
}
|
||||
|
||||
public function alert(string|\Stringable $message, array $context = []): void {
|
||||
$this->logs[] = [ 'level' => 'alert', 'message' => (string)$message, 'context' => $context ];
|
||||
}
|
||||
|
||||
public function critical(string|\Stringable $message, array $context = []): void {
|
||||
$this->logs[] = [ 'level' => 'critical', 'message' => (string)$message, 'context' => $context ];
|
||||
}
|
||||
|
||||
public function error(string|\Stringable $message, array $context = []): void {
|
||||
$this->logs[] = [ 'level' => 'error', 'message' => (string)$message, 'context' => $context ];
|
||||
}
|
||||
|
||||
public function warning(string|\Stringable $message, array $context = []): void {
|
||||
$this->logs[] = [ 'level' => 'warning', 'message' => (string)$message, 'context' => $context ];
|
||||
}
|
||||
|
||||
public function notice(string|\Stringable $message, array $context = []): void {
|
||||
$this->logs[] = [ 'level' => 'notice', 'message' => (string)$message, 'context' => $context ];
|
||||
}
|
||||
|
||||
public function info(string|\Stringable $message, array $context = []): void {
|
||||
$this->logs[] = [ 'level' => 'info', 'message' => (string)$message, 'context' => $context ];
|
||||
}
|
||||
|
||||
public function debug(string|\Stringable $message, array $context = []): void {
|
||||
$this->logs[] = [ 'level' => 'debug', 'message' => (string)$message, 'context' => $context ];
|
||||
}
|
||||
|
||||
public function log($level, string|\Stringable $message, array $context = []): void {
|
||||
$this->logs[] = [ 'level' => $level, 'message' => (string)$message, 'context' => $context ];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user