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