Skip to content

Commit 9bbd63f

Browse files
committed
Merge pull request #29 from cheprasov/dev-with-versions
v1.0-rc1
2 parents ee7a698 + d02c04c commit 9bbd63f

20 files changed

+224
-54
lines changed

README.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,48 @@ RedisClient is a fast, fully-functional and user-friendly client for Redis, opti
1010
- Support TCP/IP and UNIX sockets.
1111
- Support __PubSub__ and __monitor__ functionallity.
1212
- Support __pipeline__.
13+
- Support RAW commands as strings `"SET foo bar"` or as arrays `['SET', 'foo', 'bar']`.
1314
- Connections to Redis are established lazily by the client upon the first command.
1415
- Easy to use with IDE, client has PHPDocs for all supported versions.
16+
- By default, the client works with the latest stable version of Redis.
1517

1618
## Usage
1719

1820
### Create a new instance of RedisClient
1921
```php
2022
<?php
21-
require (dirname(__DIR__).'/src/autoloader.php');
22-
// or require ('vendor/autoload.php');
23+
require (dirname(__DIR__).'/vendor/autoload.php');
24+
// or require (dirname(__DIR__).'/src/autoloader.php');
2325

2426
use RedisClient\RedisClient;
2527
use RedisClient\Client\Version\RedisClient2x6;
2628

29+
// Create new Instance for Redis version 2.8.x with config via factory
30+
31+
$Redis = ClientFactory::create([
32+
'server' => 'tcp://127.0.0.1:6379', // or 'unix:///tmp/redis.sock'
33+
'timeout' => 2,
34+
'version' => '2.8.24'
35+
]);
36+
37+
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL;
38+
// RedisClient: 2.8
39+
40+
// Create new instance of client without factory
41+
2742
$Redis = new RedisClient([
2843
'server' => 'tcp://127.0.0.1:6379', // or 'unix:///tmp/redis.sock'
2944
'timeout' => 2
3045
]);
3146

32-
echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL;
47+
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL;
3348
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL;
3449

50+
// By default, the client works with the latest stable version of Redis.
3551
// RedisClient: 3.0
3652
// Redis: 3.0.3
53+
54+
3755
```
3856
### Example
3957
Please, see examples here: https://github.com/cheprasov/php-redis-client/tree/master/examples

examples/create_new_instance.php

+19-13
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,34 @@
1515

1616
namespace Examples;
1717

18-
require (dirname(__DIR__).'/src/autoloader.php');
19-
// or require ('vendor/autoload.php');
18+
require (dirname(__DIR__).'/vendor/autoload.php');
19+
// or require (dirname(__DIR__).'/src/autoloader.php');
2020

2121
use RedisClient\RedisClient;
2222
use RedisClient\Client\Version\RedisClient2x6;
23+
use RedisClient\ClientFactory;
2324

2425
// Example 1. Create new Instance with default config
2526

2627
$Redis = new RedisClient();
27-
28-
echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL;
28+
// By default, the client works with the latest stable version of Redis.
29+
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL;
2930
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL;
3031

31-
// result:
3232
// RedisClient: 3.0
3333
// Redis: 3.0.3
3434

3535

3636
// Example 2. Create new Instance with config
37+
// By default, the client works with the latest stable version of Redis.
3738

3839
$Redis = new RedisClient([
3940
'server' => 'tcp://127.0.0.1:6379', // or 'unix:///tmp/redis.sock'
4041
'timeout' => 2
4142
]);
4243

43-
echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL;
44+
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL;
4445
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL;
45-
46-
// result:
4746
// RedisClient: 3.0
4847
// Redis: 3.0.3
4948

@@ -55,9 +54,16 @@
5554
'timeout' => 2
5655
]);
5756

58-
echo 'RedisClient: '. $Redis->getVersion() . PHP_EOL;
59-
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL;
60-
61-
// result:
57+
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL;
6258
// RedisClient: 2.6
63-
// Redis: 3.0.3
59+
60+
// Example 4. Create new Instance for Redis version 2.8.x with config via factory
61+
62+
$Redis = ClientFactory::create([
63+
'server' => 'tcp://127.0.0.1:6379', // or 'unix:///tmp/redis.sock'
64+
'timeout' => 2,
65+
'version' => '2.8.24'
66+
]);
67+
68+
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL;
69+
// RedisClient: 2.8

examples/monitor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
*/
1111

1212
/**
13-
* Using Monitor
13+
* Monitor
1414
*/
1515

1616
namespace Examples;
1717

18-
require (dirname(__DIR__).'/src/autoloader.php');
19-
// or require ('vendor/autoload.php');
18+
require (dirname(__DIR__).'/vendor/autoload.php');
19+
// or require (dirname(__DIR__).'/src/autoloader.php');
2020

2121
use RedisClient\RedisClient;
2222

examples/pipeline.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
*/
1111

1212
/**
13-
* Using pipeline
13+
* Pipeline
1414
*/
1515

1616
namespace Examples;
1717

18-
require (dirname(__DIR__).'/src/autoloader.php');
19-
// or require ('vendor/autoload.php');
18+
require (dirname(__DIR__).'/vendor/autoload.php');
19+
// or require (dirname(__DIR__).'/src/autoloader.php');
2020

2121
use RedisClient\Pipeline\Pipeline;
2222
use RedisClient\Pipeline\PipelineInterface;

examples/pubsub.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
*/
1111

1212
/**
13-
* Using PubSub
13+
* PubSub
1414
*/
1515

1616
namespace Examples;
1717

18-
require (dirname(__DIR__).'/src/autoloader.php');
19-
// or require ('vendor/autoload.php');
18+
require (dirname(__DIR__).'/vendor/autoload.php');
19+
// or require (dirname(__DIR__).'/src/autoloader.php');
2020

2121
use RedisClient\RedisClient;
2222

examples/raw_commands.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* This file is part of RedisClient.
4+
* git: https://github.com/cheprasov/php-redis-client
5+
*
6+
* (C) Alexander Cheprasov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/**
13+
* RAW commands
14+
*/
15+
16+
namespace Examples;
17+
18+
require (dirname(__DIR__).'/vendor/autoload.php');
19+
// or require (dirname(__DIR__).'/src/autoloader.php');
20+
21+
use RedisClient\RedisClient;
22+
23+
$Redis = new RedisClient();
24+
25+
// Example 1. As string[] by <executeRaw>
26+
// Every part of command must be a separate string
27+
// <executeRaw> is better way to use raw commands than <executeRawString>
28+
29+
$Redis->executeRaw(['SET', 'foo', 'bar']);
30+
echo 'result: '. $Redis->executeRaw(['GET', 'foo']) .PHP_EOL;
31+
// bar
32+
33+
// Example s. As string by <executeRawString>
34+
$Redis->executeRawString('SET foo bar');
35+
echo 'result: '. $Redis->executeRawString('GET foo') .PHP_EOL;
36+
// bar
37+
38+
// You can use quotes for keys and arguments
39+
$Redis->executeRawString('SET "key with spaces" "or value with spaces"');
40+
echo 'result: '. $Redis->executeRawString('GET "key with spaces"') .PHP_EOL;
41+
// or value with spaces

examples/transactions.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
*/
1111

1212
/**
13-
* Using transactions
13+
* Transactions
1414
*/
1515

1616
namespace Examples;
1717

18-
require (dirname(__DIR__).'/src/autoloader.php');
19-
// or require ('vendor/autoload.php');
18+
require (dirname(__DIR__).'/vendor/autoload.php');
19+
// or require (dirname(__DIR__).'/src/autoloader.php');
2020

2121
use RedisClient\Pipeline\Pipeline;
2222
use RedisClient\Pipeline\PipelineInterface;

src/RedisClient/Client/Version/RedisClient2x6.php

-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use RedisClient\Pipeline\PipelineInterface;
1616
use RedisClient\Pipeline\Version\Pipeline2x6;
1717

18-
/**
19-
* Class RedisClient
20-
* @package RedisClient
21-
*/
2218
class RedisClient2x6 extends AbstractRedisClient {
2319
use CommandsTrait;
2420

src/RedisClient/Client/Version/RedisClient2x8.php

-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use RedisClient\Pipeline\PipelineInterface;
1616
use RedisClient\Pipeline\Version\Pipeline2x8;
1717

18-
/**
19-
* Class RedisClient
20-
* @package RedisClient
21-
*/
2218
class RedisClient2x8 extends AbstractRedisClient {
2319
use CommandsTrait;
2420

src/RedisClient/Client/Version/RedisClient3x0.php

-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use RedisClient\Pipeline\PipelineInterface;
1616
use RedisClient\Pipeline\Version\Pipeline3x0;
1717

18-
/**
19-
* Class RedisClient
20-
* @package RedisClient
21-
*/
2218
class RedisClient3x0 extends AbstractRedisClient {
2319
use CommandsTrait;
2420

src/RedisClient/Client/Version/RedisClient3x2.php

-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use RedisClient\Pipeline\PipelineInterface;
1616
use RedisClient\Pipeline\Version\Pipeline3x2;
1717

18-
/**
19-
* Class RedisClient
20-
* @package RedisClient
21-
*/
2218
class RedisClient3x2 extends AbstractRedisClient {
2319
use CommandsTrait;
2420

src/RedisClient/ClientFactory.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* This file is part of RedisClient.
4+
* git: https://github.com/cheprasov/php-redis-client
5+
*
6+
* (C) Alexander Cheprasov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace RedisClient;
12+
13+
use RedisClient\Client\AbstractRedisClient;
14+
use RedisClient\Client\Version\RedisClient2x6;
15+
use RedisClient\Client\Version\RedisClient2x8;
16+
use RedisClient\Client\Version\RedisClient3x0;
17+
use RedisClient\Client\Version\RedisClient3x2;
18+
19+
class ClientFactory {
20+
21+
protected static $versions = [
22+
'2.6' => RedisClient2x6::class,
23+
'2.8' => RedisClient2x8::class,
24+
'3.0' => RedisClient3x0::class,
25+
'3.2' => RedisClient3x2::class,
26+
];
27+
28+
/**
29+
* @param null|array $config
30+
* @return RedisClient2x6|RedisClient2x8|RedisClient3x0|RedisClient3x2|RedisClient
31+
*/
32+
public static function create($config = null) {
33+
if (isset($config['version'])) {
34+
return self::createClientByVersion($config['version'], $config);
35+
}
36+
return new RedisClient($config);
37+
}
38+
39+
/**
40+
* @param string $version
41+
* @param null|array $config
42+
* @return RedisClient2x6|RedisClient2x8|RedisClient3x0|RedisClient3x2
43+
*/
44+
public static function createClientByVersion($version, $config = null) {
45+
list($major, $minor, $patch) = explode('.', $version.'.0.0');
46+
$ver = (int) $major .'.'. (int) $minor;
47+
48+
$versions = array_keys(self::$versions);
49+
foreach ($versions as $v) {
50+
if ($v >= $ver) {
51+
$class = self::$versions[$v];
52+
break;
53+
}
54+
}
55+
if (empty($class)) {
56+
throw new \InvalidArgumentException(
57+
'RedisClient does not support Redis version '.$version.'. Please, use version ' .end($versions)
58+
);
59+
}
60+
return new $class($config);
61+
}
62+
63+
}

src/RedisClient/Command/Traits/AbstractCommandsTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract protected function subscribeCommand(array $subCommand, array $unsubComm
3535
/**
3636
* @return string
3737
*/
38-
abstract public function getVersion();
38+
abstract public function getSupportedVersion();
3939

4040
/**
4141
* @var array

src/RedisClient/Command/Traits/Version2x6/CommandsTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ trait CommandsTrait {
3131
/**
3232
* @return string
3333
*/
34-
public function getVersion() {
34+
public function getSupportedVersion() {
3535
return '2.6';
3636
}
3737

src/RedisClient/Command/Traits/Version2x8/CommandsTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ trait CommandsTrait {
3737
/**
3838
* @return string
3939
*/
40-
public function getVersion() {
40+
public function getSupportedVersion() {
4141
return '2.8';
4242
}
4343

src/RedisClient/Command/Traits/Version3x0/CommandsTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ trait CommandsTrait {
4545
/**
4646
* @return string
4747
*/
48-
public function getVersion() {
48+
public function getSupportedVersion() {
4949
return '3.0';
5050
}
5151

src/RedisClient/Command/Traits/Version3x2/CommandsTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ trait CommandsTrait {
4747
/**
4848
* @return string
4949
*/
50-
public function getVersion() {
50+
public function getSupportedVersion() {
5151
return '3.2';
5252
}
5353

src/RedisClient/RedisClient.php

-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
use RedisClient\Pipeline\Pipeline;
1414
use RedisClient\Pipeline\PipelineInterface;
1515

16-
17-
/**
18-
* Class RedisClient
19-
* @package RedisClient
20-
*/
2116
class RedisClient extends RedisClientLastStableVersion {
2217

2318
/**

tests/Integration/Version2x6/ConnectionCommandsTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Test\Integration\Version2x6;
1212

1313
use RedisClient\Client\Version\RedisClient2x6;
14+
use RedisClient\ClientFactory;
1415
use RedisClient\Exception\ErrorResponseException;
1516

1617
/**

0 commit comments

Comments
 (0)