PubSubContext.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 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 instance used by the context.
  27. * @param array 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. * Checks if the passed client instance satisfies the required conditions
  39. * needed to initialize a Publish / Subscribe context.
  40. *
  41. * @param ClientInterface Client instance used by the context.
  42. */
  43. private function checkCapabilities(ClientInterface $client)
  44. {
  45. if ($client->getConnection() instanceof AggregatedConnectionInterface) {
  46. throw new NotSupportedException('Cannot initialize a PUB/SUB context when using aggregated connections');
  47. }
  48. $commands = array('publish', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');
  49. if ($client->getProfile()->supportsCommands($commands) === false) {
  50. throw new NotSupportedException('The current profile does not support PUB/SUB related commands');
  51. }
  52. }
  53. /**
  54. * This method shares the logic to handle both SUBSCRIBE and PSUBSCRIBE.
  55. *
  56. * @param string $subscribeAction Type of subscription.
  57. */
  58. private function genericSubscribeInit($subscribeAction)
  59. {
  60. if (isset($this->options[$subscribeAction])) {
  61. $this->$subscribeAction($this->options[$subscribeAction]);
  62. }
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function writeCommand($method, $arguments)
  68. {
  69. $arguments = Helpers::filterArrayArguments($arguments);
  70. $command = $this->client->createCommand($method, $arguments);
  71. $this->client->getConnection()->writeCommand($command);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. protected function disconnect()
  77. {
  78. $this->client->disconnect();
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. protected function getValue()
  84. {
  85. $response = $this->client->getConnection()->read();
  86. switch ($response[0]) {
  87. case self::SUBSCRIBE:
  88. case self::UNSUBSCRIBE:
  89. case self::PSUBSCRIBE:
  90. case self::PUNSUBSCRIBE:
  91. if ($response[2] === 0) {
  92. $this->invalidate();
  93. }
  94. case self::MESSAGE:
  95. return (object) array(
  96. 'kind' => $response[0],
  97. 'channel' => $response[1],
  98. 'payload' => $response[2],
  99. );
  100. case self::PMESSAGE:
  101. return (object) array(
  102. 'kind' => $response[0],
  103. 'pattern' => $response[1],
  104. 'channel' => $response[2],
  105. 'payload' => $response[3],
  106. );
  107. default:
  108. $message = "Received an unknown message type {$response[0]} inside of a pubsub context";
  109. throw new ClientException($message);
  110. }
  111. }
  112. }