From 9d2cb975eb7333105d5a9d0e5086a38f9928b362 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sat, 16 Nov 2013 15:42:56 +0100 Subject: [PATCH] Rename classes in Predis\Monitor namespace. --- ...MonitorContext.php => MonitorConsumer.php} | 8 ++-- lib/Predis/Client.php | 8 ++-- .../{MonitorContext.php => Consumer.php} | 45 +++++++++++-------- tests/Predis/ClientTest.php | 4 +- ...onitorContextTest.php => ConsumerTest.php} | 35 ++++++++------- 5 files changed, 54 insertions(+), 46 deletions(-) rename examples/{MonitorContext.php => MonitorConsumer.php} (89%) rename lib/Predis/Monitor/{MonitorContext.php => Consumer.php} (77%) rename tests/Predis/Monitor/{MonitorContextTest.php => ConsumerTest.php} (86%) diff --git a/examples/MonitorContext.php b/examples/MonitorConsumer.php similarity index 89% rename from examples/MonitorContext.php rename to examples/MonitorConsumer.php index e1cad742..6857c9c9 100644 --- a/examples/MonitorContext.php +++ b/examples/MonitorConsumer.php @@ -11,7 +11,7 @@ require 'SharedConfigurations.php'; -// This is a basic example on how to use the Predis\MonitorContext class. +// This is a basic example on how to use the Predis\Monitor\Consumer class. // You can use redis-cli to send commands to the same Redis instance your client is // connected to, and then type "ECHO QUIT_MONITOR" in redis-cli when you want to // exit the monitor loop and terminate this script in a graceful way. @@ -25,11 +25,11 @@ $timestamp = new DateTime(); foreach (($monitor = $client->monitor()) as $event) { $timestamp->setTimestamp((int) $event->timestamp); - // If we notice a ECHO command with the message QUIT_MONITOR, we close the - // monitor context and then break the loop. + // If we notice a ECHO command with the message QUIT_MONITOR, we stop the + // monitor consumer and then break the loop. if ($event->command === 'ECHO' && $event->arguments === '"QUIT_MONITOR"') { echo "Exiting the monitor loop...\n"; - $monitor->closeContext(); + $monitor->stop(); break; } diff --git a/lib/Predis/Client.php b/lib/Predis/Client.php index 3a88f7cb..db7ff131 100644 --- a/lib/Predis/Client.php +++ b/lib/Predis/Client.php @@ -20,7 +20,7 @@ use Predis\Configuration\OptionsInterface; use Predis\Connection\AggregatedConnectionInterface; use Predis\Connection\ConnectionInterface; use Predis\Connection\ConnectionParametersInterface; -use Predis\Monitor\MonitorContext; +use Predis\Monitor; use Predis\Pipeline\PipelineContext; use Predis\Profile\ServerProfile; use Predis\PubSub; @@ -451,12 +451,12 @@ class Client implements ClientInterface } /** - * Creates a new monitor context and returns it. + * Creates a new monitor consumer and returns it. * - * @return MonitorContext + * @return Monitor\Consumer */ public function monitor() { - return new MonitorContext($this); + return new Monitor\Consumer($this); } } diff --git a/lib/Predis/Monitor/MonitorContext.php b/lib/Predis/Monitor/Consumer.php similarity index 77% rename from lib/Predis/Monitor/MonitorContext.php rename to lib/Predis/Monitor/Consumer.php index a053b0b2..2d834c30 100644 --- a/lib/Predis/Monitor/MonitorContext.php +++ b/lib/Predis/Monitor/Consumer.php @@ -11,73 +11,80 @@ namespace Predis\Monitor; +use Iterator; use Predis\ClientInterface; use Predis\NotSupportedException; use Predis\Connection\AggregatedConnectionInterface; /** - * Client-side abstraction of a Redis MONITOR context. + * Redis MONITOR consumer. * * @author Daniele Alessandri */ -class MonitorContext implements \Iterator +class Consumer implements Iterator { private $client; - private $isValid; + private $valid; private $position; /** - * @param ClientInterface $client Client instance used by the context. + * @param ClientInterface $client Client instance used by the consumer. */ public function __construct(ClientInterface $client) { $this->checkCapabilities($client); $this->client = $client; - $this->openContext(); + + $this->start(); } /** - * Automatically closes the context when PHP's garbage collector kicks in. + * Automatically stops the consumer when PHP's garbage collector kicks in. */ public function __destruct() { - $this->closeContext(); + $this->stop(); } /** * Checks if the passed client instance satisfies the required conditions - * needed to initialize a monitor context. + * needed to initialize a monitor consumer. * - * @param ClientInterface $client Client instance used by the context. + * @param ClientInterface $client Client instance used by the consumer. */ private function checkCapabilities(ClientInterface $client) { if ($client->getConnection() instanceof AggregatedConnectionInterface) { - throw new NotSupportedException('Cannot initialize a monitor context when using aggregated connections'); + throw new NotSupportedException( + 'Cannot initialize a monitor consumer when using aggregated connections' + ); } + if ($client->getProfile()->supportsCommand('monitor') === false) { - throw new NotSupportedException('The current profile does not support the MONITOR command'); + throw new NotSupportedException( + 'The current profile does not support the MONITOR command' + ); } } /** - * Initializes the context and sends the MONITOR command to the server. + * Initializes the consumer and sends the MONITOR command to the server. */ - protected function openContext() + protected function start() { - $this->isValid = true; + $this->valid = true; $monitor = $this->client->createCommand('monitor'); $this->client->executeCommand($monitor); } /** - * Closes the context. Internally this is done by disconnecting from server + * Stops the consumer. Internally this is done by disconnecting from server * since there is no way to terminate the stream initialized by MONITOR. */ - public function closeContext() + public function stop() { $this->client->disconnect(); - $this->isValid = false; + $this->valid = false; } /** @@ -115,13 +122,13 @@ class MonitorContext implements \Iterator } /** - * Checks if the the context is still in a valid state to continue. + * Checks if the the consumer is still in a valid state to continue. * * @return Boolean */ public function valid() { - return $this->isValid; + return $this->valid; } /** diff --git a/tests/Predis/ClientTest.php b/tests/Predis/ClientTest.php index d602c552..3ea8b889 100644 --- a/tests/Predis/ClientTest.php +++ b/tests/Predis/ClientTest.php @@ -742,12 +742,12 @@ class ClientTest extends StandardTestCase /** * @group disconnected */ - public function testMonitorReturnsMonitorContext() + public function testMonitorReturnsMonitorConsumer() { $connection = $this->getMock('Predis\Connection\SingleConnectionInterface'); $client = new Client($connection); - $this->assertInstanceOf('Predis\Monitor\MonitorContext', $monitor = $client->monitor()); + $this->assertInstanceOf('Predis\Monitor\Consumer', $monitor = $client->monitor()); } /** diff --git a/tests/Predis/Monitor/MonitorContextTest.php b/tests/Predis/Monitor/ConsumerTest.php similarity index 86% rename from tests/Predis/Monitor/MonitorContextTest.php rename to tests/Predis/Monitor/ConsumerTest.php index 52a555fe..a825ae6c 100644 --- a/tests/Predis/Monitor/MonitorContextTest.php +++ b/tests/Predis/Monitor/ConsumerTest.php @@ -15,18 +15,19 @@ use PHPUnit_Framework_TestCase as StandardTestCase; use Predis\Client; use Predis\Profile\ServerProfile; +use Predis\Monitor\Consumer as MonitorConsumer; /** * @group realm-monitor */ -class MonitorContextTest extends StandardTestCase +class ConsumerTest extends StandardTestCase { /** * @group disconnected * @expectedException Predis\NotSupportedException * @expectedExceptionMessage The current profile does not support the MONITOR command */ - public function testMonitorContextRequireMonitorCommand() + public function testMonitorConsumerRequireMonitorCommand() { $profile = $this->getMock('Predis\Profile\ServerProfileInterface'); $profile->expects($this->once()) @@ -35,26 +36,26 @@ class MonitorContextTest extends StandardTestCase ->will($this->returnValue(false)); $client = new Client(null, array('profile' => $profile)); - $monitor = new MonitorContext($client); + $monitor = new MonitorConsumer($client); } /** * @group disconnected * @expectedException Predis\NotSupportedException - * @expectedExceptionMessage Cannot initialize a monitor context when using aggregated connections + * @expectedExceptionMessage Cannot initialize a monitor consumer when using aggregated connections */ - public function testMonitorContextDoesNotWorkOnClusters() + public function testMonitorConsumerDoesNotWorkOnClusters() { $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface'); $client = new Client($cluster); - $monitor = new MonitorContext($client); + $monitor = new MonitorConsumer($client); } /** * @group disconnected */ - public function testConstructorOpensContext() + public function testConstructorStartsConsumer() { $cmdMonitor = ServerProfile::getDefault()->createCommand('monitor'); @@ -69,7 +70,7 @@ class MonitorContextTest extends StandardTestCase ->method('executeCommand') ->with($cmdMonitor); - $monitor = new MonitorContext($client); + $monitor = new MonitorConsumer($client); } /** @@ -78,28 +79,28 @@ class MonitorContextTest extends StandardTestCase * but the reason is probably that the GC invokes __destruct() on monitor * thus calling $client->disconnect() a second time at the end of the test. */ - public function testClosingContextClosesConnection() + public function testStoppingConsumerClosesConnection() { $connection = $this->getMock('Predis\Connection\SingleConnectionInterface'); $client = $this->getMock('Predis\Client', array('disconnect'), array($connection)); $client->expects($this->exactly(2))->method('disconnect'); - $monitor = new MonitorContext($client); - $monitor->closeContext(); + $monitor = new MonitorConsumer($client); + $monitor->stop(); } /** * @group disconnected */ - public function testGarbageCollectorRunClosesContext() + public function testGarbageCollectorRunStopsConsumer() { $connection = $this->getMock('Predis\Connection\SingleConnectionInterface'); $client = $this->getMock('Predis\Client', array('disconnect'), array($connection)); $client->expects($this->once())->method('disconnect'); - $monitor = new MonitorContext($client); + $monitor = new MonitorConsumer($client); unset($monitor); } @@ -116,7 +117,7 @@ class MonitorContextTest extends StandardTestCase ->will($this->returnValue($message)); $client = new Client($connection); - $monitor = new MonitorContext($client); + $monitor = new MonitorConsumer($client); $payload = $monitor->current(); $this->assertSame(1323367530, (int) $payload->timestamp); @@ -139,7 +140,7 @@ class MonitorContextTest extends StandardTestCase ->will($this->returnValue($message)); $client = new Client($connection); - $monitor = new MonitorContext($client); + $monitor = new MonitorConsumer($client); $payload = $monitor->current(); $this->assertSame(1323367530, (int) $payload->timestamp); @@ -175,7 +176,7 @@ class MonitorContextTest extends StandardTestCase $consumer = new Client($parameters, $options); $consumer->connect(); - $monitor = new MonitorContext($consumer); + $monitor = new MonitorConsumer($consumer); $producer->echo('message1'); $producer->echo('message2'); @@ -185,7 +186,7 @@ class MonitorContextTest extends StandardTestCase if ($message->command == 'ECHO') { $echoed[] = $arguments = trim($message->arguments, '"'); if ($arguments == 'QUIT') { - $monitor->closeContext(); + $monitor->stop(); } } }