|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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 Symfony\Component\VarDumper\Cloner\Stub; |
| 15 | + |
| 16 | +/** |
| 17 | + * Casts Amqp related classes to array representation. |
| 18 | + * |
| 19 | + * @author Grégoire Pineau <[email protected]> |
| 20 | + * |
| 21 | + * @final |
| 22 | + */ |
| 23 | +class AmqpCaster |
| 24 | +{ |
| 25 | + private const FLAGS = [ |
| 26 | + \AMQP_DURABLE => 'AMQP_DURABLE', |
| 27 | + \AMQP_PASSIVE => 'AMQP_PASSIVE', |
| 28 | + \AMQP_EXCLUSIVE => 'AMQP_EXCLUSIVE', |
| 29 | + \AMQP_AUTODELETE => 'AMQP_AUTODELETE', |
| 30 | + \AMQP_INTERNAL => 'AMQP_INTERNAL', |
| 31 | + \AMQP_NOLOCAL => 'AMQP_NOLOCAL', |
| 32 | + \AMQP_AUTOACK => 'AMQP_AUTOACK', |
| 33 | + \AMQP_IFEMPTY => 'AMQP_IFEMPTY', |
| 34 | + \AMQP_IFUNUSED => 'AMQP_IFUNUSED', |
| 35 | + \AMQP_MANDATORY => 'AMQP_MANDATORY', |
| 36 | + \AMQP_IMMEDIATE => 'AMQP_IMMEDIATE', |
| 37 | + \AMQP_MULTIPLE => 'AMQP_MULTIPLE', |
| 38 | + \AMQP_NOWAIT => 'AMQP_NOWAIT', |
| 39 | + \AMQP_REQUEUE => 'AMQP_REQUEUE', |
| 40 | + ]; |
| 41 | + |
| 42 | + private const EXCHANGE_TYPES = [ |
| 43 | + \AMQP_EX_TYPE_DIRECT => 'AMQP_EX_TYPE_DIRECT', |
| 44 | + \AMQP_EX_TYPE_FANOUT => 'AMQP_EX_TYPE_FANOUT', |
| 45 | + \AMQP_EX_TYPE_TOPIC => 'AMQP_EX_TYPE_TOPIC', |
| 46 | + \AMQP_EX_TYPE_HEADERS => 'AMQP_EX_TYPE_HEADERS', |
| 47 | + ]; |
| 48 | + |
| 49 | + public static function castConnection(\AMQPConnection $c, array $a, Stub $stub, bool $isNested) |
| 50 | + { |
| 51 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 52 | + |
| 53 | + $a += [ |
| 54 | + $prefix.'is_connected' => $c->isConnected(), |
| 55 | + ]; |
| 56 | + |
| 57 | + // Recent version of the extension already expose private properties |
| 58 | + if (isset($a["\x00AMQPConnection\x00login"])) { |
| 59 | + return $a; |
| 60 | + } |
| 61 | + |
| 62 | + // BC layer in the amqp lib |
| 63 | + if (method_exists($c, 'getReadTimeout')) { |
| 64 | + $timeout = $c->getReadTimeout(); |
| 65 | + } else { |
| 66 | + $timeout = $c->getTimeout(); |
| 67 | + } |
| 68 | + |
| 69 | + $a += [ |
| 70 | + $prefix.'is_connected' => $c->isConnected(), |
| 71 | + $prefix.'login' => $c->getLogin(), |
| 72 | + $prefix.'password' => $c->getPassword(), |
| 73 | + $prefix.'host' => $c->getHost(), |
| 74 | + $prefix.'vhost' => $c->getVhost(), |
| 75 | + $prefix.'port' => $c->getPort(), |
| 76 | + $prefix.'read_timeout' => $timeout, |
| 77 | + ]; |
| 78 | + |
| 79 | + return $a; |
| 80 | + } |
| 81 | + |
| 82 | + public static function castChannel(\AMQPChannel $c, array $a, Stub $stub, bool $isNested) |
| 83 | + { |
| 84 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 85 | + |
| 86 | + $a += [ |
| 87 | + $prefix.'is_connected' => $c->isConnected(), |
| 88 | + $prefix.'channel_id' => $c->getChannelId(), |
| 89 | + ]; |
| 90 | + |
| 91 | + // Recent version of the extension already expose private properties |
| 92 | + if (isset($a["\x00AMQPChannel\x00connection"])) { |
| 93 | + return $a; |
| 94 | + } |
| 95 | + |
| 96 | + $a += [ |
| 97 | + $prefix.'connection' => $c->getConnection(), |
| 98 | + $prefix.'prefetch_size' => $c->getPrefetchSize(), |
| 99 | + $prefix.'prefetch_count' => $c->getPrefetchCount(), |
| 100 | + ]; |
| 101 | + |
| 102 | + return $a; |
| 103 | + } |
| 104 | + |
| 105 | + public static function castQueue(\AMQPQueue $c, array $a, Stub $stub, bool $isNested) |
| 106 | + { |
| 107 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 108 | + |
| 109 | + $a += [ |
| 110 | + $prefix.'flags' => self::extractFlags($c->getFlags()), |
| 111 | + ]; |
| 112 | + |
| 113 | + // Recent version of the extension already expose private properties |
| 114 | + if (isset($a["\x00AMQPQueue\x00name"])) { |
| 115 | + return $a; |
| 116 | + } |
| 117 | + |
| 118 | + $a += [ |
| 119 | + $prefix.'connection' => $c->getConnection(), |
| 120 | + $prefix.'channel' => $c->getChannel(), |
| 121 | + $prefix.'name' => $c->getName(), |
| 122 | + $prefix.'arguments' => $c->getArguments(), |
| 123 | + ]; |
| 124 | + |
| 125 | + return $a; |
| 126 | + } |
| 127 | + |
| 128 | + public static function castExchange(\AMQPExchange $c, array $a, Stub $stub, bool $isNested) |
| 129 | + { |
| 130 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 131 | + |
| 132 | + $a += [ |
| 133 | + $prefix.'flags' => self::extractFlags($c->getFlags()), |
| 134 | + ]; |
| 135 | + |
| 136 | + $type = isset(self::EXCHANGE_TYPES[$c->getType()]) ? new ConstStub(self::EXCHANGE_TYPES[$c->getType()], $c->getType()) : $c->getType(); |
| 137 | + |
| 138 | + // Recent version of the extension already expose private properties |
| 139 | + if (isset($a["\x00AMQPExchange\x00name"])) { |
| 140 | + $a["\x00AMQPExchange\x00type"] = $type; |
| 141 | + |
| 142 | + return $a; |
| 143 | + } |
| 144 | + |
| 145 | + $a += [ |
| 146 | + $prefix.'connection' => $c->getConnection(), |
| 147 | + $prefix.'channel' => $c->getChannel(), |
| 148 | + $prefix.'name' => $c->getName(), |
| 149 | + $prefix.'type' => $type, |
| 150 | + $prefix.'arguments' => $c->getArguments(), |
| 151 | + ]; |
| 152 | + |
| 153 | + return $a; |
| 154 | + } |
| 155 | + |
| 156 | + public static function castEnvelope(\AMQPEnvelope $c, array $a, Stub $stub, bool $isNested, int $filter = 0) |
| 157 | + { |
| 158 | + $prefix = Caster::PREFIX_VIRTUAL; |
| 159 | + |
| 160 | + $deliveryMode = new ConstStub($c->getDeliveryMode().(2 === $c->getDeliveryMode() ? ' (persistent)' : ' (non-persistent)'), $c->getDeliveryMode()); |
| 161 | + |
| 162 | + // Recent version of the extension already expose private properties |
| 163 | + if (isset($a["\x00AMQPEnvelope\x00body"])) { |
| 164 | + $a["\0AMQPEnvelope\0delivery_mode"] = $deliveryMode; |
| 165 | + |
| 166 | + return $a; |
| 167 | + } |
| 168 | + |
| 169 | + if (!($filter & Caster::EXCLUDE_VERBOSE)) { |
| 170 | + $a += [$prefix.'body' => $c->getBody()]; |
| 171 | + } |
| 172 | + |
| 173 | + $a += [ |
| 174 | + $prefix.'delivery_tag' => $c->getDeliveryTag(), |
| 175 | + $prefix.'is_redelivery' => $c->isRedelivery(), |
| 176 | + $prefix.'exchange_name' => $c->getExchangeName(), |
| 177 | + $prefix.'routing_key' => $c->getRoutingKey(), |
| 178 | + $prefix.'content_type' => $c->getContentType(), |
| 179 | + $prefix.'content_encoding' => $c->getContentEncoding(), |
| 180 | + $prefix.'headers' => $c->getHeaders(), |
| 181 | + $prefix.'delivery_mode' => $deliveryMode, |
| 182 | + $prefix.'priority' => $c->getPriority(), |
| 183 | + $prefix.'correlation_id' => $c->getCorrelationId(), |
| 184 | + $prefix.'reply_to' => $c->getReplyTo(), |
| 185 | + $prefix.'expiration' => $c->getExpiration(), |
| 186 | + $prefix.'message_id' => $c->getMessageId(), |
| 187 | + $prefix.'timestamp' => $c->getTimeStamp(), |
| 188 | + $prefix.'type' => $c->getType(), |
| 189 | + $prefix.'user_id' => $c->getUserId(), |
| 190 | + $prefix.'app_id' => $c->getAppId(), |
| 191 | + ]; |
| 192 | + |
| 193 | + return $a; |
| 194 | + } |
| 195 | + |
| 196 | + private static function extractFlags(int $flags): ConstStub |
| 197 | + { |
| 198 | + $flagsArray = []; |
| 199 | + |
| 200 | + foreach (self::FLAGS as $value => $name) { |
| 201 | + if ($flags & $value) { |
| 202 | + $flagsArray[] = $name; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + if (!$flagsArray) { |
| 207 | + $flagsArray = ['AMQP_NOPARAM']; |
| 208 | + } |
| 209 | + |
| 210 | + return new ConstStub(implode('|', $flagsArray), $flags); |
| 211 | + } |
| 212 | +} |
0 commit comments