Browse Source

Implement Predis\MonitorContext as an abstraction for the MONITOR command.

Daniele Alessandri 14 years ago
parent
commit
c07c610a0d

+ 4 - 0
lib/Predis/Client.php

@@ -214,4 +214,8 @@ class Client {
     public function pubSubContext(Array $options = null) {
         return new PubSubContext($this, $options);
     }
+
+    public function monitor() {
+        return new MonitorContext($this);
+    }
 }

+ 71 - 0
lib/Predis/MonitorContext.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace Predis;
+
+class MonitorContext implements \Iterator {
+    private $_client, $_isValid, $_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 ClientException(
+                'Cannot initialize a monitor context over a cluster of connections'
+            );
+        }
+        if ($client->getProfile()->supportsCommand('monitor') === false) {
+            throw new ClientException(
+                '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() {
+        // NOOP
+    }
+
+    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() {
+        $event = $this->_client->getConnection()->read();
+        @list($timestamp, $command, $arguments) = split(' ', $event, 3);
+        return (object) array(
+            'timestamp' => (float) $timestamp,
+            'command'   => substr($command, 1, -1),
+            'arguments' => $arguments ?: '',
+        );
+    }
+}

+ 1 - 0
lib/Predis/Profiles/ServerVersion12.php

@@ -94,6 +94,7 @@ class ServerVersion12 extends ServerProfile {
             /* remote server control commands */
             'info'                      => '\Predis\Commands\Info',
             'slaveof'                   => '\Predis\Commands\SlaveOf',
+            'monitor'                   => '\Predis\Commands\Monitor',
 
             /* persistence control commands */
             'save'                      => '\Predis\Commands\Save',

+ 1 - 0
lib/Predis/Profiles/ServerVersion20.php

@@ -94,6 +94,7 @@ class ServerVersion20 extends ServerProfile {
             /* remote server control commands */
             'info'                      => '\Predis\Commands\Info',
             'slaveof'                   => '\Predis\Commands\SlaveOf',
+            'monitor'                   => '\Predis\Commands\Monitor',
 
             /* persistence control commands */
             'save'                      => '\Predis\Commands\Save',

+ 1 - 0
lib/Predis/Profiles/ServerVersion22.php

@@ -94,6 +94,7 @@ class ServerVersion22 extends ServerProfile {
             /* remote server control commands */
             'info'                      => '\Predis\Commands\Info',
             'slaveof'                   => '\Predis\Commands\SlaveOf',
+            'monitor'                   => '\Predis\Commands\Monitor',
 
             /* persistence control commands */
             'save'                      => '\Predis\Commands\Save',