AbstractPubSubContext.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 STATUS_VALID = 1; // 0b0001
  25. const STATUS_SUBSCRIBED = 2; // 0b0010
  26. const STATUS_PSUBSCRIBED = 4; // 0b0100
  27. private $position = null;
  28. private $statusFlags = self::STATUS_VALID;
  29. /**
  30. * Automatically closes the context when PHP's garbage collector kicks in.
  31. */
  32. public function __destruct()
  33. {
  34. $this->closeContext(true);
  35. }
  36. /**
  37. * Checks if the specified flag is valid in the state of the context.
  38. *
  39. * @param int $value Flag.
  40. * @return bool
  41. */
  42. protected function isFlagSet($value)
  43. {
  44. return ($this->statusFlags & $value) === $value;
  45. }
  46. /**
  47. * Subscribes to the specified channels.
  48. *
  49. * @param mixed $channel,... One or more channel names.
  50. */
  51. public function subscribe($channel /*, ... */)
  52. {
  53. $this->writeCommand(self::SUBSCRIBE, func_get_args());
  54. $this->statusFlags |= self::STATUS_SUBSCRIBED;
  55. }
  56. /**
  57. * Unsubscribes from the specified channels.
  58. *
  59. * @param string ... One or more channel names.
  60. */
  61. public function unsubscribe(/* ... */)
  62. {
  63. $this->writeCommand(self::UNSUBSCRIBE, func_get_args());
  64. }
  65. /**
  66. * Subscribes to the specified channels using a pattern.
  67. *
  68. * @param mixed $pattern,... One or more channel name patterns.
  69. */
  70. public function psubscribe($pattern /* ... */)
  71. {
  72. $this->writeCommand(self::PSUBSCRIBE, func_get_args());
  73. $this->statusFlags |= self::STATUS_PSUBSCRIBED;
  74. }
  75. /**
  76. * Unsubscribes from the specified channels using a pattern.
  77. *
  78. * @param string ... One or more channel name patterns.
  79. */
  80. public function punsubscribe(/* ... */)
  81. {
  82. $this->writeCommand(self::PUNSUBSCRIBE, func_get_args());
  83. }
  84. /**
  85. * Closes the context by unsubscribing from all the subscribed channels.
  86. * Optionally, the context can be forcefully closed by dropping the
  87. * underlying connection.
  88. *
  89. * @param bool $force Forcefully close the context by closing the connection.
  90. * @return bool Returns false if there are no pending messages.
  91. */
  92. public function closeContext($force = false)
  93. {
  94. if (!$this->valid()) {
  95. return false;
  96. }
  97. if ($force) {
  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 !$force;
  109. }
  110. /**
  111. * Closes the underlying connection on forced disconnection.
  112. */
  113. abstract protected function disconnect();
  114. /**
  115. * Writes a Redis command on the underlying connection.
  116. *
  117. * @param string $method ID of the command.
  118. * @param array $arguments List of arguments.
  119. */
  120. abstract protected function writeCommand($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 context is still in a valid state to continue.
  157. *
  158. * @return bool
  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 context.
  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. }