|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniMan\Drivers\Redis; |
|
4
|
|
|
|
|
5
|
|
|
use Nette\Localization\ITranslator; |
|
6
|
|
|
use RedisException; |
|
7
|
|
|
use RedisProxy\RedisProxy; |
|
8
|
|
|
use UniMan\Core\Driver\AbstractDriver; |
|
9
|
|
|
use UniMan\Core\Exception\ConnectException; |
|
10
|
|
|
use UniMan\Drivers\Redis\Forms\RedisCredentialsForm; |
|
11
|
|
|
use UniMan\Drivers\Redis\RedisDatabaseAliasStorage; |
|
12
|
|
|
|
|
13
|
|
|
class RedisDriver extends AbstractDriver |
|
14
|
|
|
{ |
|
15
|
|
|
const TYPE_KEY = 'key'; |
|
16
|
|
|
const TYPE_HASH = 'hash'; |
|
17
|
|
|
const TYPE_SET = 'set'; |
|
18
|
|
|
|
|
19
|
|
|
private $connection; |
|
20
|
|
|
|
|
21
|
|
|
private $databaseAliasStorage; |
|
22
|
|
|
|
|
23
|
2 |
|
public function __construct(ITranslator $translator, RedisDatabaseAliasStorage $databaseAliasStorage) |
|
24
|
|
|
{ |
|
25
|
2 |
|
parent::__construct($translator); |
|
26
|
2 |
|
$this->databaseAliasStorage = $databaseAliasStorage; |
|
27
|
2 |
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
public function check() |
|
30
|
|
|
{ |
|
31
|
2 |
|
return extension_loaded('redis') || class_exists('Predis\Client'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
public function extensions() |
|
35
|
|
|
{ |
|
36
|
2 |
|
return ['redis']; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
public function classes() |
|
40
|
|
|
{ |
|
41
|
2 |
|
return ['Predis\Client']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
public function type() |
|
45
|
|
|
{ |
|
46
|
2 |
|
return 'redis'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public function defaultCredentials() |
|
50
|
|
|
{ |
|
51
|
|
|
return [ |
|
52
|
2 |
|
'host' => 'localhost', |
|
53
|
1 |
|
'port' => '6379', |
|
54
|
1 |
|
'database' => '0', |
|
55
|
1 |
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function getCredentialsForm() |
|
59
|
|
|
{ |
|
60
|
2 |
|
return new RedisCredentialsForm(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function connect(array $credentials) |
|
64
|
|
|
{ |
|
65
|
|
|
try { |
|
66
|
2 |
|
$this->connection = new RedisProxy($credentials['host'], $credentials['port'], $credentials['database']); |
|
67
|
1 |
|
} catch (RedisException $e) { |
|
68
|
|
|
throw new ConnectException($e->getMessage()); |
|
69
|
|
|
} |
|
70
|
2 |
|
} |
|
71
|
|
|
|
|
72
|
2 |
|
protected function getFormManager() |
|
73
|
|
|
{ |
|
74
|
2 |
|
return new RedisFormManager($this->connection, $this->databaseAliasStorage); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
protected function getHeaderManager() |
|
78
|
|
|
{ |
|
79
|
2 |
|
return new RedisHeaderManager(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
protected function getPermissions() |
|
83
|
|
|
{ |
|
84
|
2 |
|
return new RedisPermissions(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
protected function getDataManager() |
|
88
|
|
|
{ |
|
89
|
2 |
|
return new RedisDataManager($this->connection, $this->databaseAliasStorage); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|