forked from laravel/scout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngineManager.php
195 lines (166 loc) · 4.67 KB
/
EngineManager.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace Laravel\Scout;
use Algolia\AlgoliaSearch\Config\SearchConfig;
use Algolia\AlgoliaSearch\SearchClient as Algolia;
use Algolia\AlgoliaSearch\Support\UserAgent;
use Exception;
use Illuminate\Support\Manager;
use Laravel\Scout\Engines\AlgoliaEngine;
use Laravel\Scout\Engines\CollectionEngine;
use Laravel\Scout\Engines\DatabaseEngine;
use Laravel\Scout\Engines\MeiliSearchEngine;
use Laravel\Scout\Engines\NullEngine;
use MeiliSearch\Client as MeiliSearch;
class EngineManager extends Manager
{
/**
* Get a driver instance.
*
* @param string|null $name
* @return \Laravel\Scout\Engines\Engine
*/
public function engine($name = null)
{
return $this->driver($name);
}
/**
* Create an Algolia engine instance.
*
* @return \Laravel\Scout\Engines\AlgoliaEngine
*/
public function createAlgoliaDriver()
{
$this->ensureAlgoliaClientIsInstalled();
UserAgent::addCustomUserAgent('Laravel Scout', '9.4.12');
$config = SearchConfig::create(
config('scout.algolia.id'),
config('scout.algolia.secret')
)->setDefaultHeaders(
$this->defaultAlgoliaHeaders()
);
if (is_int($connectTimeout = config('scout.algolia.connect_timeout'))) {
$config->setConnectTimeout($connectTimeout);
}
if (is_int($readTimeout = config('scout.algolia.read_timeout'))) {
$config->setReadTimeout($readTimeout);
}
if (is_int($writeTimeout = config('scout.algolia.write_timeout'))) {
$config->setWriteTimeout($writeTimeout);
}
return new AlgoliaEngine(Algolia::createWithConfig($config), config('scout.soft_delete'));
}
/**
* Ensure the Algolia API client is installed.
*
* @return void
*
* @throws \Exception
*/
protected function ensureAlgoliaClientIsInstalled()
{
if (class_exists(Algolia::class)) {
return;
}
if (class_exists('AlgoliaSearch\Client')) {
throw new Exception('Please upgrade your Algolia client to version: ^2.2.');
}
throw new Exception('Please install the Algolia client: algolia/algoliasearch-client-php.');
}
/**
* Set the default Algolia configuration headers.
*
* @return array
*/
protected function defaultAlgoliaHeaders()
{
if (! config('scout.identify')) {
return [];
}
$headers = [];
if (! config('app.debug') &&
filter_var($ip = request()->ip(), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
) {
$headers['X-Forwarded-For'] = $ip;
}
if (($user = request()->user()) && method_exists($user, 'getKey')) {
$headers['X-Algolia-UserToken'] = $user->getKey();
}
return $headers;
}
/**
* Create an MeiliSearch engine instance.
*
* @return \Laravel\Scout\Engines\MeiliSearchEngine
*/
public function createMeilisearchDriver()
{
$this->ensureMeiliSearchClientIsInstalled();
return new MeiliSearchEngine(
$this->container->make(MeiliSearch::class),
config('scout.soft_delete', false)
);
}
/**
* Ensure the MeiliSearch client is installed.
*
* @return void
*
* @throws \Exception
*/
protected function ensureMeiliSearchClientIsInstalled()
{
if (class_exists(MeiliSearch::class)) {
return;
}
throw new Exception('Please install the MeiliSearch client: meilisearch/meilisearch-php.');
}
/**
* Create a database engine instance.
*
* @return \Laravel\Scout\Engines\DatabaseEngine
*/
public function createDatabaseDriver()
{
return new DatabaseEngine;
}
/**
* Create a collection engine instance.
*
* @return \Laravel\Scout\Engines\CollectionEngine
*/
public function createCollectionDriver()
{
return new CollectionEngine;
}
/**
* Create a null engine instance.
*
* @return \Laravel\Scout\Engines\NullEngine
*/
public function createNullDriver()
{
return new NullEngine;
}
/**
* Forget all of the resolved engine instances.
*
* @return $this
*/
public function forgetEngines()
{
$this->drivers = [];
return $this;
}
/**
* Get the default Scout driver name.
*
* @return string
*/
public function getDefaultDriver()
{
if (is_null($driver = config('scout.driver'))) {
return 'null';
}
return $driver;
}
}