AbstractPubSubContext.php 5.1 KB

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