1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Pomm's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2017 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Foundation\Observer; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\Exception\NotificationException; |
13
|
|
|
use PommProject\Foundation\Client\Client; |
14
|
|
|
use PommProject\Foundation\Session\Session; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Observer |
18
|
|
|
* |
19
|
|
|
* Observer session client. |
20
|
|
|
* Listen to notifications sent to the server. |
21
|
|
|
* |
22
|
|
|
* @package Foundation |
23
|
|
|
* @copyright 2014 - 2017 Grégoire HUBERT |
24
|
|
|
* @author Grégoire HUBERT |
25
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
26
|
|
|
* @see Client |
27
|
|
|
*/ |
28
|
|
|
class Observer extends Client |
29
|
|
|
{ |
30
|
|
|
protected $channel; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* __construct |
34
|
|
|
* |
35
|
|
|
* Constructor |
36
|
|
|
* |
37
|
|
|
* @param string $channel |
38
|
|
|
*/ |
39
|
|
|
public function __construct($channel) |
40
|
|
|
{ |
41
|
|
|
$this->channel = $channel; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* getClientType |
46
|
|
|
* |
47
|
|
|
* @see Client |
48
|
|
|
*/ |
49
|
|
|
public function getClientType() |
50
|
|
|
{ |
51
|
|
|
return 'observer'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* getClientIdentifier |
56
|
|
|
* |
57
|
|
|
* @see Client |
58
|
|
|
*/ |
59
|
|
|
public function getClientIdentifier() |
60
|
|
|
{ |
61
|
|
|
return $this->channel; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* initialize |
66
|
|
|
* |
67
|
|
|
* @see Client |
68
|
|
|
*/ |
69
|
|
|
public function initialize(Session $session) |
70
|
|
|
{ |
71
|
|
|
parent::initialize($session); |
72
|
|
|
$this->restartListening(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* shutdown |
77
|
|
|
* |
78
|
|
|
* @see Client |
79
|
|
|
*/ |
80
|
|
|
public function shutdown() |
81
|
|
|
{ |
82
|
|
|
$this->unlisten($this->channel); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* getNotification |
87
|
|
|
* |
88
|
|
|
* Check if a notification is pending. If so, the payload is returned. |
89
|
|
|
* Otherwise, null is returned. |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function getNotification() |
94
|
|
|
{ |
95
|
|
|
return $this |
96
|
|
|
->getSession() |
97
|
|
|
->getConnection() |
98
|
|
|
->getNotification() |
99
|
|
|
; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* restartListening |
104
|
|
|
* |
105
|
|
|
* Send a LISTEN command to the backend. This is called in the initialize() |
106
|
|
|
* method but it can be unlisten if the listen command took place in a |
107
|
|
|
* transaction. |
108
|
|
|
* |
109
|
|
|
* @return Observer $this |
110
|
|
|
*/ |
111
|
|
|
public function restartListening() |
112
|
|
|
{ |
113
|
|
|
return $this->listen($this->channel); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* listen |
118
|
|
|
* |
119
|
|
|
* Start to listen on the given channel. The observer automatically starts |
120
|
|
|
* listening when registered against the session. |
121
|
|
|
* NOTE: When listen is issued in a transaction it is unlisten when the |
122
|
|
|
* transaction is committed or rollback. |
123
|
|
|
* |
124
|
|
|
* @param string $channel |
125
|
|
|
* @return Observer $this |
126
|
|
|
*/ |
127
|
|
|
protected function listen($channel) |
128
|
|
|
{ |
129
|
|
|
$this |
130
|
|
|
->executeAnonymousQuery( |
131
|
|
|
sprintf( |
132
|
|
|
"listen %s", |
133
|
|
|
$this->escapeIdentifier($channel) |
134
|
|
|
) |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* unlisten |
142
|
|
|
* |
143
|
|
|
* Stop listening to events. |
144
|
|
|
* |
145
|
|
|
* @param string $channel |
146
|
|
|
* @return Observer $this |
147
|
|
|
* |
148
|
|
|
*/ |
149
|
|
|
protected function unlisten($channel) |
150
|
|
|
{ |
151
|
|
|
$this->executeAnonymousQuery( |
152
|
|
|
sprintf( |
153
|
|
|
"unlisten %s", |
154
|
|
|
$this->escapeIdentifier($channel) |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* throwNotification |
163
|
|
|
* |
164
|
|
|
* Check if a notification is pending. If so, a NotificationException is thrown. |
165
|
|
|
* |
166
|
|
|
* @throws NotificationException |
167
|
|
|
* @return Observer $this |
168
|
|
|
*/ |
169
|
|
|
public function throwNotification() |
170
|
|
|
{ |
171
|
|
|
$notification = $this->getNotification(); |
172
|
|
|
|
173
|
|
|
if ($notification !== null) { |
174
|
|
|
throw new NotificationException($notification); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* executeAnonymousQuery |
182
|
|
|
* |
183
|
|
|
* Proxy for Connection::executeAnonymousQuery() |
184
|
|
|
* |
185
|
|
|
* @param string $sql |
186
|
|
|
* @return Observer $this |
187
|
|
|
* @see Connection |
188
|
|
|
*/ |
189
|
|
|
protected function executeAnonymousQuery($sql) |
190
|
|
|
{ |
191
|
|
|
$this |
192
|
|
|
->getSession() |
193
|
|
|
->getConnection() |
194
|
|
|
->executeAnonymousQuery($sql) |
195
|
|
|
; |
196
|
|
|
|
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* escapeIdentifier |
202
|
|
|
* |
203
|
|
|
* Proxy for Connection::escapeIdentifier() |
204
|
|
|
* |
205
|
|
|
* @param string $string |
206
|
|
|
* @return string |
207
|
|
|
* @see Connection |
208
|
|
|
*/ |
209
|
|
|
protected function escapeIdentifier($string) |
210
|
|
|
{ |
211
|
|
|
return $this |
212
|
|
|
->getSession() |
213
|
|
|
->getConnection() |
214
|
|
|
->escapeIdentifier($string) |
215
|
|
|
; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|