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