Consumer.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.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 Predis\PubSub;
  11. use Predis\ClientException;
  12. use Predis\ClientInterface;
  13. use Predis\Command\Command;
  14. use Predis\NotSupportedException;
  15. use Predis\Connection\AggregateConnectionInterface;
  16. /**
  17. * PUB/SUB consumer abstraction.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class Consumer extends AbstractConsumer
  22. {
  23. private $client;
  24. private $options;
  25. /**
  26. * @param ClientInterface $client Client instance used by the consumer.
  27. * @param array $options Options for the consumer initialization.
  28. */
  29. public function __construct(ClientInterface $client, array $options = null)
  30. {
  31. $this->checkCapabilities($client);
  32. $this->options = $options ?: array();
  33. $this->client = $client;
  34. $this->genericSubscribeInit('subscribe');
  35. $this->genericSubscribeInit('psubscribe');
  36. }
  37. /**
  38. * Returns the underlying client instance used by the pub/sub iterator.
  39. *
  40. * @return ClientInterface
  41. */
  42. public function getClient()
  43. {
  44. return $this->client;
  45. }
  46. /**
  47. * Checks if the client instance satisfies the required conditions needed to
  48. * initialize a PUB/SUB consumer.
  49. *
  50. * @param ClientInterface $client Client instance used by the consumer.
  51. */
  52. private function checkCapabilities(ClientInterface $client)
  53. {
  54. if ($client->getConnection() instanceof AggregateConnectionInterface) {
  55. throw new NotSupportedException(
  56. 'Cannot initialize a PUB/SUB consumer when using aggregate connections'
  57. );
  58. }
  59. $commands = array('publish', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');
  60. if ($client->getProfile()->supportsCommands($commands) === false) {
  61. throw new NotSupportedException(
  62. 'The current profile does not support PUB/SUB related commands'
  63. );
  64. }
  65. }
  66. /**
  67. * This method shares the logic to handle both SUBSCRIBE and PSUBSCRIBE.
  68. *
  69. * @param string $subscribeAction Type of subscription.
  70. */
  71. private function genericSubscribeInit($subscribeAction)
  72. {
  73. if (isset($this->options[$subscribeAction])) {
  74. $this->$subscribeAction($this->options[$subscribeAction]);
  75. }
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function writeRequest($method, $arguments)
  81. {
  82. $arguments = Command::normalizeArguments($arguments);
  83. $command = $this->client->createCommand($method, $arguments);
  84. $this->client->getConnection()->writeRequest($command);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function disconnect()
  90. {
  91. $this->client->disconnect();
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function getValue()
  97. {
  98. $response = $this->client->getConnection()->read();
  99. switch ($response[0]) {
  100. case self::SUBSCRIBE:
  101. case self::UNSUBSCRIBE:
  102. case self::PSUBSCRIBE:
  103. case self::PUNSUBSCRIBE:
  104. if ($response[2] === 0) {
  105. $this->invalidate();
  106. }
  107. case self::MESSAGE:
  108. return (object) array(
  109. 'kind' => $response[0],
  110. 'channel' => $response[1],
  111. 'payload' => $response[2],
  112. );
  113. case self::PMESSAGE:
  114. return (object) array(
  115. 'kind' => $response[0],
  116. 'pattern' => $response[1],
  117. 'channel' => $response[2],
  118. 'payload' => $response[3],
  119. );
  120. default:
  121. throw new ClientException(
  122. "Received an unknown message type {$response[0]} inside PUB/SUB"
  123. );
  124. }
  125. }
  126. }