AmqpCaster.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // BC layer in the ampq lib
  45. if (method_exists($c, 'getReadTimeout')) {
  46. $timeout = $c->getReadTimeout();
  47. } else {
  48. $timeout = $c->getTimeout();
  49. }
  50. $a += array(
  51. $prefix.'isConnected' => $c->isConnected(),
  52. $prefix.'login' => $c->getLogin(),
  53. $prefix.'password' => $c->getPassword(),
  54. $prefix.'host' => $c->getHost(),
  55. $prefix.'port' => $c->getPort(),
  56. $prefix.'vhost' => $c->getVhost(),
  57. $prefix.'readTimeout' => $timeout,
  58. );
  59. return $a;
  60. }
  61. public static function castChannel(\AMQPChannel $c, array $a, Stub $stub, $isNested)
  62. {
  63. $prefix = Caster::PREFIX_VIRTUAL;
  64. $a += array(
  65. $prefix.'isConnected' => $c->isConnected(),
  66. $prefix.'channelId' => $c->getChannelId(),
  67. $prefix.'prefetchSize' => $c->getPrefetchSize(),
  68. $prefix.'prefetchCount' => $c->getPrefetchCount(),
  69. $prefix.'connection' => $c->getConnection(),
  70. );
  71. return $a;
  72. }
  73. public static function castQueue(\AMQPQueue $c, array $a, Stub $stub, $isNested)
  74. {
  75. $prefix = Caster::PREFIX_VIRTUAL;
  76. $a += array(
  77. $prefix.'name' => $c->getName(),
  78. $prefix.'flags' => self::extractFlags($c->getFlags()),
  79. $prefix.'arguments' => $c->getArguments(),
  80. $prefix.'connection' => $c->getConnection(),
  81. $prefix.'channel' => $c->getChannel(),
  82. );
  83. return $a;
  84. }
  85. public static function castExchange(\AMQPExchange $c, array $a, Stub $stub, $isNested)
  86. {
  87. $prefix = Caster::PREFIX_VIRTUAL;
  88. $a += array(
  89. $prefix.'name' => $c->getName(),
  90. $prefix.'flags' => self::extractFlags($c->getFlags()),
  91. $prefix.'type' => isset(self::$exchangeTypes[$c->getType()]) ? new ConstStub(self::$exchangeTypes[$c->getType()], $c->getType()) : $c->getType(),
  92. $prefix.'arguments' => $c->getArguments(),
  93. $prefix.'channel' => $c->getChannel(),
  94. $prefix.'connection' => $c->getConnection(),
  95. );
  96. return $a;
  97. }
  98. public static function castEnvelope(\AMQPEnvelope $c, array $a, Stub $stub, $isNested, $filter = 0)
  99. {
  100. $prefix = Caster::PREFIX_VIRTUAL;
  101. if (!($filter & Caster::EXCLUDE_VERBOSE)) {
  102. $a += array($prefix.'body' => $c->getBody());
  103. }
  104. $a += array(
  105. $prefix.'routingKey' => $c->getRoutingKey(),
  106. $prefix.'deliveryTag' => $c->getDeliveryTag(),
  107. $prefix.'deliveryMode' => new ConstStub($c->getDeliveryMode().(2 === $c->getDeliveryMode() ? ' (persistent)' : ' (non-persistent)'), $c->getDeliveryMode()),
  108. $prefix.'exchangeName' => $c->getExchangeName(),
  109. $prefix.'isRedelivery' => $c->isRedelivery(),
  110. $prefix.'contentType' => $c->getContentType(),
  111. $prefix.'contentEncoding' => $c->getContentEncoding(),
  112. $prefix.'type' => $c->getType(),
  113. $prefix.'timestamp' => $c->getTimestamp(),
  114. $prefix.'priority' => $c->getPriority(),
  115. $prefix.'expiration' => $c->getExpiration(),
  116. $prefix.'userId' => $c->getUserId(),
  117. $prefix.'appId' => $c->getAppId(),
  118. $prefix.'messageId' => $c->getMessageId(),
  119. $prefix.'replyTo' => $c->getReplyTo(),
  120. $prefix.'correlationId' => $c->getCorrelationId(),
  121. $prefix.'headers' => $c->getHeaders(),
  122. );
  123. return $a;
  124. }
  125. private static function extractFlags($flags)
  126. {
  127. $flagsArray = array();
  128. foreach (self::$flags as $value => $name) {
  129. if ($flags & $value) {
  130. $flagsArray[] = $name;
  131. }
  132. }
  133. if (!$flagsArray) {
  134. $flagsArray = array('AMQP_NOPARAM');
  135. }
  136. return new ConstStub(implode('|', $flagsArray), $flags);
  137. }
  138. }