55use Enqueue \AmqpTools \DelayStrategyAware ;
66use Enqueue \AmqpTools \DelayStrategyAwareTrait ;
77use Interop \Amqp \AmqpBind as InteropAmqpBind ;
8+ use Interop \Amqp \AmqpConsumer as InteropAmqpConsumer ;
89use Interop \Amqp \AmqpContext as InteropAmqpContext ;
910use Interop \Amqp \AmqpQueue as InteropAmqpQueue ;
1011use Interop \Amqp \AmqpTopic as InteropAmqpTopic ;
@@ -41,6 +42,11 @@ class AmqpContext implements InteropAmqpContext, DelayStrategyAware
4142 */
4243 private $ receiveMethod ;
4344
45+ /**
46+ * @var array
47+ */
48+ private $ basicConsumeSubscribers ;
49+
4450 /**
4551 * Callable must return instance of \AMQPChannel once called.
4652 *
@@ -60,6 +66,7 @@ public function __construct($extChannel, $receiveMethod)
6066 }
6167
6268 $ this ->buffer = new Buffer ();
69+ $ this ->basicConsumeSubscribers = [];
6370 }
6471
6572 /**
@@ -289,4 +296,121 @@ public function getExtChannel()
289296
290297 return $ this ->extChannel ;
291298 }
299+
300+ /**
301+ * Notify broker that the channel is interested in consuming messages from this queue.
302+ *
303+ * @param InteropAmqpConsumer $consumer
304+ * @param callable $callback A callback function to which the
305+ * consumed message will be passed. The
306+ * function must accept at a minimum
307+ * one parameter, an \Interop\Amqp\AmqpMessage object,
308+ * and an optional second parameter
309+ * the \Interop\Amqp\AmqpConsumer from which the message was
310+ * consumed. The \Interop\Amqp\AmqpContext::basicConsume() will
311+ * not return the processing thread back to
312+ * the PHP script until the callback
313+ * function returns FALSE.
314+ */
315+ public function basicConsumeSubscribe (InteropAmqpConsumer $ consumer , callable $ callback )
316+ {
317+ $ extQueue = new \AMQPQueue ($ this ->getExtChannel ());
318+ $ extQueue ->setName ($ consumer ->getQueue ()->getQueueName ());
319+
320+ $ extQueue ->consume (null , Flags ::convertConsumerFlags ($ consumer ->getFlags ()), $ consumer ->getConsumerTag ());
321+
322+ $ consumerTag = $ extQueue ->getConsumerTag ();
323+ $ consumer ->setConsumerTag ($ consumerTag );
324+ $ this ->basicConsumeSubscribers [$ consumerTag ] = [$ consumer , $ callback ];
325+ }
326+
327+ public function basicConsumeUnsubscribe (InteropAmqpConsumer $ consumer )
328+ {
329+ // if ($consumer->getConsumerTag()) {
330+ // $consumerTag = $consumer->getConsumerTag();
331+ // $consumer->setConsumerTag(null);
332+ //
333+ // $extQueue = new \AMQPQueue($this->getExtChannel());
334+ // $extQueue->setName($consumer->getQueue()->getQueueName());
335+ //
336+ // $extQueue->cancel($consumerTag);
337+ // unset($this->basicConsumeSubscribers[$consumerTag]);
338+ // }
339+ }
340+
341+ /**
342+ * @param float|int $timeout milliseconds, consumes endlessly if zero set
343+ */
344+ public function basicConsume ($ timeout = 0 )
345+ {
346+ if (empty ($ this ->basicConsumeSubscribers )) {
347+ throw new \LogicException ('There is no subscribers. Consider calling basicConsumeSubscribe before consuming ' );
348+ }
349+
350+ /** @var \AMQPQueue $extQueue */
351+ $ extConnection = $ this ->getExtChannel ()->getConnection ();
352+
353+ $ originalTimeout = $ extConnection ->getReadTimeout ();
354+ try {
355+ $ extConnection ->setReadTimeout ($ timeout / 1000 );
356+
357+ reset ($ this ->basicConsumeSubscribers );
358+ /** @var $consumer AmqpConsumer */
359+ list ($ consumer ) = current ($ this ->basicConsumeSubscribers );
360+
361+ $ extQueue = new \AMQPQueue ($ this ->getExtChannel ());
362+ $ extQueue ->setName ($ consumer ->getQueue ()->getQueueName ());
363+ $ extQueue ->consume (function (\AMQPEnvelope $ extEnvelope , \AMQPQueue $ q ) {
364+ $ message = $ this ->convertMessage ($ extEnvelope );
365+ $ message ->setConsumerTag ($ q ->getConsumerTag ());
366+
367+ /**
368+ * @var AmqpConsumer
369+ * @var callable $callback
370+ */
371+ list ($ consumer , $ callback ) = $ this ->basicConsumeSubscribers [$ q ->getConsumerTag ()];
372+
373+ return call_user_func ($ callback , $ message , $ consumer );
374+ }, AMQP_JUST_CONSUME );
375+ } catch (\AMQPQueueException $ e ) {
376+ if ('Consumer timeout exceed ' == $ e ->getMessage ()) {
377+ return null ;
378+ }
379+
380+ throw $ e ;
381+ } finally {
382+ $ extConnection ->setReadTimeout ($ originalTimeout );
383+ }
384+ }
385+
386+ /**
387+ * @param \AMQPEnvelope $extEnvelope
388+ *
389+ * @return AmqpMessage
390+ */
391+ private function convertMessage (\AMQPEnvelope $ extEnvelope )
392+ {
393+ $ message = new AmqpMessage (
394+ $ extEnvelope ->getBody (),
395+ $ extEnvelope ->getHeaders (),
396+ [
397+ 'message_id ' => $ extEnvelope ->getMessageId (),
398+ 'correlation_id ' => $ extEnvelope ->getCorrelationId (),
399+ 'app_id ' => $ extEnvelope ->getAppId (),
400+ 'type ' => $ extEnvelope ->getType (),
401+ 'content_encoding ' => $ extEnvelope ->getContentEncoding (),
402+ 'content_type ' => $ extEnvelope ->getContentType (),
403+ 'expiration ' => $ extEnvelope ->getExpiration (),
404+ 'priority ' => $ extEnvelope ->getPriority (),
405+ 'reply_to ' => $ extEnvelope ->getReplyTo (),
406+ 'timestamp ' => $ extEnvelope ->getTimeStamp (),
407+ 'user_id ' => $ extEnvelope ->getUserId (),
408+ ]
409+ );
410+ $ message ->setRedelivered ($ extEnvelope ->isRedelivery ());
411+ $ message ->setDeliveryTag ($ extEnvelope ->getDeliveryTag ());
412+ $ message ->setRoutingKey ($ extEnvelope ->getRoutingKey ());
413+
414+ return $ message ;
415+ }
292416}
0 commit comments