AbstractConsumer.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * Base implementation of a PUB/SUB consumer abstraction based on PHP iterators.
  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 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. * @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 $channel,... One or more channel names.
  51. */
  52. public function subscribe($channel /*, ... */)
  53. {
  54. $this->writeRequest(self::SUBSCRIBE, func_get_args());
  55. $this->statusFlags |= self::STATUS_SUBSCRIBED;
  56. }
  57. /**
  58. * Unsubscribes from the specified channels.
  59. *
  60. * @param string ... One or more channel names.
  61. */
  62. public function unsubscribe(/* ... */)
  63. {
  64. $this->writeRequest(self::UNSUBSCRIBE, func_get_args());
  65. }
  66. /**
  67. * Subscribes to the specified channels using a pattern.
  68. *
  69. * @param mixed $pattern,... One or more channel name patterns.
  70. */
  71. public function psubscribe($pattern /* ... */)
  72. {
  73. $this->writeRequest(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 string ... One or more channel name patterns.
  80. */
  81. public function punsubscribe(/* ... */)
  82. {
  83. $this->writeRequest(self::PUNSUBSCRIBE, func_get_args());
  84. }
  85. /**
  86. * Closes the context by unsubscribing from all the subscribed channels. The
  87. * context can be forcefully closed by dropping the underlying connection.
  88. *
  89. * @param bool $drop Indicates if the context should be closed by dropping the connection.
  90. * @return bool Returns false when there are no pending messages.
  91. */
  92. public function stop($drop = false)
  93. {
  94. if (!$this->valid()) {
  95. return false;
  96. }
  97. if ($drop) {
  98. $this->invalidate();
  99. $this->disconnect();
  100. } else {
  101. if ($this->isFlagSet(self::STATUS_SUBSCRIBED)) {
  102. $this->unsubscribe();
  103. }
  104. if ($this->isFlagSet(self::STATUS_PSUBSCRIBED)) {
  105. $this->punsubscribe();
  106. }
  107. }
  108. return !$drop;
  109. }
  110. /**
  111. * Closes the underlying connection when forcing a disconnection.
  112. */
  113. abstract protected function disconnect();
  114. /**
  115. * Writes a Redis command on the underlying connection.
  116. *
  117. * @param string $method Command ID.
  118. * @param array $arguments Arguments for the command.
  119. */
  120. abstract protected function writeRequest($method, $arguments);
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function rewind()
  125. {
  126. // NOOP
  127. }
  128. /**
  129. * Returns the last message payload retrieved from the server and generated
  130. * by one of the active subscriptions.
  131. *
  132. * @return array
  133. */
  134. public function current()
  135. {
  136. return $this->getValue();
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function key()
  142. {
  143. return $this->position;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function next()
  149. {
  150. if ($this->valid()) {
  151. $this->position++;
  152. }
  153. return $this->position;
  154. }
  155. /**
  156. * Checks if the the consumer is still in a valid state to continue.
  157. *
  158. * @return Boolean
  159. */
  160. public function valid()
  161. {
  162. $isValid = $this->isFlagSet(self::STATUS_VALID);
  163. $subscriptionFlags = self::STATUS_SUBSCRIBED | self::STATUS_PSUBSCRIBED;
  164. $hasSubscriptions = ($this->statusFlags & $subscriptionFlags) > 0;
  165. return $isValid && $hasSubscriptions;
  166. }
  167. /**
  168. * Resets the state of the consumer.
  169. */
  170. protected function invalidate()
  171. {
  172. $this->statusFlags = 0; // 0b0000;
  173. }
  174. /**
  175. * Waits for a new message from the server generated by one of the active
  176. * subscriptions and returns it when available.
  177. *
  178. * @return array
  179. */
  180. abstract protected function getValue();
  181. }