-
Notifications
You must be signed in to change notification settings - Fork 115
/
Algolia.php
118 lines (99 loc) · 2.73 KB
/
Algolia.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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 = '3.2.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) 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;
}
}