AbstractPubSubContext.php 5.3 KB

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