AbstractPubSubContext.php 5.1 KB

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