|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Puzzle\AMQP\Clients; |
|
4
|
|
|
|
|
5
|
|
|
use Puzzle\AMQP\Client; |
|
6
|
|
|
use Puzzle\AMQP\WritableMessage; |
|
7
|
|
|
use Puzzle\AMQP\Messages\InMemoryJson; |
|
8
|
|
|
use Psr\Log\NullLogger; |
|
9
|
|
|
use Puzzle\AMQP\Collections; |
|
10
|
|
|
|
|
11
|
|
|
class InMemory implements Client |
|
12
|
|
|
{ |
|
13
|
|
|
use \Psr\Log\LoggerAwareTrait; |
|
14
|
|
|
|
|
15
|
|
|
private |
|
16
|
|
|
$sentMessages; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
14 |
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
14 |
|
$this->sentMessages = array(); |
|
21
|
14 |
|
$this->logger = new NullLogger(); |
|
22
|
14 |
|
} |
|
23
|
|
|
|
|
24
|
8 |
|
public function publish($exchangeName, WritableMessage $message) |
|
25
|
|
|
{ |
|
26
|
8 |
|
$this->updateMessageAttributes($message); |
|
27
|
8 |
|
$this->saveMessage($exchangeName, $message); |
|
28
|
|
|
} |
|
29
|
1 |
|
|
|
30
|
|
|
private function updateMessageAttributes(WritableMessage $message) |
|
31
|
1 |
|
{ |
|
32
|
|
|
$message->setAttribute('app_id', 'memory'); |
|
33
|
|
|
$message->addHeader('routing_key', $message->getRoutingKey()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getQueue($queueName) |
|
37
|
|
|
{ |
|
38
|
|
|
throw new \RuntimeException('This AMQP Client must be used only for sending purpose'); |
|
39
|
8 |
|
} |
|
40
|
|
|
|
|
41
|
8 |
|
public function getExchange($exchangeName) |
|
42
|
8 |
|
{ |
|
43
|
|
|
throw new \RuntimeException('This AMQP Client must be used only for sending purpose'); |
|
44
|
8 |
|
} |
|
45
|
8 |
|
|
|
46
|
|
|
private function saveMessage($exchangeName, WritableMessage $message) |
|
47
|
8 |
|
{ |
|
48
|
|
|
$this->sentMessages[] = array( |
|
49
|
8 |
|
'exchange' => $exchangeName, |
|
50
|
|
|
'message' => $message |
|
51
|
|
|
); |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function getSentMessages() |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->sentMessages; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function dropSentMessages() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->sentMessages = array(); |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
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.