AbstractPubSubContext.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. } else {
  105. if ($this->isFlagSet(self::STATUS_SUBSCRIBED)) {
  106. $this->unsubscribe();
  107. }
  108. if ($this->isFlagSet(self::STATUS_PSUBSCRIBED)) {
  109. $this->punsubscribe();
  110. }
  111. }
  112. return !$force;
  113. }
  114. /**
  115. * Closes the underlying connection on forced disconnection.
  116. */
  117. protected abstract function disconnect();
  118. /**
  119. * Writes a Redis command on the underlying connection.
  120. *
  121. * @param string $method ID of the command.
  122. * @param array $arguments List of arguments.
  123. */
  124. protected abstract function writeCommand($method, $arguments);
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function rewind()
  129. {
  130. // NOOP
  131. }
  132. /**
  133. * Returns the last message payload retrieved from the server and generated
  134. * by one of the active subscriptions.
  135. *
  136. * @return array
  137. */
  138. public function current()
  139. {
  140. return $this->getValue();
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function key()
  146. {
  147. return $this->position;
  148. }
  149. /**
  150. * {@inheritdoc}
  151. */
  152. public function next()
  153. {
  154. if ($this->valid()) {
  155. $this->position++;
  156. }
  157. return $this->position;
  158. }
  159. /**
  160. * Checks if the the context is still in a valid state to continue.
  161. *
  162. * @return Boolean
  163. */
  164. public function valid()
  165. {
  166. $isValid = $this->isFlagSet(self::STATUS_VALID);
  167. $subscriptionFlags = self::STATUS_SUBSCRIBED | self::STATUS_PSUBSCRIBED;
  168. $hasSubscriptions = ($this->statusFlags & $subscriptionFlags) > 0;
  169. return $isValid && $hasSubscriptions;
  170. }
  171. /**
  172. * Resets the state of the context.
  173. */
  174. protected function invalidate()
  175. {
  176. $this->statusFlags = 0; // 0b0000;
  177. }
  178. /**
  179. * Waits for a new message from the server generated by one of the active
  180. * subscriptions and returns it when available.
  181. *
  182. * @return array
  183. */
  184. protected abstract function getValue();
  185. }