Skip to content

Commit a468006

Browse files
authored
Merge pull request #43 from MacFJA/fix-predis-2
Add Predis v2 compatibility
2 parents 0717693 + 5d488b2 commit a468006

File tree

7 files changed

+57
-10
lines changed

7 files changed

+57
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
- Documentation on paginated responses
1212
- Throw custom exception is client is missing
13+
- Add compatibility for Predis version 2.x
14+
- (dev) Add `make clean` to remove all generated files
1315

1416
### Fixed
1517

@@ -18,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1820
### Changed
1921

2022
- Rework paginated responses
23+
- Add warning for Predis commands initialization
24+
- (dev) Remove deprecated `--no-suggest` option of Composer in the `Makefile`
2125

2226
### Deprecated
2327

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: analyze fix-code test test-with-integration coverage coverage-with-integration validation integration-test integration-coverage
1+
.PHONY: analyze fix-code clean test test-with-integration coverage coverage-with-integration validation integration-test integration-coverage
22

33
analyze: | vendor
44
$(COMPOSER) exec -v parallel-lint -- src
@@ -11,6 +11,13 @@ analyze: | vendor
1111
$(COMPOSER) exec -v phpstan -- analyse
1212
$(COMPOSER) exec -v psalm -- src
1313

14+
clean:
15+
rm -rf composer.phar
16+
rm -rf vendor/
17+
rm -rf composer.lock
18+
rm -rf .php-cs-fixer.cache
19+
rm -rf .phpunit.result.cache
20+
1421
fix-code: | vendor
1522
$(COMPOSER) normalize
1623
$(COMPOSER) exec -v php-cs-fixer -- fix -v
@@ -39,7 +46,7 @@ integration-coverage: | vendor
3946
validation: fix-code analyze test-with-integration coverage-with-integration
4047

4148
vendor: composer.json
42-
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist
49+
$(COMPOSER) install --optimize-autoloader --prefer-dist
4350
touch vendor
4451

4552
composer.phar:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ $result = $client->pipeline($search, $stats, $aggregate, $suggestion);
182182
// $result[3] is the suggestion result
183183
```
184184

185-
### Use Predis shorthand syntax
185+
### Use Predis (v1.x) shorthand syntax
186186

187187
```php
188188
$client = new \Predis\Client(/* ... */);
@@ -225,4 +225,4 @@ See [CONTRIBUTING](CONTRIBUTING.md) for more information.
225225

226226
## License
227227

228-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
228+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"phpmd/phpmd": "^2.10",
4040
"phpstan/phpstan": "^1.2.0",
4141
"phpunit/phpunit": "^8.5 || ^9.3",
42-
"predis/predis": "^1.1",
42+
"predis/predis": "^1.1 || ^2.0",
4343
"ptrofimov/tinyredisclient": "^1.1",
4444
"redisent/redisent": "dev-master",
4545
"roave/security-advisories": "dev-latest",

phpstan.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ parameters:
1313
- '#Method MacFJA\\RediSearch\\tests\\Redis.+::dataProvider\(\) return type has no value type specified in iterable type (array|Generator).#'
1414
-
1515
message: '#Call to an undefined method MacFJA\\RediSearch\\Redis\\Command\\Option\\CommandOption::set\w+\(\).#'
16-
path: src/Redis/Command/Option/GroupedOption.php
16+
path: src/Redis/Command/Option/GroupedOption.php
17+
- '#Predis\\Profile\\RedisProfile#'

src/Redis/Client/PredisClient.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@
2626
use MacFJA\RediSearch\Redis\Command;
2727
use Predis\ClientContextInterface;
2828
use Predis\ClientInterface;
29+
use Predis\Command\CommandInterface;
2930
use Predis\Command\RawCommand;
31+
use Predis\Command\RawFactory;
3032
use RuntimeException;
3133

3234
class PredisClient extends AbstractClient
3335
{
3436
/** @var ClientInterface */
3537
private $redis;
3638

39+
/** @var null|RawFactory */
40+
private static $rawFactory;
41+
3742
/**
3843
* @codeCoverageIgnore
3944
*/
@@ -51,7 +56,7 @@ private function __construct(ClientInterface $redis)
5156

5257
public function execute(Command $command)
5358
{
54-
$rawResponse = $this->redis->executeCommand(new RawCommand(array_merge([$command->getId()], $command->getArguments())));
59+
$rawResponse = $this->redis->executeCommand(self::createRawCommand(array_merge([$command->getId()], $command->getArguments())));
5560

5661
return $command->parseResponse($rawResponse);
5762
}
@@ -82,7 +87,7 @@ public static function make($redis): Client
8287
*/
8388
public function executeRaw(...$args)
8489
{
85-
return $this->redis->executeCommand(new RawCommand($args));
90+
return $this->redis->executeCommand(self::createRawCommand($args));
8691
}
8792

8893
protected function doPipeline(Command ...$commands): array
@@ -91,8 +96,27 @@ protected function doPipeline(Command ...$commands): array
9196

9297
return $this->redis->pipeline(static function (ClientContextInterface $pipeline) use ($commands): void {
9398
foreach ($commands as $command) {
94-
$pipeline->executeCommand(new RawCommand(array_merge([$command->getId()], $command->getArguments())));
99+
$pipeline->executeCommand(self::createRawCommand(array_merge([$command->getId()], $command->getArguments())));
95100
}
96101
});
97102
}
103+
104+
/**
105+
* @param array<float|int|string> $args
106+
* @codeCoverageIgnore
107+
*/
108+
private static function createRawCommand(array $args): CommandInterface
109+
{
110+
if (class_exists(RawFactory::class)) {
111+
if (null === self::$rawFactory) {
112+
self::$rawFactory = new RawFactory();
113+
}
114+
$commandID = array_shift($args);
115+
116+
return self::$rawFactory->create((string) $commandID, $args);
117+
}
118+
119+
// @phpstan-ignore-next-line
120+
return new RawCommand($args);
121+
}
98122
}

src/Redis/Initializer.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace MacFJA\RediSearch\Redis;
2323

24+
use MacFJA\RediSearch\Redis\Client\AbstractClient;
2425
use MacFJA\RediSearch\Redis\Client\Rediska\RediskaRediSearchCommand;
2526
use MacFJA\RediSearch\Redis\Command\Aggregate;
2627
use MacFJA\RediSearch\Redis\Command\AliasAdd;
@@ -59,9 +60,19 @@ class Initializer
5960
{
6061
/**
6162
* @codeCoverageIgnore
63+
* @psalm-suppress UndefinedDocblockClass
64+
* @psalm-suppress UndefinedClass
65+
*
66+
* @param RedisProfile $profile
6267
*/
63-
public static function registerCommandsPredis(RedisProfile $profile): void
68+
public static function registerCommandsPredis($profile): void
6469
{
70+
if (!$profile instanceof RedisProfile) {
71+
false === AbstractClient::$disableNotice
72+
&& trigger_error(sprintf('The parameter $profile must be an instance of %s, which is only available in Predis 1.x.', RedisProfile::class), E_USER_WARNING);
73+
74+
return;
75+
}
6576
$profile->defineCommand('ftaggregate', Aggregate::class);
6677
$profile->defineCommand('ftaliasadd', AliasAdd::class);
6778
$profile->defineCommand('ftaliasdel', AliasDel::class);

0 commit comments

Comments
 (0)