123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace Predis;
- class MonitorContext implements \Iterator
- {
- private $client;
- private $isValid;
- private $position;
-
- public function __construct(Client $client)
- {
- $this->checkCapabilities($client);
- $this->client = $client;
- $this->openContext();
- }
-
- public function __destruct()
- {
- $this->closeContext();
- }
-
- private function checkCapabilities(Client $client)
- {
- if (Helpers::isCluster($client->getConnection())) {
- throw new NotSupportedException('Cannot initialize a monitor context over a cluster of connections');
- }
- if ($client->getProfile()->supportsCommand('monitor') === false) {
- throw new NotSupportedException('The current profile does not support the MONITOR command');
- }
- }
-
- protected function openContext()
- {
- $this->isValid = true;
- $monitor = $this->client->createCommand('monitor');
- $this->client->executeCommand($monitor);
- }
-
- public function closeContext()
- {
- $this->client->disconnect();
- $this->isValid = false;
- }
-
- public function rewind()
- {
-
- }
-
- public function current()
- {
- return $this->getValue();
- }
-
- public function key()
- {
- return $this->position;
- }
-
- public function next()
- {
- $this->position++;
- }
-
- public function valid()
- {
- return $this->isValid;
- }
-
- private function getValue()
- {
- $database = 0;
- $event = $this->client->getConnection()->read();
- $callback = function($matches) use (&$database) {
- if (isset($matches[1])) {
- $database = (int) $matches[1];
- }
- return ' ';
- };
- $event = preg_replace_callback('/ \(db (\d+)\) /', $callback, $event, 1);
- @list($timestamp, $command, $arguments) = split(' ', $event, 3);
- return (object) array(
- 'timestamp' => (float) $timestamp,
- 'database' => $database,
- 'command' => substr($command, 1, -1),
- 'arguments' => $arguments,
- );
- }
- }
|