AmqpCaster.php 6.5 KB

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