MonitorContext.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Monitor;
  11. use Predis\ClientInterface;
  12. use Predis\NotSupportedException;
  13. use Predis\Connection\AggregatedConnectionInterface;
  14. /**
  15. * Client-side abstraction of a Redis MONITOR context.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class MonitorContext implements \Iterator
  20. {
  21. private $client;
  22. private $isValid;
  23. private $position;
  24. /**
  25. * @param ClientInterface Client instance used by the context.
  26. */
  27. public function __construct(ClientInterface $client)
  28. {
  29. $this->checkCapabilities($client);
  30. $this->client = $client;
  31. $this->openContext();
  32. }
  33. /**
  34. * Automatically closes the context when PHP's garbage collector kicks in.
  35. */
  36. public function __destruct()
  37. {
  38. $this->closeContext();
  39. }
  40. /**
  41. * Checks if the passed client instance satisfies the required conditions
  42. * needed to initialize a monitor context.
  43. *
  44. * @param ClientInterface Client instance used by the context.
  45. */
  46. private function checkCapabilities(ClientInterface $client)
  47. {
  48. if ($client->getConnection() instanceof AggregatedConnectionInterface) {
  49. throw new NotSupportedException('Cannot initialize a monitor context when using aggregated connections');
  50. }
  51. if ($client->getProfile()->supportsCommand('monitor') === false) {
  52. throw new NotSupportedException('The current profile does not support the MONITOR command');
  53. }
  54. }
  55. /**
  56. * Initializes the context and sends the MONITOR command to the server.
  57. */
  58. protected function openContext()
  59. {
  60. $this->isValid = true;
  61. $monitor = $this->client->createCommand('monitor');
  62. $this->client->executeCommand($monitor);
  63. }
  64. /**
  65. * Closes the context. Internally this is done by disconnecting from server
  66. * since there is no way to terminate the stream initialized by MONITOR.
  67. */
  68. public function closeContext()
  69. {
  70. $this->client->disconnect();
  71. $this->isValid = false;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function rewind()
  77. {
  78. // NOOP
  79. }
  80. /**
  81. * Returns the last message payload retrieved from the server.
  82. *
  83. * @return Object
  84. */
  85. public function current()
  86. {
  87. return $this->getValue();
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function key()
  93. {
  94. return $this->position;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function next()
  100. {
  101. $this->position++;
  102. }
  103. /**
  104. * Checks if the the context is still in a valid state to continue.
  105. *
  106. * @return Boolean
  107. */
  108. public function valid()
  109. {
  110. return $this->isValid;
  111. }
  112. /**
  113. * Waits for a new message from the server generated by MONITOR and
  114. * returns it when available.
  115. *
  116. * @return Object
  117. */
  118. private function getValue()
  119. {
  120. $database = 0;
  121. $client = null;
  122. $event = $this->client->getConnection()->read();
  123. $callback = function ($matches) use (&$database, &$client) {
  124. if (2 === $count = count($matches)) {
  125. // Redis <= 2.4
  126. $database = (int) $matches[1];
  127. }
  128. if (4 === $count) {
  129. // Redis >= 2.6
  130. $database = (int) $matches[2];
  131. $client = $matches[3];
  132. }
  133. return ' ';
  134. };
  135. $event = preg_replace_callback('/ \(db (\d+)\) | \[(\d+) (.*?)\] /', $callback, $event, 1);
  136. @list($timestamp, $command, $arguments) = explode(' ', $event, 3);
  137. return (object) array(
  138. 'timestamp' => (float) $timestamp,
  139. 'database' => $database,
  140. 'client' => $client,
  141. 'command' => substr($command, 1, -1),
  142. 'arguments' => $arguments,
  143. );
  144. }
  145. }