|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\VarDumper\Caster; |
| 13 | + |
| 14 | +use RdKafka; |
| 15 | +use RdKafka\Conf; |
| 16 | +use RdKafka\Exception as RdKafkaException; |
| 17 | +use RdKafka\KafkaConsumer; |
| 18 | +use RdKafka\Message; |
| 19 | +use RdKafka\Metadata\Broker as BrokerMetadata; |
| 20 | +use RdKafka\Metadata\Collection as CollectionMetadata; |
| 21 | +use RdKafka\Metadata\Partition as PartitionMetadata; |
| 22 | +use RdKafka\Metadata\Topic as TopicMetadata; |
| 23 | +use RdKafka\Topic; |
| 24 | +use RdKafka\TopicConf; |
| 25 | +use RdKafka\TopicPartition; |
| 26 | +use Symfony\Component\VarDumper\Cloner\Stub; |
| 27 | + |
| 28 | +/** |
| 29 | + * Casts RdKafka related classes to array representation. |
| 30 | + * |
| 31 | + * @author Romain Neutron <imprec@gmail.com> |
| 32 | + */ |
| 33 | +class RdKafkaCaster |
| 34 | +{ |
| 35 | + public static function castKafkaConsumer(KafkaConsumer $c, array $a, Stub $stub, $isNested) |
| 36 | + { |
| 37 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 38 | + |
| 39 | + try { |
| 40 | + $assignment = $c->getAssignment(); |
| 41 | + } catch (RdKafkaException $e) { |
| 42 | + $assignment = []; |
| 43 | + } |
| 44 | + |
| 45 | + $a += [ |
| 46 | + $prefix.'subscription' => $c->getSubscription(), |
| 47 | + $prefix.'assignment' => $assignment, |
| 48 | + ]; |
| 49 | + |
| 50 | + $a += self::extractMetadata($c); |
| 51 | + |
| 52 | + return $a; |
| 53 | + } |
| 54 | + |
| 55 | + public static function castTopic(Topic $c, array $a, Stub $stub, $isNested) |
| 56 | + { |
| 57 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 58 | + |
| 59 | + $a += [ |
| 60 | + $prefix.'name' => $c->getName(), |
| 61 | + ]; |
| 62 | + |
| 63 | + return $a; |
| 64 | + } |
| 65 | + |
| 66 | + public static function castTopicPartition(TopicPartition $c, array $a) |
| 67 | + { |
| 68 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 69 | + |
| 70 | + $a += [ |
| 71 | + $prefix.'offset' => $c->getOffset(), |
| 72 | + $prefix.'partition' => $c->getPartition(), |
| 73 | + $prefix.'topic' => $c->getTopic(), |
| 74 | + ]; |
| 75 | + |
| 76 | + return $a; |
| 77 | + } |
| 78 | + |
| 79 | + public static function castMessage(Message $c, array $a, Stub $stub, $isNested) |
| 80 | + { |
| 81 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 82 | + |
| 83 | + $a += [ |
| 84 | + $prefix.'errstr' => $c->errstr(), |
| 85 | + ]; |
| 86 | + |
| 87 | + return $a; |
| 88 | + } |
| 89 | + |
| 90 | + public static function castConf(Conf $c, array $a, Stub $stub, $isNested) |
| 91 | + { |
| 92 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 93 | + |
| 94 | + foreach ($c->dump() as $key => $value) { |
| 95 | + $a[$prefix.$key] = $value; |
| 96 | + } |
| 97 | + |
| 98 | + return $a; |
| 99 | + } |
| 100 | + |
| 101 | + public static function castTopicConf(TopicConf $c, array $a, Stub $stub, $isNested) |
| 102 | + { |
| 103 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 104 | + |
| 105 | + foreach ($c->dump() as $key => $value) { |
| 106 | + $a[$prefix.$key] = $value; |
| 107 | + } |
| 108 | + |
| 109 | + return $a; |
| 110 | + } |
| 111 | + |
| 112 | + public static function castRdKafka(\RdKafka $c, array $a, Stub $stub, $isNested) |
| 113 | + { |
| 114 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 115 | + |
| 116 | + $a += [ |
| 117 | + $prefix.'out_q_len' => $c->getOutQLen(), |
| 118 | + ]; |
| 119 | + |
| 120 | + $a += self::extractMetadata($c); |
| 121 | + |
| 122 | + return $a; |
| 123 | + } |
| 124 | + |
| 125 | + public static function castCollectionMetadata(CollectionMetadata $c, array $a, Stub $stub, $isNested) |
| 126 | + { |
| 127 | + $a += iterator_to_array($c); |
| 128 | + |
| 129 | + return $a; |
| 130 | + } |
| 131 | + |
| 132 | + public static function castTopicMetadata(TopicMetadata $c, array $a, Stub $stub, $isNested) |
| 133 | + { |
| 134 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 135 | + |
| 136 | + $a += [ |
| 137 | + $prefix.'name' => $c->getTopic(), |
| 138 | + $prefix.'partitions' => $c->getPartitions(), |
| 139 | + ]; |
| 140 | + |
| 141 | + return $a; |
| 142 | + } |
| 143 | + |
| 144 | + public static function castPartitionMetadata(PartitionMetadata $c, array $a, Stub $stub, $isNested) |
| 145 | + { |
| 146 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 147 | + |
| 148 | + $a += [ |
| 149 | + $prefix.'id' => $c->getId(), |
| 150 | + $prefix.'err' => $c->getErr(), |
| 151 | + $prefix.'leader' => $c->getLeader(), |
| 152 | + ]; |
| 153 | + |
| 154 | + return $a; |
| 155 | + } |
| 156 | + |
| 157 | + public static function castBrokerMetadata(BrokerMetadata $c, array $a, Stub $stub, $isNested) |
| 158 | + { |
| 159 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 160 | + |
| 161 | + $a += [ |
| 162 | + $prefix.'id' => $c->getId(), |
| 163 | + $prefix.'host' => $c->getHost(), |
| 164 | + $prefix.'port' => $c->getPort(), |
| 165 | + ]; |
| 166 | + |
| 167 | + return $a; |
| 168 | + } |
| 169 | + |
| 170 | + private static function extractMetadata($c) |
| 171 | + { |
| 172 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 173 | + |
| 174 | + try { |
| 175 | + $m = $c->getMetadata(true, null, 500); |
| 176 | + } catch (RdKafkaException $e) { |
| 177 | + return []; |
| 178 | + } |
| 179 | + |
| 180 | + return [ |
| 181 | + $prefix.'orig_broker_id' => $m->getOrigBrokerId(), |
| 182 | + $prefix.'orig_broker_name' => $m->getOrigBrokerName(), |
| 183 | + $prefix.'brokers' => $m->getBrokers(), |
| 184 | + $prefix.'topics' => $m->getTopics(), |
| 185 | + ]; |
| 186 | + } |
| 187 | +} |
0 commit comments