1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OldSound\RabbitMqBundle\RabbitMq; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
7
|
|
|
use PhpAmqpLib\Wire\AMQPTable; |
8
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\JsonValidator; |
9
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\xmlValidator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Producer, that publishes AMQP Messages |
13
|
|
|
*/ |
14
|
|
|
class Producer extends BaseAmqp implements ProducerInterface |
15
|
|
|
{ |
16
|
|
|
protected $contentType = 'text/plain'; |
17
|
|
|
protected $deliveryMode = 2; |
18
|
|
|
protected $defaultRoutingKey = ''; |
19
|
|
|
public $validatorCheck = false; |
20
|
|
|
public $validatorFile = ""; |
21
|
|
|
|
22
|
|
|
public function setValidatorFile($validatorFile){ |
23
|
|
|
$this->validatorFile = $validatorFile; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setValidatorCheck($validatorCheck) |
27
|
|
|
{ |
28
|
|
|
$this->validatorCheck = $validatorCheck; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setContentType($contentType) |
32
|
|
|
{ |
33
|
|
|
$this->contentType = $contentType; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setDeliveryMode($deliveryMode) |
39
|
|
|
{ |
40
|
|
|
$this->deliveryMode = $deliveryMode; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setDefaultRoutingKey($defaultRoutingKey) |
46
|
|
|
{ |
47
|
|
|
$this->defaultRoutingKey = $defaultRoutingKey; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function getBasicProperties() |
53
|
|
|
{ |
54
|
|
|
return array('content_type' => $this->contentType, 'delivery_mode' => $this->deliveryMode); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function validateMessage($msg) |
58
|
|
|
{ |
59
|
|
|
if (!array_key_exists($this->contentType, $this->validatorFile)){ |
|
|
|
|
60
|
|
|
throw new Exception('Cannot find validator file of ' . $this->contentType); |
61
|
|
|
} |
62
|
|
|
// Insert new validator here |
63
|
|
|
if ($this->contentType == 'application/json'){ |
64
|
|
|
$validatorEngine = new JsonValidator(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->contentType == 'application/xml'){ |
68
|
|
|
$validatorEngine = new xmlValidator(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!$validatorEngine->isValid($msg, $this->validatorFile[$this->contentType])){ |
|
|
|
|
72
|
|
|
throw new Exception($this->contentType . " message verification failed"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Publishes the message and merges additional properties with basic properties |
79
|
|
|
* |
80
|
|
|
* @param string $msgBody |
81
|
|
|
* @param string $routingKey |
82
|
|
|
* @param array $additionalProperties |
83
|
|
|
* @param array $headers |
84
|
|
|
*/ |
85
|
|
|
public function publish($msgBody, $routingKey = null, $additionalProperties = array(), array $headers = null) |
86
|
|
|
{ |
87
|
|
|
if ($this->validatorCheck){ |
88
|
|
|
$this->validateMessage($msgBody); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($this->autoSetupFabric) { |
92
|
|
|
$this->setupFabric(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$msg = new AMQPMessage((string) $msgBody, array_merge($this->getBasicProperties(), $additionalProperties)); |
96
|
|
|
|
97
|
|
|
if (!empty($headers)) { |
98
|
|
|
$headersTable = new AMQPTable($headers); |
99
|
|
|
$msg->set('application_headers', $headersTable); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$real_routingKey = $routingKey !== null ? $routingKey : $this->defaultRoutingKey; |
103
|
|
|
$this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$real_routingKey); |
104
|
|
|
$this->logger->debug('AMQP message published', array( |
105
|
|
|
'amqp' => array( |
106
|
|
|
'body' => $msgBody, |
107
|
|
|
'routingkeys' => $routingKey, |
108
|
|
|
'properties' => $additionalProperties, |
109
|
|
|
'headers' => $headers |
110
|
|
|
) |
111
|
|
|
)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|