|
| 1 | +<?php |
| 2 | +namespace Enqueue\RdKafka; |
| 3 | + |
| 4 | +use Interop\Queue\PsrConsumer; |
| 5 | +use Interop\Queue\PsrContext; |
| 6 | +use Interop\Queue\PsrDestination; |
| 7 | +use Interop\Queue\PsrMessage; |
| 8 | +use Interop\Queue\PsrProducer; |
| 9 | +use Interop\Queue\PsrQueue; |
| 10 | +use Interop\Queue\PsrTopic; |
| 11 | +use RdKafka\Conf; |
| 12 | +use RdKafka\Producer; |
| 13 | +use RdKafka\TopicConf; |
| 14 | + |
| 15 | +class RdKafkaContext implements PsrContext |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var array |
| 19 | + */ |
| 20 | + private $config; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var Conf |
| 24 | + */ |
| 25 | + private $conf; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Producer |
| 29 | + */ |
| 30 | + private $producer; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param array $config |
| 34 | + */ |
| 35 | + public function __construct(array $config) |
| 36 | + { |
| 37 | + $this->config = $config; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritdoc} |
| 42 | + */ |
| 43 | + public function createMessage($body = '', array $properties = [], array $headers = []) |
| 44 | + { |
| 45 | + return new RdKafkaMessage($body, $properties, $headers); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritdoc} |
| 50 | + * |
| 51 | + * @return RdKafkaTopic |
| 52 | + */ |
| 53 | + public function createTopic($topicName) |
| 54 | + { |
| 55 | + return new RdKafkaTopic($topicName); |
| 56 | + } |
| 57 | + |
| 58 | + public function createQueue($queueName) |
| 59 | + { |
| 60 | + // TODO: Implement createQueue() method. |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | + public function createTemporaryQueue() |
| 67 | + { |
| 68 | + throw new \LogicException('Not implemented'); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * {@inheritdoc} |
| 73 | + * |
| 74 | + * @return RdKafkaProducer |
| 75 | + */ |
| 76 | + public function createProducer() |
| 77 | + { |
| 78 | + return new RdKafkaProducer($this->getProducer()); |
| 79 | + } |
| 80 | + |
| 81 | + public function createConsumer(PsrDestination $destination) |
| 82 | + { |
| 83 | + // TODO: Implement createConsumer() method. |
| 84 | + } |
| 85 | + |
| 86 | + public function close() |
| 87 | + { |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return Producer |
| 93 | + */ |
| 94 | + private function getProducer() |
| 95 | + { |
| 96 | + if (null === $this->producer) { |
| 97 | + $this->producer = new Producer($this->getConf()); |
| 98 | + |
| 99 | + if (isset($this->config['log_level'])) { |
| 100 | + $this->producer->setLogLevel($this->config['log_level']); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return $this->producer; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @return Conf |
| 109 | + */ |
| 110 | + private function getConf() |
| 111 | + { |
| 112 | + if (null === $this->conf) { |
| 113 | + $topicConf = new TopicConf(); |
| 114 | + |
| 115 | + if (isset($this->config['topic']) && is_array($this->config['topic'])) { |
| 116 | + foreach ($this->config['topic'] as $key => $value) { |
| 117 | + $topicConf->set($key, $value); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (isset($this->config['partitioner'])) { |
| 122 | + $topicConf->setPartitioner($this->config['partitioner']); |
| 123 | + } |
| 124 | + |
| 125 | + $this->conf = new Conf(); |
| 126 | + |
| 127 | + if (isset($this->config['global']) && is_array($this->config['global'])) { |
| 128 | + foreach ($this->config['global'] as $key => $value) { |
| 129 | + $this->conf->set($key, $value); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (isset($this->config['dr_msg_cb'])) { |
| 134 | + $this->conf->setDrMsgCb($this->config['dr_msg_cb']); |
| 135 | + } |
| 136 | + |
| 137 | + if (isset($this->config['error_cb'])) { |
| 138 | + $this->conf->setErrorCb($this->config['error_cb']); |
| 139 | + } |
| 140 | + |
| 141 | + if (isset($this->config['rebalance_cb'])) { |
| 142 | + $this->conf->setRebalanceCb($this->config['errorebalance_cbr_cb']); |
| 143 | + } |
| 144 | + |
| 145 | + $this->conf->setDefaultTopicConf($topicConf); |
| 146 | + } |
| 147 | + |
| 148 | + return $this->conf; |
| 149 | + } |
| 150 | +} |
0 commit comments