1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BinaryCube\CarrotMQ\Driver; |
6
|
|
|
|
7
|
|
|
use Throwable; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use Interop\Amqp\AmqpContext; |
10
|
|
|
use BinaryCube\CarrotMQ\Component; |
11
|
|
|
use Interop\Amqp\AmqpConnectionFactory; |
12
|
|
|
use BinaryCube\CarrotMQ\Support\LoggerAwareTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Driver |
16
|
|
|
*/ |
17
|
|
|
abstract class Driver extends Component |
18
|
|
|
{ |
19
|
|
|
use LoggerAwareTrait; |
20
|
|
|
|
21
|
|
|
const STATE_OPEN = 'open'; |
22
|
|
|
const STATE_CLOSE = 'close'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $state = self::STATE_CLOSE; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @const array Default driver parameters |
31
|
|
|
*/ |
32
|
|
|
const DEFAULTS = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $config = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var AmqpConnectionFactory |
41
|
|
|
*/ |
42
|
|
|
protected $interop; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var AmqpContext |
46
|
|
|
*/ |
47
|
|
|
protected $context; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor. |
51
|
|
|
* |
52
|
|
|
* @param array $config |
53
|
|
|
* @param LoggerInterface|null $logger |
54
|
|
|
*/ |
55
|
|
|
public function __construct(array $config = [], ?LoggerInterface $logger = null) |
56
|
|
|
{ |
57
|
|
|
parent::__construct(null, $logger); |
58
|
|
|
|
59
|
|
|
$this->config = $config; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return AmqpConnectionFactory |
64
|
|
|
*/ |
65
|
|
|
public function interop(): AmqpConnectionFactory |
66
|
|
|
{ |
67
|
|
|
return $this->interop; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param bool $new |
72
|
|
|
* |
73
|
|
|
* @return AmqpContext |
74
|
|
|
*/ |
75
|
|
|
public function context(bool $new = false): AmqpContext |
76
|
|
|
{ |
77
|
|
|
if ($new || empty($this->context)) { |
78
|
|
|
$this->context = $this->interop->createContext(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->context; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function state(): string |
88
|
|
|
{ |
89
|
|
|
return $this->state; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function open() |
96
|
|
|
{ |
97
|
|
|
if ($this->state === self::STATE_OPEN) { |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->interop = $this->build(); |
102
|
|
|
$this->state = self::STATE_OPEN; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function close() |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
|
|
$this->context->close(); |
114
|
|
|
} catch (Throwable $e) { |
115
|
|
|
/* Ignore on shutdown. */ |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->context = null; |
119
|
|
|
$this->interop = null; |
120
|
|
|
$this->state = self::STATE_CLOSE; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function reconnect() |
129
|
|
|
{ |
130
|
|
|
$this->close()->open(); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return AmqpConnectionFactory |
137
|
|
|
*/ |
138
|
|
|
abstract protected function build(): AmqpConnectionFactory; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
public function __destruct() |
144
|
|
|
{ |
145
|
|
|
$this->close(); |
146
|
|
|
|
147
|
|
|
unset( |
148
|
|
|
$this->state, |
149
|
|
|
$this->config, |
150
|
|
|
$this->context, |
151
|
|
|
$this->interop |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|