1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OldSound\RabbitMqBundle\RabbitMq; |
4
|
|
|
|
5
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
6
|
|
|
use PhpAmqpLib\Wire\AMQPTable; |
7
|
|
|
use Swaggest\JsonSchema\Schema; |
8
|
|
|
use Swaggest\JsonSchema\Exception; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Producer, that publishes AMQP Messages |
12
|
|
|
*/ |
13
|
|
|
class Producer extends BaseAmqp implements ProducerInterface |
14
|
|
|
{ |
15
|
|
|
protected $contentType = 'text/plain'; |
16
|
|
|
protected $deliveryMode = 2; |
17
|
|
|
protected $defaultRoutingKey = ''; |
18
|
|
|
public $jsonSchemaCheck = false; |
19
|
|
|
public $jsonSchemaFile = ""; |
20
|
|
|
|
21
|
|
|
public function setJsonSchemaFile($jsonSchemaFile){ |
22
|
|
|
$this->jsonSchemaFile = $jsonSchemaFile; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function setJsonSchemaCheck($jsonSchemaCheck) |
26
|
|
|
{ |
27
|
|
|
$this->jsonSchemaCheck = $jsonSchemaCheck; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function setContentType($contentType) |
31
|
|
|
{ |
32
|
|
|
$this->contentType = $contentType; |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setDeliveryMode($deliveryMode) |
38
|
|
|
{ |
39
|
|
|
$this->deliveryMode = $deliveryMode; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setDefaultRoutingKey($defaultRoutingKey) |
45
|
|
|
{ |
46
|
|
|
$this->defaultRoutingKey = $defaultRoutingKey; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function getBasicProperties() |
52
|
|
|
{ |
53
|
|
|
return array('content_type' => $this->contentType, 'delivery_mode' => $this->deliveryMode); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function validateJsonMessage($msg){ |
57
|
|
|
try{ |
58
|
|
|
$schema = Schema::import(json_decode(file_get_contents($this->jsonSchemaFile, true))); |
59
|
|
|
$schema->in($msg); |
60
|
|
|
}catch (Exception $e){ |
61
|
|
|
throw new Exception($e->getMessage()); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Publishes the message and merges additional properties with basic properties |
67
|
|
|
* |
68
|
|
|
* @param string $msgBody |
69
|
|
|
* @param string $routingKey |
70
|
|
|
* @param array $additionalProperties |
71
|
|
|
* @param array $headers |
72
|
|
|
*/ |
73
|
|
|
public function publish($msgBody, $routingKey = null, $additionalProperties = array(), array $headers = null) |
74
|
|
|
{ |
75
|
|
|
if ($this->contentType == 'application/json' && $this->jsonSchemaCheck){ |
76
|
|
|
$this->validateJsonMessage($msgBody); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($this->autoSetupFabric) { |
80
|
|
|
$this->setupFabric(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$msg = new AMQPMessage((string) $msgBody, array_merge($this->getBasicProperties(), $additionalProperties)); |
84
|
|
|
|
85
|
|
|
if (!empty($headers)) { |
86
|
|
|
$headersTable = new AMQPTable($headers); |
87
|
|
|
$msg->set('application_headers', $headersTable); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$real_routingKey = $routingKey !== null ? $routingKey : $this->defaultRoutingKey; |
91
|
|
|
$this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$real_routingKey); |
92
|
|
|
$this->logger->debug('AMQP message published', array( |
93
|
|
|
'amqp' => array( |
94
|
|
|
'body' => $msgBody, |
95
|
|
|
'routingkeys' => $routingKey, |
96
|
|
|
'properties' => $additionalProperties, |
97
|
|
|
'headers' => $headers |
98
|
|
|
) |
99
|
|
|
)); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|