PubSubContext.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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;
  11. class PubSubContext implements \Iterator
  12. {
  13. const SUBSCRIBE = 'subscribe';
  14. const UNSUBSCRIBE = 'unsubscribe';
  15. const PSUBSCRIBE = 'psubscribe';
  16. const PUNSUBSCRIBE = 'punsubscribe';
  17. const MESSAGE = 'message';
  18. const PMESSAGE = 'pmessage';
  19. const STATUS_VALID = 0x0001;
  20. const STATUS_SUBSCRIBED = 0x0010;
  21. const STATUS_PSUBSCRIBED = 0x0100;
  22. private $_client;
  23. private $_position;
  24. private $_options;
  25. public function __construct(Client $client, Array $options = null)
  26. {
  27. $this->checkCapabilities($client);
  28. $this->_options = $options ?: array();
  29. $this->_client = $client;
  30. $this->_statusFlags = self::STATUS_VALID;
  31. $this->genericSubscribeInit('subscribe');
  32. $this->genericSubscribeInit('psubscribe');
  33. }
  34. public function __destruct()
  35. {
  36. $this->closeContext();
  37. }
  38. private function checkCapabilities(Client $client)
  39. {
  40. if (Helpers::isCluster($client->getConnection())) {
  41. throw new ClientException(
  42. 'Cannot initialize a PUB/SUB context over a cluster of connections'
  43. );
  44. }
  45. $commands = array('publish', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');
  46. if ($client->getProfile()->supportsCommands($commands) === false) {
  47. throw new ClientException(
  48. 'The current profile does not support PUB/SUB related commands'
  49. );
  50. }
  51. }
  52. private function genericSubscribeInit($subscribeAction)
  53. {
  54. if (isset($this->_options[$subscribeAction])) {
  55. $this->$subscribeAction($this->_options[$subscribeAction]);
  56. }
  57. }
  58. private function isFlagSet($value)
  59. {
  60. return ($this->_statusFlags & $value) === $value;
  61. }
  62. public function subscribe(/* arguments */)
  63. {
  64. $this->writeCommand(self::SUBSCRIBE, func_get_args());
  65. $this->_statusFlags |= self::STATUS_SUBSCRIBED;
  66. }
  67. public function unsubscribe(/* arguments */)
  68. {
  69. $this->writeCommand(self::UNSUBSCRIBE, func_get_args());
  70. }
  71. public function psubscribe(/* arguments */)
  72. {
  73. $this->writeCommand(self::PSUBSCRIBE, func_get_args());
  74. $this->_statusFlags |= self::STATUS_PSUBSCRIBED;
  75. }
  76. public function punsubscribe(/* arguments */)
  77. {
  78. $this->writeCommand(self::PUNSUBSCRIBE, func_get_args());
  79. }
  80. public function closeContext()
  81. {
  82. if ($this->valid()) {
  83. if ($this->isFlagSet(self::STATUS_SUBSCRIBED)) {
  84. $this->unsubscribe();
  85. }
  86. if ($this->isFlagSet(self::STATUS_PSUBSCRIBED)) {
  87. $this->punsubscribe();
  88. }
  89. }
  90. }
  91. private function writeCommand($method, $arguments)
  92. {
  93. $arguments = Helpers::filterArrayArguments($arguments);
  94. $command = $this->_client->createCommand($method, $arguments);
  95. $this->_client->getConnection()->writeCommand($command);
  96. }
  97. public function rewind()
  98. {
  99. // NOOP
  100. }
  101. public function current()
  102. {
  103. return $this->getValue();
  104. }
  105. public function key()
  106. {
  107. return $this->_position;
  108. }
  109. public function next()
  110. {
  111. if ($this->isFlagSet(self::STATUS_VALID)) {
  112. $this->_position++;
  113. }
  114. return $this->_position;
  115. }
  116. public function valid()
  117. {
  118. $isValid = $this->isFlagSet(self::STATUS_VALID);
  119. $subscriptionFlags = self::STATUS_SUBSCRIBED | self::STATUS_PSUBSCRIBED;
  120. $hasSubscriptions = ($this->_statusFlags & $subscriptionFlags) > 0;
  121. return $isValid && $hasSubscriptions;
  122. }
  123. private function invalidate()
  124. {
  125. $this->_statusFlags = 0x0000;
  126. }
  127. private function getValue()
  128. {
  129. $response = $this->_client->getConnection()->read();
  130. switch ($response[0]) {
  131. case self::SUBSCRIBE:
  132. case self::UNSUBSCRIBE:
  133. case self::PSUBSCRIBE:
  134. case self::PUNSUBSCRIBE:
  135. if ($response[2] === 0) {
  136. $this->invalidate();
  137. }
  138. case self::MESSAGE:
  139. return (object) array(
  140. 'kind' => $response[0],
  141. 'channel' => $response[1],
  142. 'payload' => $response[2],
  143. );
  144. case self::PMESSAGE:
  145. return (object) array(
  146. 'kind' => $response[0],
  147. 'pattern' => $response[1],
  148. 'channel' => $response[2],
  149. 'payload' => $response[3],
  150. );
  151. default:
  152. throw new ClientException(
  153. "Received an unknown message type {$response[0]} inside of a pubsub context"
  154. );
  155. }
  156. }
  157. }