Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(php): Add Retry Strategy & cache for the PHP client #95

Merged
merged 9 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ ENV DOCKER=true

RUN apk add openjdk11 maven jq bash perl curl

# PHP dependencies
RUN apk add composer php8 php8-tokenizer

WORKDIR /app

CMD ["bash"]
13 changes: 9 additions & 4 deletions clients/algoliasearch-client-php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle": "^7.3",
"guzzlehttp/psr7": "^2.0"
"guzzlehttp/psr7": "^2.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
Comment on lines +27 to +30
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pin the last version for all deps pls ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this has been recently set this way on the PHP repo to ensure the compatibility with latest PHP versions : https://github.com/algolia/algoliasearch-client-php/pull/696/files . So I'd keep it this way for now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep having issue with support for old version of software maybe now is a good time to get rid of them and force clients to update.

Copy link
Contributor Author

@damcou damcou Jan 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With PHP, multiple version (from 7.4 to 8.1) are coexisting, that's why we have such config in the composer.json . We can discuss it later with some PHP experts but for now, as it's not really related to the retry strategy itself, I would postpone this potential decision.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to APIC-271

},
"require-dev": {
"phpunit/phpunit": "^8.0 || ^9.0",
"friendsofphp/php-cs-fixer": "^2.12"
"friendsofphp/php-cs-fixer": "^3.5.0"
},
"autoload": {
"psr-4": { "Algolia\\AlgoliaSearch\\" : "lib/" }
"psr-4": { "Algolia\\AlgoliaSearch\\" : "lib/" },
"files": [
"lib/Http/Psr7/functions.php"
]
},
"autoload-dev": {
"psr-4": { "Algolia\\AlgoliaSearch\\Test\\" : "test/" }
Expand Down
118 changes: 118 additions & 0 deletions clients/algoliasearch-client-php/lib/Algolia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Algolia\AlgoliaSearch;

use Algolia\AlgoliaSearch\Cache\NullCacheDriver;
use Algolia\AlgoliaSearch\Http\HttpClientInterface;
use Algolia\AlgoliaSearch\Log\DebugLogger;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;

final class Algolia
{
const VERSION = '0.0.0';

/**
* Holds an instance of the simple cache repository (PSR-16).
*
* @var \Psr\SimpleCache\CacheInterface|null
*/
private static $cache;

/**
* Holds an instance of the logger (PSR-3).
*
* @var \Psr\Log\LoggerInterface|null
*/
private static $logger;

/**
* @var \Algolia\AlgoliaSearch\Http\HttpClientInterface
*/
private static $httpClient;

public static function isCacheEnabled()
{
if (null === self::$cache) {
return false;
}

return !self::getCache() instanceof NullCacheDriver;
}

/**
* Gets the cache instance.
*
* @return \Psr\SimpleCache\CacheInterface
*/
public static function getCache()
{
if (null === self::$cache) {
self::setCache(new NullCacheDriver());
}

return self::$cache;
}

/**
* Sets the cache instance.
*/
public static function setCache(CacheInterface $cache)
{
self::$cache = $cache;
}

/**
* Gets the logger instance.
*
* @return \Psr\Log\LoggerInterface
*/
public static function getLogger()
{
if (null === self::$logger) {
self::setLogger(new DebugLogger());
}

return self::$logger;
}

/**
* Sets the logger instance.
*/
public static function setLogger(LoggerInterface $logger)
{
self::$logger = $logger;
}

public static function getHttpClient()
{
$guzzleVersion = null;
if (interface_exists('\GuzzleHttp\ClientInterface')) {
if (defined('\GuzzleHttp\ClientInterface::VERSION')) {
$guzzleVersion = (int) mb_substr(\GuzzleHttp\Client::VERSION, 0, 1);
} else {
$guzzleVersion = \GuzzleHttp\ClientInterface::MAJOR_VERSION;
}
}

if (null === self::$httpClient) {
if (class_exists('\GuzzleHttp\Client') && 6 <= $guzzleVersion) {
self::setHttpClient(new \Algolia\AlgoliaSearch\Http\GuzzleHttpClient());
} else {
self::setHttpClient(new \Algolia\AlgoliaSearch\Http\CurlHttpClient());
}
}

return self::$httpClient;
}

public static function setHttpClient(HttpClientInterface $httpClient)
{
self::$httpClient = $httpClient;
}

public static function resetHttpClient()
{
self::$httpClient = null;
}
}
Loading