Skip to content

Commit 8708592

Browse files
committed
amqplib
1 parent b7a9979 commit 8708592

9 files changed

Lines changed: 824 additions & 0 deletions

‎composer.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"enqueue/enqueue": "*@dev",
88
"enqueue/stomp": "*@dev",
99
"enqueue/amqp-ext": "*@dev",
10+
"enqueue/amqplib": "*@dev",
1011
"enqueue/redis": "*@dev",
1112
"enqueue/fs": "*@dev",
1213
"enqueue/null": "*@dev",
@@ -68,6 +69,10 @@
6869
"type": "path",
6970
"url": "pkg/amqp-ext"
7071
},
72+
{
73+
"type": "path",
74+
"url": "pkg/amqplib"
75+
},
7176
{
7277
"type": "path",
7378
"url": "pkg/redis"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Enqueue\Amqplib;
4+
5+
use Interop\Queue\PsrConnectionFactory;
6+
use PhpAmqpLib\Connection\AbstractConnection;
7+
use PhpAmqpLib\Connection\AMQPStreamConnection;
8+
9+
class AmqpConnectionFactory implements PsrConnectionFactory
10+
{
11+
private $config;
12+
private $connection;
13+
14+
public function __construct(array $config = [])
15+
{
16+
$this->config = array_replace($this->defaultConfig(), $config);
17+
}
18+
19+
public function createContext()
20+
{
21+
return new AmqpContext($this->establishConnection());
22+
}
23+
24+
private function establishConnection()
25+
{
26+
if (false == $this->connection) {
27+
$this->connection = new AMQPStreamConnection(
28+
$this->config['host'],
29+
$this->config['port'],
30+
$this->config['user'],
31+
$this->config['pass'],
32+
$this->config['vhost']
33+
);
34+
}
35+
36+
return $this->connection;
37+
}
38+
39+
private function defaultConfig()
40+
{
41+
return [
42+
'host' => 'localhost',
43+
'port' => 5672,
44+
'vhost' => '/',
45+
'user' => 'guest',
46+
'pass' => 'guest',
47+
];
48+
}
49+
}

‎pkg/amqplib/AmqpConsumer.php‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Enqueue\Amqplib;
4+
5+
use Interop\Queue\InvalidMessageException;
6+
use Interop\Queue\PsrConsumer;
7+
use Interop\Queue\PsrMessage;
8+
use PhpAmqpLib\Channel\AMQPChannel;
9+
use PhpAmqpLib\Message\AMQPMessage as LibAMQPMessage;
10+
use PhpAmqpLib\Wire\AMQPTable;
11+
12+
class AmqpConsumer implements PsrConsumer
13+
{
14+
private $channel;
15+
private $queue;
16+
17+
public function __construct(AMQPChannel $channel, AmqpQueue $queue)
18+
{
19+
$this->channel = $channel;
20+
$this->queue = $queue;
21+
}
22+
23+
public function getQueue()
24+
{
25+
return $this->queue;
26+
}
27+
28+
public function receive($timeout = 0)
29+
{
30+
$end = microtime(true) + ($timeout / 1000);
31+
32+
while (0 === $timeout || microtime(true) < $end) {
33+
if ($message = $this->receiveNoWait()) {
34+
return $message;
35+
}
36+
37+
usleep(100000); //100ms
38+
}
39+
}
40+
41+
public function receiveNoWait()
42+
{
43+
if ($message = $this->channel->basic_get($this->queue->getQueueName())) {
44+
return $this->convertMessage($message);
45+
}
46+
}
47+
48+
public function acknowledge(PsrMessage $message)
49+
{
50+
InvalidMessageException::assertMessageInstanceOf($message, AmqpMessage::class);
51+
52+
$this->channel->basic_ack($message->getDeliveryTag());
53+
}
54+
55+
public function reject(PsrMessage $message, $requeue = false)
56+
{
57+
InvalidMessageException::assertMessageInstanceOf($message, AmqpMessage::class);
58+
59+
$this->channel->basic_reject($message->getDeliveryTag(), $requeue);
60+
}
61+
62+
private function convertMessage(LibAMQPMessage $amqpMessage)
63+
{
64+
$headers = new AMQPTable($amqpMessage->get_properties());
65+
$headers = $headers->getNativeData();
66+
67+
$properties = [];
68+
if (isset($headers['application_headers'])) {
69+
$properties = $headers['application_headers'];
70+
}
71+
unset($headers['application_headers']);
72+
73+
$message = new AmqpMessage($amqpMessage->getBody(), $properties, $headers);
74+
$message->setDeliveryTag($amqpMessage->delivery_info['delivery_tag']);
75+
$message->setRedelivered($amqpMessage->delivery_info['redelivered']);
76+
77+
return $message;
78+
}
79+
}

‎pkg/amqplib/AmqpContext.php‎

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace Enqueue\Amqplib;
4+
5+
use Interop\Queue\Exception;
6+
use Interop\Queue\InvalidDestinationException;
7+
use Interop\Queue\PsrContext;
8+
use Interop\Queue\PsrDestination;
9+
use Interop\Queue\PsrTopic;
10+
use PhpAmqpLib\Connection\AbstractConnection;
11+
12+
class AmqpContext implements PsrContext
13+
{
14+
private $connection;
15+
private $channel;
16+
17+
public function __construct(AbstractConnection $connection)
18+
{
19+
$this->connection = $connection;
20+
}
21+
22+
public function createMessage($body = null, array $properties = [], array $headers = [])
23+
{
24+
return new AmqpMessage($body, $properties, $headers);
25+
}
26+
27+
public function createQueue($name)
28+
{
29+
return new AmqpQueue($name);
30+
}
31+
32+
public function createTopic($name)
33+
{
34+
return new AmqpTopic($name);
35+
}
36+
37+
public function createConsumer(PsrDestination $destination)
38+
{
39+
InvalidDestinationException::assertDestinationInstanceOf($destination, AmqpQueue::class);
40+
41+
return new AmqpConsumer($this->getChannel(), $destination);
42+
}
43+
44+
public function createProducer()
45+
{
46+
return new AmqpProducer($this->getChannel());
47+
}
48+
49+
public function createTemporaryQueue()
50+
{
51+
$queue = $this->createQueue(null);
52+
$queue->setExclusive(true);
53+
54+
$this->declareQueue($queue);
55+
56+
return $queue;
57+
}
58+
59+
public function declareTopic(PsrDestination $destination)
60+
{
61+
InvalidDestinationException::assertDestinationInstanceOf($destination, AmqpTopic::class);
62+
63+
$this->getChannel()->exchange_declare(
64+
$destination->getTopicName(),
65+
$destination->getType(),
66+
$destination->isPassive(),
67+
$destination->isDurable(),
68+
$destination->isAutoDelete(),
69+
$destination->isInternal(),
70+
$destination->isNoWait(),
71+
$destination->getArguments(),
72+
$destination->getTicket()
73+
);
74+
}
75+
76+
public function declareQueue(PsrDestination $destination)
77+
{
78+
InvalidDestinationException::assertDestinationInstanceOf($destination, AmqpQueue::class);
79+
80+
$this->getChannel()->queue_declare(
81+
$destination->getQueueName(),
82+
$destination->isPassive(),
83+
$destination->isDurable(),
84+
$destination->isExclusive(),
85+
$destination->isAutoDelete(),
86+
$destination->isNoWait(),
87+
$destination->getArguments(),
88+
$destination->getTicket()
89+
);
90+
}
91+
92+
public function bind(PsrDestination $source, PsrDestination $target)
93+
{
94+
$source instanceof PsrTopic
95+
? InvalidDestinationException::assertDestinationInstanceOf($source, AmqpTopic::class)
96+
: InvalidDestinationException::assertDestinationInstanceOf($source, AmqpQueue::class)
97+
;
98+
99+
$target instanceof PsrTopic
100+
? InvalidDestinationException::assertDestinationInstanceOf($target, AmqpTopic::class)
101+
: InvalidDestinationException::assertDestinationInstanceOf($target, AmqpQueue::class)
102+
;
103+
104+
if ($source instanceof AmqpQueue && $target instanceof AmqpQueue) {
105+
throw new Exception('Is not possible to bind queue to queue. It is possible to bind topic to queue or topic to topic');
106+
}
107+
108+
// bind exchange to exchange
109+
if ($source instanceof AmqpTopic && $target instanceof AmqpTopic) {
110+
$this->getChannel()->exchange_bind(
111+
$target->getTopicName(),
112+
$source->getTopicName(),
113+
$source->getRoutingKey(),
114+
$source->isNowait(),
115+
$source->getArguments(),
116+
$source->getTicket()
117+
);
118+
// bind queue to exchange
119+
} elseif ($source instanceof AmqpQueue) {
120+
$this->getChannel()->queue_bind(
121+
$source->getQueueName(),
122+
$target->getTopicName(),
123+
$target->getRoutingKey(),
124+
$target->isNowait(),
125+
$target->getArguments(),
126+
$target->getTicket()
127+
);
128+
// bind exchange to queue
129+
} else {
130+
$this->getChannel()->queue_bind(
131+
$target->getQueueName(),
132+
$source->getTopicName(),
133+
$source->getRoutingKey(),
134+
$source->isNowait(),
135+
$source->getArguments(),
136+
$source->getTicket()
137+
);
138+
}
139+
}
140+
141+
/**
142+
* {@inheritdoc}
143+
*/
144+
public function close()
145+
{
146+
if ($this->channel) {
147+
$this->channel->close();
148+
}
149+
}
150+
151+
private function getChannel()
152+
{
153+
if (null === $this->channel) {
154+
$this->channel = $this->connection->channel();
155+
}
156+
157+
return $this->channel;
158+
}
159+
}

0 commit comments

Comments
 (0)