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