|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OldSound\RabbitMqBundle\RabbitMq; |
|
4
|
|
|
|
|
5
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
|
6
|
|
|
use PhpAmqpLib\Wire\AMQPTable; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Producer, that publishes AMQP Messages |
|
10
|
|
|
*/ |
|
11
|
|
|
class Producer extends BaseAmqp implements ProducerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
protected $contentType = 'text/plain'; |
|
14
|
|
|
protected $deliveryMode = 2; |
|
15
|
|
|
|
|
16
|
|
|
public function setContentType($contentType) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->contentType = $contentType; |
|
19
|
|
|
|
|
20
|
|
|
return $this; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function setDeliveryMode($deliveryMode) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->deliveryMode = $deliveryMode; |
|
26
|
|
|
|
|
27
|
|
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function getBasicProperties() |
|
31
|
|
|
{ |
|
32
|
|
|
return array('content_type' => $this->contentType, 'delivery_mode' => $this->deliveryMode); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Publishes the message and merges additional properties with basic properties |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $msgBody |
|
39
|
|
|
* @param string $routingKey |
|
40
|
|
|
* @param array $additionalProperties |
|
41
|
|
|
* @param array $headers |
|
42
|
|
|
*/ |
|
43
|
|
|
public function publish($msgBody, $routingKey = '', $additionalProperties = array(), array $headers = null) |
|
44
|
|
|
{ |
|
45
|
|
|
if ($this->autoSetupFabric) { |
|
46
|
|
|
$this->setupFabric(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$msg = new AMQPMessage((string) $msgBody, array_merge($this->getBasicProperties(), $additionalProperties)); |
|
50
|
|
|
|
|
51
|
|
|
if (!empty($headers)) { |
|
52
|
|
|
$headersTable = new AMQPTable($headers); |
|
53
|
|
|
$msg->set('application_headers', $headersTable); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$routingKey); |
|
57
|
|
|
$this->logger->debug('AMQP message published', array( |
|
58
|
|
|
'amqp' => array( |
|
59
|
|
|
'body' => $msgBody, |
|
60
|
|
|
'routingkeys' => $routingKey, |
|
61
|
|
|
'properties' => $additionalProperties, |
|
62
|
|
|
'headers' => $headers |
|
63
|
|
|
) |
|
64
|
|
|
)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|