You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.5 KiB

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