AbstractConsumer.php 5.3 KB

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