AbstractConsumer.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Iterator;
  12. /**
  13. * Client-side abstraction of a Publish / Subscribe context.
  14. *
  15. * @author Daniele Alessandri <suppakilla@gmail.com>
  16. */
  17. abstract class AbstractConsumer implements Iterator
  18. {
  19. const SUBSCRIBE = 'subscribe';
  20. const UNSUBSCRIBE = 'unsubscribe';
  21. const PSUBSCRIBE = 'psubscribe';
  22. const PUNSUBSCRIBE = 'punsubscribe';
  23. const MESSAGE = 'message';
  24. const PMESSAGE = 'pmessage';
  25. const STATUS_VALID = 1; // 0b0001
  26. const STATUS_SUBSCRIBED = 2; // 0b0010
  27. const STATUS_PSUBSCRIBED = 4; // 0b0100
  28. private $position = null;
  29. private $statusFlags = self::STATUS_VALID;
  30. /**
  31. * Automatically closes the context when PHP's garbage collector kicks in.
  32. */
  33. public function __destruct()
  34. {
  35. $this->stop(true);
  36. }
  37. /**
  38. * Checks if the specified flag is valid in the state of the context.
  39. *
  40. * @param int $value Flag.
  41. * @return Boolean
  42. */
  43. protected function isFlagSet($value)
  44. {
  45. return ($this->statusFlags & $value) === $value;
  46. }
  47. /**
  48. * Subscribes to the specified channels.
  49. *
  50. * @param mixed $arg,... One or more channel names.
  51. */
  52. public function subscribe(/* arguments */)
  53. {
  54. $this->writeCommand(self::SUBSCRIBE, func_get_args());
  55. $this->statusFlags |= self::STATUS_SUBSCRIBED;
  56. }
  57. /**
  58. * Unsubscribes from the specified channels.
  59. *
  60. * @param mixed $arg,... One or more channel names.
  61. */
  62. public function unsubscribe(/* arguments */)
  63. {
  64. $this->writeCommand(self::UNSUBSCRIBE, func_get_args());
  65. }
  66. /**
  67. * Subscribes to the specified channels using a pattern.
  68. *
  69. * @param mixed $arg,... One or more channel name patterns.
  70. */
  71. public function psubscribe(/* arguments */)
  72. {
  73. $this->writeCommand(self::PSUBSCRIBE, func_get_args());
  74. $this->statusFlags |= self::STATUS_PSUBSCRIBED;
  75. }
  76. /**
  77. * Unsubscribes from the specified channels using a pattern.
  78. *
  79. * @param mixed $arg,... One or more channel name patterns.
  80. */
  81. public function punsubscribe(/* arguments */)
  82. {
  83. $this->writeCommand(self::PUNSUBSCRIBE, func_get_args());
  84. }
  85. /**
  86. * Closes the context by unsubscribing from all the subscribed channels.
  87. * Optionally, the context can be forcefully closed by dropping the
  88. * underlying connection.
  89. *
  90. * @param bool $drop Forcefully close the context by closing the connection.
  91. * @return bool Returns false if there are no pending messages.
  92. */
  93. public function stop($drop = false)
  94. {
  95. if (!$this->valid()) {
  96. return false;
  97. }
  98. if ($drop) {
  99. $this->invalidate();
  100. $this->disconnect();
  101. } else {
  102. if ($this->isFlagSet(self::STATUS_SUBSCRIBED)) {
  103. $this->unsubscribe();
  104. }
  105. if ($this->isFlagSet(self::STATUS_PSUBSCRIBED)) {
  106. $this->punsubscribe();
  107. }
  108. }
  109. return !$drop;
  110. }
  111. /**
  112. * Closes the underlying connection on forced disconnection.
  113. */
  114. protected abstract function disconnect();
  115. /**
  116. * Writes a Redis command on the underlying connection.
  117. *
  118. * @param string $method ID of the command.
  119. * @param array $arguments List of arguments.
  120. */
  121. protected abstract function writeCommand($method, $arguments);
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function rewind()
  126. {
  127. // NOOP
  128. }
  129. /**
  130. * Returns the last message payload retrieved from the server and generated
  131. * by one of the active subscriptions.
  132. *
  133. * @return array
  134. */
  135. public function current()
  136. {
  137. return $this->getValue();
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function key()
  143. {
  144. return $this->position;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function next()
  150. {
  151. if ($this->valid()) {
  152. $this->position++;
  153. }
  154. return $this->position;
  155. }
  156. /**
  157. * Checks if the the context is still in a valid state to continue.
  158. *
  159. * @return Boolean
  160. */
  161. public function valid()
  162. {
  163. $isValid = $this->isFlagSet(self::STATUS_VALID);
  164. $subscriptionFlags = self::STATUS_SUBSCRIBED | self::STATUS_PSUBSCRIBED;
  165. $hasSubscriptions = ($this->statusFlags & $subscriptionFlags) > 0;
  166. return $isValid && $hasSubscriptions;
  167. }
  168. /**
  169. * Resets the state of the context.
  170. */
  171. protected function invalidate()
  172. {
  173. $this->statusFlags = 0; // 0b0000;
  174. }
  175. /**
  176. * Waits for a new message from the server generated by one of the active
  177. * subscriptions and returns it when available.
  178. *
  179. * @return array
  180. */
  181. protected abstract function getValue();
  182. }