Skip to content

Commit 101d49c

Browse files
committed
Remove hard dependency on Predis + allow other Redis lib
1 parent 79b7281 commit 101d49c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+973
-271
lines changed

.unused.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
'enlightn/security-checker', // QA tool
2929
'php-parallel-lint/php-parallel-lint', // QA tool
3030
'sebastian/phpcpd', // QA tool
31+
'ukko/phpredis-phpdoc' // Stubs
3132
],
3233
'excludeDirectories' => [],
3334
'scanFiles' => [],

README.md

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,38 @@ composer require macfja/redisearch
1212

1313
## Usage
1414

15+
### Get a Redis client
16+
17+
This lib can use several connector for Redis:
18+
- [Predis](https://github.com/predis/predis/wiki) - Pure PHP implementation
19+
- [Phpredis](https://github.com/phpredis/phpredis) - PHP extension
20+
- [Phpiredis](https://github.com/nrk/phpiredis) - PHP extension depending on [hiredis](https://github.com/redis/hiredis)
21+
22+
You can pick the connector depending of your need.
23+
24+
```php
25+
$clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();
26+
27+
// With Predis
28+
$client = $clientFacade->getClient(new \Predis\Client(/* ... */));
29+
30+
// With Phpredis extension
31+
$client = $clientFacade->getClient(new Redis([/* ... */]));
32+
33+
// With Phpiredis extension
34+
$client = $clientFacade->getClient(phpiredis_connect($host));
35+
```
36+
37+
You can add your own implementation, all you need is to implement the interface `\MacFJA\RediSearch\Redis\Client` and add it to the client facace with:
38+
```php
39+
$clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();
40+
$clientFacade->addFactory(\MyVendor\MyPackage\MyRedisClient::class);
41+
```
42+
1543
### Create a new index
1644

1745
```php
18-
$client = new \Predis\Client(/* ... */);
46+
$client = /* ... */;
1947
$builder = new \MacFJA\RediSearch\IndexBuilder();
2048

2149
// Field can be create in advance
@@ -38,9 +66,9 @@ This will give you a new instance of the builder with the configured data.
3866
### Add a document
3967

4068
```php
41-
$client = new \Predis\Client(/* ... */);
69+
$client = /* ... */;
4270
$index = new \MacFJA\RediSearch\Index('person', $client);
43-
$index->addFromArray([
71+
$index->addDocumentFromArray([
4472
'firstname' => 'Joe',
4573
'lastname' => 'Doe',
4674
'age' => 30,
@@ -51,15 +79,15 @@ $index->addFromArray([
5179
### Search
5280

5381
```php
54-
$client = new \Predis\Client(/* ... */);
82+
$client = /* ... */;
5583
$search = new \MacFJA\RediSearch\Redis\Command\Search();
5684

5785
$search
5886
->setIndex('person')
5987
->setQuery('Doe')
6088
->setHighlight(['lastname'])
6189
->setWithScores();
62-
$results = $client->executeCommand($search);
90+
$results = $client->execute($search);
6391
```
6492

6593
#### Create a search query
@@ -96,9 +124,8 @@ use MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption;
96124
use MacFJA\RediSearch\Redis\Command\AggregateCommand\ReduceOption;
97125
use MacFJA\RediSearch\Redis\Command\Search;
98126
use MacFJA\RediSearch\Redis\Command\SugGet;
99-
use Predis\Client;
100127

101-
$client = new Client(/* ... */);
128+
$client = /* ... */;
102129

103130
$query = '@age:[(17 +inf] %john%';
104131
$search = new Search();
@@ -123,13 +150,7 @@ $suggestion->setDictionary('names')
123150
->setPrefix('john')
124151
->setFuzzy();
125152

126-
$result = $client->pipeline()
127-
->executeCommand($search)
128-
->executeCommand($stats)
129-
->executeCommand($aggregate)
130-
->executeCommand($suggestion)
131-
->execute()
132-
;
153+
$result = $client->pipeline($search, $stats, $aggregate, $suggestion);
133154

134155
// $result[0] is the search result
135156
// $result[1] is the first aggregation result

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"php": "^7.2",
2020
"ext-intl": "*",
2121
"composer/semver": "^3.2",
22-
"predis/predis": "^1.1",
2322
"respect/validation": "^2.0"
2423
},
2524
"require-dev": {
@@ -31,11 +30,18 @@
3130
"phpmd/phpmd": "^2.10",
3231
"phpstan/phpstan": "^0.12.92",
3332
"phpunit/phpunit": "^8.5",
33+
"predis/predis": "^1.1",
3434
"roave/security-advisories": "dev-latest",
3535
"rskuipers/php-assumptions": "^0.8.0",
3636
"sebastian/phpcpd": "^4.1",
37+
"ukko/phpredis-phpdoc": "dev-master",
3738
"vimeo/psalm": "^4.7"
3839
},
40+
"suggest": {
41+
"ext-phpiredis": "To use Phpiredis extension implementation",
42+
"ext-phpredis": "To use Phpredis extension implementation",
43+
"predis/predis": "To use Predis implementation"
44+
},
3945
"config": {
4046
"platform": {
4147
"php": "7.2"

composer.lock

Lines changed: 118 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ parameters:
33
paths:
44
- src
55
- tests
6+
excludePaths:
7+
analyse:
8+
- tests/psalm/stubs/phpiredis.php
69
ignoreErrors:
710
- '#Method MacFJA\\RediSearch\\Redis\\Command\\\w+::getLanguageOptions\(\) should return#'
811
- '#uses generic trait MacFJA\\RediSearch\\Redis\\Command\\Option\\DecoratedOptionTrait but does not specify its types: T#'

psalm.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
<directory name="vendor" />
1313
</ignoreFiles>
1414
</projectFiles>
15+
<stubs>
16+
<file name="vendor/ukko/phpredis-phpdoc/src/Redis.php"/>
17+
<file name="tests/psalm/stubs/phpiredis.php"/>
18+
</stubs>
1519
</psalm>

0 commit comments

Comments
 (0)