Nessuna descrizione

AmqpCaster.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 since Symfony 4.4
  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, $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, $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, $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, $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, $isNested, $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. }