1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Messages; |
4
|
|
|
|
5
|
|
|
use Psr\Log\InvalidArgumentException; |
6
|
|
|
use Puzzle\AMQP\WritableMessage; |
7
|
|
|
use Puzzle\AMQP\ReadableMessage; |
8
|
|
|
use Puzzle\AMQP\Messages\Bodies\NullBody; |
9
|
|
|
|
10
|
|
|
class Message implements WritableMessage |
11
|
|
|
{ |
12
|
|
|
use BodySetter; |
13
|
|
|
|
14
|
|
|
protected |
15
|
|
|
$body; |
16
|
|
|
|
17
|
|
|
private |
18
|
|
|
$flags, |
|
|
|
|
19
|
|
|
$headers, |
20
|
|
|
$attributes; |
21
|
|
|
|
22
|
21 |
|
public function __construct($routingKey = '') |
23
|
|
|
{ |
24
|
21 |
|
$this->body = new NullBody(); |
25
|
21 |
|
$this->flags = AMQP_NOPARAM; |
26
|
21 |
|
$this->headers = array(); |
27
|
21 |
|
$this->initializeAttributes(); |
28
|
21 |
|
$this->setAttribute('routing_key', $routingKey); |
29
|
21 |
|
} |
30
|
|
|
|
31
|
21 |
|
private function initializeAttributes() |
32
|
|
|
{ |
33
|
21 |
|
$this->attributes = array( |
34
|
21 |
|
'routing_key' => null, |
35
|
21 |
|
'content_type' => $this->getContentType(), |
36
|
21 |
|
'content_encoding' => 'utf8', |
37
|
|
|
'message_id' => function($timestamp) { |
38
|
8 |
|
return sha1($this->getRoutingKey() . $timestamp . $this->generateBodyId() . mt_rand()); |
39
|
21 |
|
}, |
40
|
21 |
|
'user_id' => null, |
41
|
21 |
|
'app_id' => null, |
42
|
21 |
|
'delivery_mode' => self::PERSISTENT, |
43
|
21 |
|
'priority' => null, |
44
|
|
|
'timestamp' => function($timestamp) { |
45
|
9 |
|
return $timestamp; |
46
|
21 |
|
}, |
47
|
21 |
|
'expiration' => null, |
48
|
21 |
|
'type' => null, |
49
|
21 |
|
'reply_to' => null, |
50
|
21 |
|
'correlation_id' => null, |
51
|
|
|
'headers' => function($timestamp) { |
52
|
10 |
|
return $this->packHeaders($timestamp); |
53
|
21 |
|
}, |
54
|
|
|
); |
55
|
21 |
|
} |
56
|
|
|
|
57
|
8 |
|
private function generateBodyId() |
58
|
|
|
{ |
59
|
8 |
|
if($this->body instanceof Footprintable) |
60
|
8 |
|
{ |
61
|
1 |
|
return $this->body->footprint(); |
62
|
|
|
} |
63
|
|
|
|
64
|
7 |
|
return uniqid(true); |
65
|
|
|
} |
66
|
|
|
|
67
|
21 |
|
public function getContentType() |
68
|
|
|
{ |
69
|
21 |
|
return $this->body->getContentType(); |
70
|
|
|
} |
71
|
|
|
|
72
|
19 |
|
public function getRoutingKey() |
73
|
|
|
{ |
74
|
19 |
|
return $this->getAttribute('routing_key'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getFormattedBody() |
78
|
|
|
{ |
79
|
|
|
return $this->body->format(); |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
public function setBody(Body $body) |
83
|
|
|
{ |
84
|
4 |
|
$this->body = $body; |
85
|
4 |
|
$this->updateContentType(); |
86
|
|
|
|
87
|
4 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
11 |
|
private function updateContentType() |
91
|
|
|
{ |
92
|
11 |
|
$this->attributes['content_type'] = $this->body->getContentType(); |
93
|
11 |
|
} |
94
|
|
|
|
95
|
|
|
public function getFlags() |
96
|
|
|
{ |
97
|
|
|
return $this->flags; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setFlags($flags) |
101
|
|
|
{ |
102
|
|
|
$this->flags = $flags; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
13 |
|
public function addHeader($headerName, $value) |
108
|
|
|
{ |
109
|
13 |
|
$this->headers[$headerName] = $value; |
110
|
|
|
|
111
|
13 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
public function addHeaders(array $headers) |
115
|
|
|
{ |
116
|
3 |
|
foreach($headers as $name => $value) |
117
|
|
|
{ |
118
|
3 |
|
$this->addHeader($name, $value); |
119
|
3 |
|
} |
120
|
|
|
|
121
|
3 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
public function setAuthor($author) |
125
|
|
|
{ |
126
|
1 |
|
$this->addHeader('author', $author); |
127
|
|
|
|
128
|
1 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
10 |
|
public function packAttributes($timestamp = false) |
132
|
|
|
{ |
133
|
10 |
|
$this->updateContentType(); |
134
|
|
|
|
135
|
10 |
|
if($timestamp === false) |
136
|
10 |
|
{ |
137
|
8 |
|
$timestamp = (new \DateTime("now"))->getTimestamp(); |
138
|
8 |
|
} |
139
|
|
|
|
140
|
10 |
|
return array_map(function($value) use($timestamp) { |
141
|
|
|
|
142
|
10 |
|
if($value instanceof \Closure) |
143
|
10 |
|
{ |
144
|
10 |
|
$value = $value($timestamp); |
145
|
10 |
|
} |
146
|
|
|
|
147
|
10 |
|
return $value; |
148
|
|
|
|
149
|
10 |
|
}, $this->attributes); |
150
|
|
|
} |
151
|
|
|
|
152
|
10 |
|
private function packHeaders($timestamp) |
153
|
|
|
{ |
154
|
10 |
|
$this->headers['message_datetime'] = date('Y-m-d H:i:s', $timestamp); |
155
|
|
|
|
156
|
10 |
|
return $this->headers; |
157
|
|
|
} |
158
|
|
|
|
159
|
21 |
|
public function setAttribute($attributeName, $value) |
160
|
|
|
{ |
161
|
21 |
|
if($attributeName !== 'headers') |
162
|
21 |
|
{ |
163
|
21 |
|
if(array_key_exists($attributeName, $this->attributes)) |
164
|
21 |
|
{ |
165
|
21 |
|
$this->attributes[$attributeName] = $value; |
166
|
21 |
|
} |
167
|
21 |
|
} |
168
|
|
|
|
169
|
21 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getAppId() |
173
|
|
|
{ |
174
|
|
|
return $this->getAttribute('app_id'); |
175
|
|
|
} |
176
|
|
|
|
177
|
3 |
|
public function getHeaders() |
178
|
|
|
{ |
179
|
3 |
|
$attributes = $this->packAttributes(); |
180
|
|
|
|
181
|
3 |
|
return $attributes['headers']; |
182
|
|
|
} |
183
|
|
|
|
184
|
21 |
|
public function getAttribute($attributeName) |
185
|
|
|
{ |
186
|
21 |
|
if(array_key_exists($attributeName, $this->attributes)) |
187
|
21 |
|
{ |
188
|
20 |
|
return $this->attributes[$attributeName]; |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
throw new InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName)); |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
public function __toString() |
195
|
|
|
{ |
196
|
1 |
|
return json_encode(array( |
197
|
1 |
|
'routing_key' => $this->getRoutingKey(), |
198
|
1 |
|
'body' => (string) $this->body, |
199
|
1 |
|
'attributes' => $this->attributes, |
200
|
1 |
|
'flags' => $this->flags |
201
|
1 |
|
)); |
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
public function setExpiration($expirationInSeconds) |
205
|
|
|
{ |
206
|
1 |
|
$ttlInMs = 1000 * (int) $expirationInSeconds; |
207
|
|
|
|
208
|
1 |
|
$this->setAttribute('expiration', (string) $ttlInMs); |
209
|
|
|
|
210
|
1 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
2 |
|
public static function buildFromReadableMessage(ReadableMessage $readableMessage, $newRoutingKey = false) |
214
|
|
|
{ |
215
|
2 |
|
$routingKey = $readableMessage->getRoutingKey(); |
216
|
|
|
|
217
|
2 |
|
if($newRoutingKey !== false) |
218
|
2 |
|
{ |
219
|
1 |
|
$routingKey = $newRoutingKey; |
220
|
1 |
|
} |
221
|
|
|
|
222
|
2 |
|
$writableMessage = new static($routingKey); |
223
|
2 |
|
$writableMessage->setBody($readableMessage->getBody()); |
224
|
|
|
|
225
|
2 |
|
$writableMessage->addHeaders($readableMessage->getHeaders()); |
226
|
|
|
|
227
|
2 |
|
$attributes = $readableMessage->getAttributes(); |
228
|
2 |
|
$skippedAttributes = array('timestamp', 'headers', 'app_id', 'routing_key'); |
229
|
2 |
|
foreach($attributes as $attributeName => $value) |
230
|
|
|
{ |
231
|
2 |
|
if(! in_array($attributeName, $skippedAttributes)) |
232
|
2 |
|
{ |
233
|
2 |
|
$writableMessage->setAttribute($attributeName, $value); |
234
|
2 |
|
} |
235
|
2 |
|
} |
236
|
|
|
|
237
|
2 |
|
return $writableMessage; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.