PubSubContext.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Command\AbstractCommand as Command;
  14. use Predis\NotSupportedException;
  15. use Predis\Connection\AggregatedConnectionInterface;
  16. /**
  17. * Client-side abstraction of a Publish / Subscribe context.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class PubSubContext extends AbstractPubSubContext
  22. {
  23. private $client;
  24. private $options;
  25. /**
  26. * @param ClientInterface $client Client instance used by the context.
  27. * @param array $options Options for the context initialization.
  28. */
  29. public function __construct(ClientInterface $client, Array $options = null)
  30. {
  31. $this->checkCapabilities($client);
  32. $this->options = $options ?: array();
  33. $this->client = $client;
  34. $this->genericSubscribeInit('subscribe');
  35. $this->genericSubscribeInit('psubscribe');
  36. }
  37. /**
  38. * Returns the underlying client instance used by the pub/sub iterator.
  39. *
  40. * @return ClientInterface
  41. */
  42. public function getClient()
  43. {
  44. return $this->client;
  45. }
  46. /**
  47. * Checks if the passed client instance satisfies the required conditions
  48. * needed to initialize a Publish / Subscribe context.
  49. *
  50. * @param ClientInterface $client Client instance used by the context.
  51. */
  52. private function checkCapabilities(ClientInterface $client)
  53. {
  54. if ($client->getConnection() instanceof AggregatedConnectionInterface) {
  55. throw new NotSupportedException('Cannot initialize a PUB/SUB context when using aggregated connections');
  56. }
  57. $commands = array('publish', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');
  58. if ($client->getProfile()->supportsCommands($commands) === false) {
  59. throw new NotSupportedException('The current profile does not support PUB/SUB related commands');
  60. }
  61. }
  62. /**
  63. * This method shares the logic to handle both SUBSCRIBE and PSUBSCRIBE.
  64. *
  65. * @param string $subscribeAction Type of subscription.
  66. */
  67. private function genericSubscribeInit($subscribeAction)
  68. {
  69. if (isset($this->options[$subscribeAction])) {
  70. $this->$subscribeAction($this->options[$subscribeAction]);
  71. }
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. protected function writeCommand($method, $arguments)
  77. {
  78. $arguments = Command::normalizeArguments($arguments);
  79. $command = $this->client->createCommand($method, $arguments);
  80. $this->client->getConnection()->writeCommand($command);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function disconnect()
  86. {
  87. $this->client->disconnect();
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. protected function getValue()
  93. {
  94. $response = $this->client->getConnection()->read();
  95. switch ($response[0]) {
  96. case self::SUBSCRIBE:
  97. case self::UNSUBSCRIBE:
  98. case self::PSUBSCRIBE:
  99. case self::PUNSUBSCRIBE:
  100. if ($response[2] === 0) {
  101. $this->invalidate();
  102. }
  103. // The missing break here is intentional as we must process
  104. // subscriptions and unsubscriptions as standard messages.
  105. case self::MESSAGE:
  106. return (object) array(
  107. 'kind' => $response[0],
  108. 'channel' => $response[1],
  109. 'payload' => $response[2],
  110. );
  111. case self::PMESSAGE:
  112. return (object) array(
  113. 'kind' => $response[0],
  114. 'pattern' => $response[1],
  115. 'channel' => $response[2],
  116. 'payload' => $response[3],
  117. );
  118. case self::PONG:
  119. return (object) array(
  120. 'kind' => $response[0],
  121. 'payload' => $response[1],
  122. );
  123. default:
  124. $message = "Received an unknown message type {$response[0]} inside of a pubsub context";
  125. throw new ClientException($message);
  126. }
  127. }
  128. }