You've already forked redmine-bundle
made calls cacheable
This commit is contained in:
@@ -12,7 +12,9 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"ext-curl": "*",
|
"ext-curl": "*",
|
||||||
"kbsali/redmine-api": "^2.7",
|
"kbsali/redmine-api": "^2.7",
|
||||||
"guzzlehttp/guzzle": "^7"
|
"guzzlehttp/guzzle": "^7",
|
||||||
|
"symfony/contracts": "^3.5",
|
||||||
|
"symfony/cache": "^7.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^9.5 || ^10",
|
"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 Redmine\Client\Psr18Client;
|
||||||
use GuzzleHttp\Client as GuzzleClient;
|
use GuzzleHttp\Client as GuzzleClient;
|
||||||
use GuzzleHttp\Psr7\HttpFactory as GuzzleHttpFactory;
|
use GuzzleHttp\Psr7\HttpFactory as GuzzleHttpFactory;
|
||||||
|
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||||
|
|
||||||
final class RedmineClient
|
final class RedmineClient
|
||||||
{
|
{
|
||||||
private ?Psr18Client $client = null;
|
private ?Psr18Client $client = null;
|
||||||
|
private ?string $url = null;
|
||||||
|
private ?string $apiKey = null;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
string $url,
|
private TagAwareCacheInterface $redmineApiCachePool
|
||||||
string $apikeyOrUsername,
|
) {}
|
||||||
?string $password = null
|
|
||||||
) {
|
|
||||||
$guzzle = new GuzzleClient();
|
|
||||||
$factory = new GuzzleHttpFactory();
|
|
||||||
|
|
||||||
$this->client = new Psr18Client(
|
public function setUrl(string $url): self
|
||||||
$guzzle,
|
{
|
||||||
$factory,
|
$this->url = $url;
|
||||||
$factory,
|
|
||||||
$url,
|
|
||||||
$apikeyOrUsername,
|
|
||||||
$password
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setApiKey(string $apiKey): self
|
||||||
public function __call(mixed $name, mixed $arguments): mixed
|
|
||||||
{
|
{
|
||||||
if (!method_exists($this->client, $name)) {
|
$this->apiKey = $apiKey;
|
||||||
throw new \BadMethodCallException(sprintf(
|
}
|
||||||
'Method %s, doesn\'t exist in class %s',
|
|
||||||
$name,
|
|
||||||
get_class($this->client)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
$id = sprintf('magdev.redmine.%s', $name);
|
||||||
|
|
||||||
$container->register($id, RedmineClient::class)
|
$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");
|
$container->registerAliasForArgument($id, RedmineClient::class, "{$name}Client");
|
||||||
|
|
||||||
if ($name === $config['default_connection']) {
|
if ($name === $config['default_connection']) {
|
||||||
|
|||||||
Reference in New Issue
Block a user