|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The MIT License |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2010 Alvaro Videla |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
15
|
|
|
* all copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
* SOFTWARE. |
|
24
|
|
|
* |
|
25
|
|
|
* PHP version 5.3 |
|
26
|
|
|
* |
|
27
|
|
|
* @category Thumper |
|
28
|
|
|
* @package Thumper |
|
29
|
|
|
* @author Alvaro Videla |
|
30
|
|
|
* @copyright 2010 Alvaro Videla. All rights reserved. |
|
31
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
|
32
|
|
|
* @link https://github.com/videlalvaro/Thumper |
|
33
|
|
|
*/ |
|
34
|
|
|
namespace Thumper; |
|
35
|
|
|
|
|
36
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
|
37
|
|
|
|
|
38
|
|
|
class Producer extends BaseAmqp |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $exchangeReady = false; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $messageBody |
|
47
|
|
|
* @param string $routingKey |
|
48
|
|
|
*/ |
|
49
|
25 |
|
public function publish($messageBody, $routingKey = '') |
|
50
|
|
|
{ |
|
51
|
25 |
|
if (!$this->exchangeReady) { |
|
52
|
25 |
|
if (isset($this->exchangeOptions['name'])) { |
|
53
|
|
|
//declare a durable non autodelete exchange |
|
54
|
25 |
|
$this->channel->exchange_declare( |
|
55
|
25 |
|
$this->exchangeOptions['name'], |
|
56
|
25 |
|
$this->exchangeOptions['type'], |
|
57
|
25 |
|
false, |
|
58
|
25 |
|
true, |
|
59
|
5 |
|
false |
|
60
|
20 |
|
); |
|
61
|
12 |
|
} |
|
62
|
15 |
|
$this->exchangeReady = true; |
|
63
|
12 |
|
} |
|
64
|
|
|
|
|
65
|
15 |
|
$this->setParameter('delivery_mode', self::PERSISTENT); |
|
66
|
|
|
|
|
67
|
15 |
|
$message = new AMQPMessage( |
|
68
|
12 |
|
$messageBody, |
|
69
|
15 |
|
$this->getParameters() |
|
70
|
12 |
|
); |
|
71
|
15 |
|
$this->channel->basic_publish( |
|
72
|
12 |
|
$message, |
|
73
|
15 |
|
$this->exchangeOptions['name'], |
|
74
|
15 |
|
!empty($routingKey) ? $routingKey : $this->routingKey |
|
75
|
12 |
|
); |
|
76
|
5 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|