You've already forked redmine-bundle
made calls cacheable
This commit is contained in:
@@ -12,7 +12,9 @@
|
||||
"require": {
|
||||
"ext-curl": "*",
|
||||
"kbsali/redmine-api": "^2.7",
|
||||
"guzzlehttp/guzzle": "^7"
|
||||
"guzzlehttp/guzzle": "^7",
|
||||
"symfony/contracts": "^3.5",
|
||||
"symfony/cache": "^7.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.5 || ^10",
|
||||
|
||||
1300
composer.lock
generated
1300
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -5,40 +5,57 @@ namespace Magdev\RedmineBundle\Client;
|
||||
use Redmine\Client\Psr18Client;
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use GuzzleHttp\Psr7\HttpFactory as GuzzleHttpFactory;
|
||||
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||
|
||||
final class RedmineClient
|
||||
{
|
||||
private ?Psr18Client $client = null;
|
||||
private ?string $url = null;
|
||||
private ?string $apiKey = null;
|
||||
|
||||
public function __construct(
|
||||
string $url,
|
||||
string $apikeyOrUsername,
|
||||
?string $password = null
|
||||
) {
|
||||
$guzzle = new GuzzleClient();
|
||||
$factory = new GuzzleHttpFactory();
|
||||
private TagAwareCacheInterface $redmineApiCachePool
|
||||
) {}
|
||||
|
||||
$this->client = new Psr18Client(
|
||||
$guzzle,
|
||||
$factory,
|
||||
$factory,
|
||||
$url,
|
||||
$apikeyOrUsername,
|
||||
$password
|
||||
);
|
||||
public function setUrl(string $url): self
|
||||
{
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
|
||||
public function __call(mixed $name, mixed $arguments): mixed
|
||||
public function setApiKey(string $apiKey): self
|
||||
{
|
||||
if (!method_exists($this->client, $name)) {
|
||||
throw new \BadMethodCallException(sprintf(
|
||||
'Method %s, doesn\'t exist in class %s',
|
||||
$name,
|
||||
get_class($this->client)
|
||||
));
|
||||
}
|
||||
$this->apiKey = $apiKey;
|
||||
}
|
||||
|
||||
return call_user_func_array([$this->client, $name], $arguments);
|
||||
public function call(string $api, string $method, array $arguments = []): mixed
|
||||
{
|
||||
$cacheKey = sprintf('%s_%s_%s', $api, $method, sha1(serialize($arguments)));
|
||||
|
||||
return $this->redmineApiCachePool->get($cacheKey, function (ItemInterface $item): string {
|
||||
$item->expiresAfter(3600);
|
||||
$item->tag([$api, $method]);
|
||||
|
||||
$apiClient = $this->getClient()->getApi($api);
|
||||
$value = call_user_method_array($method, $apiClient, $arguments);
|
||||
|
||||
return $value;
|
||||
});
|
||||
}
|
||||
|
||||
public function getClient(): ?Psr18Client
|
||||
{
|
||||
if (!$this->client) {
|
||||
$guzzle = new GuzzleClient();
|
||||
$factory = new GuzzleHttpFactory();
|
||||
|
||||
$this->client = new Psr18Client(
|
||||
$guzzle,
|
||||
$factory,
|
||||
$factory,
|
||||
$this->url,
|
||||
$this->apiKey
|
||||
);
|
||||
}
|
||||
return $this->client;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,8 @@ final class MagdevRedmineExtension extends Extension
|
||||
$id = sprintf('magdev.redmine.%s', $name);
|
||||
|
||||
$container->register($id, RedmineClient::class)
|
||||
->setArguments([$connection['url'], $connection['apikey']]);
|
||||
->addMethodCall('setUrl', [$connection['url']]);
|
||||
->addMethodCall('setApiKey', [$connection['apikey']]);
|
||||
$container->registerAliasForArgument($id, RedmineClient::class, "{$name}Client");
|
||||
|
||||
if ($name === $config['default_connection']) {
|
||||
|
||||
Reference in New Issue
Block a user