diff --git a/lib/Predis/Monitor/MonitorContext.php b/lib/Predis/Monitor/MonitorContext.php index dba2bfa3..797c6826 100644 --- a/lib/Predis/Monitor/MonitorContext.php +++ b/lib/Predis/Monitor/MonitorContext.php @@ -136,21 +136,29 @@ class MonitorContext implements \Iterator private function getValue() { $database = 0; + $client = null; $event = $this->client->getConnection()->read(); - $callback = function($matches) use (&$database) { - if (isset($matches[1])) { + $callback = function($matches) use (&$database, &$client) { + if (2 === $count = count($matches)) { + // Redis <= 2.4 $database = (int) $matches[1]; } + if (4 === $count) { + // Redis >= 2.6 + $database = (int) $matches[2]; + $client = $matches[3]; + } return ' '; }; - $event = preg_replace_callback('/ \(db (\d+)\) /', $callback, $event, 1); - @list($timestamp, $command, $arguments) = split(' ', $event, 3); + $event = preg_replace_callback('/ \(db (\d+)\) | \[(\d+) (.*?)\] /', $callback, $event, 1); + @list($timestamp, $command, $arguments) = explode(' ', $event, 3); return (object) array( 'timestamp' => (float) $timestamp, 'database' => $database, + 'client' => $client, 'command' => substr($command, 1, -1), 'arguments' => $arguments, ); diff --git a/tests/Predis/Command/ServerMonitorTest.php b/tests/Predis/Command/ServerMonitorTest.php index 5ee7bd98..cab231c9 100644 --- a/tests/Predis/Command/ServerMonitorTest.php +++ b/tests/Predis/Command/ServerMonitorTest.php @@ -64,6 +64,11 @@ class ServerMonitorTest extends CommandTestCase $command = $this->getCommand(); $this->assertTrue($connection->executeCommand($command)); - $this->assertRegExp('/\d+.\d+(\s?\(db \d+\))? "MONITOR"/', $connection->read()); + + // NOTE: Starting with 2.6 Redis does not return the "MONITOR" message after + // +OK to the client that issued the MONITOR command. + if (version_compare($this->getProfile()->getVersion(), '2.4', '<=')) { + $this->assertRegExp('/\d+.\d+(\s?\(db \d+\))? "MONITOR"/', $connection->read()); + } } } diff --git a/tests/Predis/Monitor/MonitorContextTest.php b/tests/Predis/Monitor/MonitorContextTest.php index a32e1660..c9ae7c69 100644 --- a/tests/Predis/Monitor/MonitorContextTest.php +++ b/tests/Predis/Monitor/MonitorContextTest.php @@ -106,7 +106,7 @@ class MonitorContextTest extends StandardTestCase /** * @group disconnected */ - public function testCurrentReadsMessageFromConnection() + public function testReadsMessageFromConnectionToRedis24() { $message = '1323367530.939137 (db 15) "MONITOR"'; @@ -121,6 +121,30 @@ class MonitorContextTest extends StandardTestCase $payload = $monitor->current(); $this->assertSame(1323367530, (int) $payload->timestamp); $this->assertSame(15, $payload->database); + $this->assertNull($payload->client); + $this->assertSame('MONITOR', $payload->command); + $this->assertNull($payload->arguments); + } + + /** + * @group disconnected + */ + public function testReadsMessageFromConnectionToRedis26() + { + $message = '1323367530.939137 [15 127.0.0.1:37265] "MONITOR"'; + + $connection = $this->getMock('Predis\Connection\SingleConnectionInterface'); + $connection->expects($this->once()) + ->method('read') + ->will($this->returnValue($message)); + + $client = new Client($connection); + $monitor = new MonitorContext($client); + + $payload = $monitor->current(); + $this->assertSame(1323367530, (int) $payload->timestamp); + $this->assertSame(15, $payload->database); + $this->assertSame('127.0.0.1:37265', $payload->client); $this->assertSame('MONITOR', $payload->command); $this->assertNull($payload->arguments); }