1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Xervice\Redis; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Predis\Client; |
8
|
|
|
use Xervice\Core\Factory\AbstractFactory; |
9
|
|
|
use Xervice\DataProvider\DataProvider\AbstractDataProvider; |
10
|
|
|
use Xervice\Redis\Commands\Collection; |
11
|
|
|
use Xervice\Redis\Commands\Provider; |
12
|
|
|
use Xervice\Redis\Commands\ProviderInterface; |
13
|
|
|
use Xervice\Redis\Converter\DataConverter; |
14
|
|
|
use Xervice\Redis\Converter\DataConverterInterface; |
15
|
|
|
use Xervice\Redis\Converter\ListConverter; |
16
|
|
|
use Xervice\Redis\Converter\ListConverterInterface; |
17
|
|
|
use Xervice\Redis\Transaction\Transaction; |
18
|
|
|
use Xervice\Redis\Transaction\TransactionCollection; |
19
|
|
|
use Xervice\Redis\Transaction\TransactionHandler; |
20
|
|
|
use Xervice\Redis\Transaction\TransactionHandlerInterface; |
21
|
|
|
use Xervice\Redis\Transaction\TransactionInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @method \Xervice\Redis\RedisConfig getConfig() |
25
|
|
|
*/ |
26
|
|
|
class RedisFactory extends AbstractFactory |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var \Predis\Client |
30
|
|
|
*/ |
31
|
|
|
private $client; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TransactionHandlerInterface |
35
|
|
|
*/ |
36
|
|
|
private $transationHandler; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $key |
40
|
|
|
* @param \Xervice\DataProvider\DataProvider\AbstractDataProvider $dataProvider |
41
|
|
|
* |
42
|
|
|
* @return \Xervice\Redis\Transaction\Transaction |
43
|
|
|
*/ |
44
|
1 |
|
public function createTransaction(string $key, AbstractDataProvider $dataProvider) : TransactionInterface |
45
|
|
|
{ |
46
|
1 |
|
return new Transaction( |
47
|
1 |
|
$key, |
48
|
1 |
|
$dataProvider |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return \Xervice\Redis\Transaction\TransactionHandler |
54
|
|
|
*/ |
55
|
1 |
|
public function createTransactionHandler() : TransactionHandlerInterface |
56
|
|
|
{ |
57
|
1 |
|
return new TransactionHandler( |
58
|
1 |
|
$this->createTransactionCollection() |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return \Xervice\Redis\Transaction\TransactionCollection |
64
|
|
|
*/ |
65
|
1 |
|
public function createTransactionCollection() : TransactionCollection |
66
|
|
|
{ |
67
|
1 |
|
return new TransactionCollection(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return \Xervice\Redis\Converter\ListConverter |
72
|
|
|
*/ |
73
|
3 |
|
public function createListConverter() : ListConverterInterface |
74
|
|
|
{ |
75
|
3 |
|
return new ListConverter( |
76
|
3 |
|
$this->createConverter() |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return \Xervice\Redis\Converter\DataConverter |
82
|
|
|
*/ |
83
|
6 |
|
public function createConverter(): DataConverterInterface |
84
|
|
|
{ |
85
|
6 |
|
return new DataConverter(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return \Xervice\Redis\Commands\Provider |
90
|
|
|
* @throws \Xervice\Config\Exception\ConfigNotFound |
91
|
|
|
*/ |
92
|
8 |
|
public function createCommandProvider(): ProviderInterface |
93
|
|
|
{ |
94
|
8 |
|
return new Provider( |
95
|
8 |
|
$this->getCommandCollection(), |
96
|
8 |
|
$this->getRedisClient() |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return \Predis\Client |
102
|
|
|
* @throws \Xervice\Config\Exception\ConfigNotFound |
103
|
|
|
*/ |
104
|
1 |
|
public function createRedisClient(): Client |
105
|
|
|
{ |
106
|
1 |
|
return new Client( |
107
|
1 |
|
$this->getConfig()->getRedisConfig(), |
108
|
1 |
|
$this->getConfig()->getRedisOptions() |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return \Predis\Client |
114
|
|
|
* @throws \Xervice\Config\Exception\ConfigNotFound |
115
|
|
|
*/ |
116
|
8 |
|
public function getRedisClient(): Client |
117
|
|
|
{ |
118
|
8 |
|
if ($this->client === null) { |
119
|
1 |
|
$this->client = $this->createRedisClient(); |
120
|
|
|
} |
121
|
8 |
|
return $this->client; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return \Xervice\Redis\Commands\Collection |
126
|
|
|
*/ |
127
|
8 |
|
public function getCommandCollection(): Collection |
128
|
|
|
{ |
129
|
8 |
|
return $this->getDependency(RedisDependencyProvider::REDIS_COMMAND_COLLECTION); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return \Xervice\Redis\Transaction\TransactionHandler |
134
|
|
|
*/ |
135
|
1 |
|
public function getTransactionHandler() : TransactionHandlerInterface |
136
|
|
|
{ |
137
|
1 |
|
if ($this->transationHandler === null) { |
138
|
1 |
|
$this->transationHandler = $this->createTransactionHandler(); |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
return $this->transationHandler; |
142
|
|
|
} |
143
|
|
|
} |