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