AmqpCaster.php 6.6 KB

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