1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiapi\event; |
4
|
|
|
|
5
|
|
|
use League\Event\AbstractListener; |
6
|
|
|
use League\Event\EventInterface; |
|
|
|
|
7
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
8
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
9
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PublishToQueueListener |
15
|
|
|
* |
16
|
|
|
* @author Dmytro Naumenko <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class PublishToQueueListener extends AbstractListener |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var AMQPStreamConnection |
22
|
|
|
*/ |
23
|
|
|
protected $amqp; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var LoggerInterface |
27
|
|
|
*/ |
28
|
|
|
private $logger; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var AMQPChannel |
32
|
|
|
*/ |
33
|
|
|
protected $channel; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string the queue name for the published messages |
37
|
|
|
*/ |
38
|
|
|
public $queue; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int|null When number is passed, Priority queues are supported with the maximum priority as set. |
42
|
|
|
* `null` disables priority queue support |
43
|
|
|
*/ |
44
|
|
|
public $maxPriority = null; |
45
|
|
|
|
46
|
|
|
public function __construct(AMQPStreamConnection $amqp, LoggerInterface $logger) |
47
|
|
|
{ |
48
|
|
|
$this->amqp = $amqp; |
49
|
|
|
$this->logger = $logger; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Handle an event. |
54
|
|
|
* @param EventInterface $event |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function handle(EventInterface $event): void |
58
|
|
|
{ |
59
|
|
|
if ($this->queue === null) { |
60
|
|
|
throw new \RuntimeException('Property PublishToQueueListener::queue must be set'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
try { |
64
|
|
|
$message = $this->createMessage($event); |
65
|
|
|
$this->getChannel()->basic_publish($message, '', $this->queue); |
66
|
|
|
} catch (InvalidConfigException $exception) { |
|
|
|
|
67
|
|
|
$this->logger->critical($exception->getMessage()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getChannel(): AMQPChannel |
72
|
|
|
{ |
73
|
|
|
if ($this->channel === null) { |
74
|
|
|
$this->channel = $channel = $this->amqp->channel(); |
75
|
|
|
|
76
|
|
|
$table = new \PhpAmqpLib\Wire\AMQPTable(); |
77
|
|
|
if ($this->maxPriority !== null) { |
78
|
|
|
$table->set('x-max-priority', $this->maxPriority); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$channel->queue_declare($this->queue, false, true, false, false, false, $table); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->channel; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function createMessage($event): AMQPMessage |
88
|
|
|
{ |
89
|
|
|
if (!$event instanceof \JsonSerializable) { |
|
|
|
|
90
|
|
|
throw new InvalidConfigException('Event "' . get_class($event) . '" can not be sent to queue'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$options = []; |
94
|
|
|
if ($this->maxPriority !== null && $event instanceof PriorityEventInterface) { |
95
|
|
|
if ($event->getPriority() > $this->maxPriority) { |
96
|
|
|
throw new InvalidConfigException('Event "' . get_class($event) . '" priority is above supported maximum'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$options['priority'] = $event->getPriority(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return new AMQPMessage( |
103
|
|
|
json_encode($event->jsonSerialize(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), |
104
|
|
|
array_merge([ |
105
|
|
|
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT, |
106
|
|
|
'content_type' => 'application/json', |
107
|
|
|
], $options) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: