mirror of
https://github.com/predis/predis.git
synced 2026-09-13 03:47:03 +00:00
Simplify connection factory class by removing dependency on profile.
A profile is not needed to create instances of basic initialization commands such as AUTH (authentication) and SELECT (database selection) since they do not need prefixing and other stuff. If needed, developers can still extend the base connection factory to make it use a server profile and configure the client to use it using options.
This commit is contained in:
@@ -45,6 +45,10 @@ v0.9.0 (201x-xx-xx)
|
||||
renamed to `writeRequest()` for consistency with its counterpart, the method
|
||||
`readResponse()`.
|
||||
|
||||
- The connection factory has been simplified and does not need anymore a profile
|
||||
to create `AUTH` and `SELECT` commands when connection parameters contain both
|
||||
`password` and `database`. Raw commands will be used instead.
|
||||
|
||||
- Most classes and interfaces in the `Predis\Protocol` namespace have been moved
|
||||
or renamed while rationalizing the whole API of external protocol processors.
|
||||
|
||||
|
||||
@@ -48,6 +48,6 @@ class ConnectionFactoryOption implements OptionInterface
|
||||
*/
|
||||
public function getDefault(OptionsInterface $options)
|
||||
{
|
||||
return new ConnectionFactory($options->profile);
|
||||
return new ConnectionFactory();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
namespace Predis\Connection;
|
||||
|
||||
use Predis\Profile;
|
||||
use Predis\Command;
|
||||
|
||||
/**
|
||||
* Provides a default factory for Redis connections that maps URI schemes
|
||||
@@ -21,33 +21,11 @@ use Predis\Profile;
|
||||
*/
|
||||
class ConnectionFactory implements ConnectionFactoryInterface
|
||||
{
|
||||
protected $schemes;
|
||||
protected $profile;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the default connection factory class used by Predis.
|
||||
*
|
||||
* @param Profile\ProfileInterface $profile Server profile used to initialize new connections.
|
||||
*/
|
||||
public function __construct(Profile\ProfileInterface $profile = null)
|
||||
{
|
||||
$this->schemes = $this->getDefaultSchemes();
|
||||
$this->profile = $profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a named array that maps URI schemes to connection classes.
|
||||
*
|
||||
* @return array Map of URI schemes and connection classes.
|
||||
*/
|
||||
protected function getDefaultSchemes()
|
||||
{
|
||||
return array(
|
||||
'tcp' => 'Predis\Connection\StreamConnection',
|
||||
'unix' => 'Predis\Connection\StreamConnection',
|
||||
'http' => 'Predis\Connection\WebdisConnection',
|
||||
);
|
||||
}
|
||||
protected $schemes = array(
|
||||
'tcp' => 'Predis\Connection\StreamConnection',
|
||||
'unix' => 'Predis\Connection\StreamConnection',
|
||||
'http' => 'Predis\Connection\WebdisConnection',
|
||||
);
|
||||
|
||||
/**
|
||||
* Checks if the provided argument represents a valid connection class
|
||||
@@ -141,38 +119,16 @@ class ConnectionFactory implements ConnectionFactoryInterface
|
||||
*/
|
||||
protected function prepareConnection(SingleConnectionInterface $connection)
|
||||
{
|
||||
if (isset($this->profile)) {
|
||||
$parameters = $connection->getParameters();
|
||||
$parameters = $connection->getParameters();
|
||||
|
||||
if (isset($parameters->password)) {
|
||||
$command = $this->profile->createCommand('auth', array($parameters->password));
|
||||
$connection->pushInitCommand($command);
|
||||
}
|
||||
if (isset($parameters->password)) {
|
||||
$command = new Command\RawCommand(array('AUTH', $parameters->password));
|
||||
$connection->pushInitCommand($command);
|
||||
}
|
||||
|
||||
if (isset($parameters->database)) {
|
||||
$command = $this->profile->createCommand('select', array($parameters->database));
|
||||
$connection->pushInitCommand($command);
|
||||
}
|
||||
if (isset($parameters->database)) {
|
||||
$command = new Command\RawCommand(array('SELECT', $parameters->database));
|
||||
$connection->pushInitCommand($command);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the server profile used to create initialization commands for connections.
|
||||
*
|
||||
* @param Profile\ProfileInterface $profile Server profile instance.
|
||||
*/
|
||||
public function setProfile(Profile\ProfileInterface $profile)
|
||||
{
|
||||
$this->profile = $profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the server profile used to create initialization commands for connections.
|
||||
*
|
||||
* @return Profile\ProfileInterface
|
||||
*/
|
||||
public function getProfile()
|
||||
{
|
||||
return $this->profile;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,51 +125,25 @@ class ConnectionFactoryTest extends PredisTestCase
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @todo This test smells but there's no other way around it right now.
|
||||
*/
|
||||
public function testCreateConnectionWithInitializationCommands()
|
||||
{
|
||||
$test = $this;
|
||||
$database = 15;
|
||||
$password = 'foobar';
|
||||
$commands = array();
|
||||
|
||||
$createCommand = function ($id, $arguments) use ($test, &$commands) {
|
||||
$commands[$id] = $arguments;
|
||||
$command = $test->getMock('Predis\Command\CommandInterface');
|
||||
|
||||
return $command;
|
||||
};
|
||||
|
||||
$profile = $this->getMock('Predis\Profile\ProfileInterface');
|
||||
$profile->expects($this->exactly(2))
|
||||
->method('createCommand')
|
||||
->with($this->isType('string'), $this->isType('array'))
|
||||
->will($this->returnCallback($createCommand));
|
||||
|
||||
$factory = new ConnectionFactory($profile);
|
||||
$parameters = new ConnectionParameters(array('database' => $database, 'password' => $password));
|
||||
$connection = $factory->create($parameters);
|
||||
|
||||
$this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
|
||||
$this->assertEquals(2, count($commands)); // TODO: assertCount()?
|
||||
$this->assertEquals(array($database), $commands['select']);
|
||||
$this->assertEquals(array($password), $commands['auth']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @todo This test smells but there's no other way around it right now.
|
||||
*/
|
||||
public function testCreateConnectionWithDatabaseAndPasswordButNoProfile()
|
||||
{
|
||||
$parameters = new ConnectionParameters(array('database' => 0, 'password' => 'foobar'));
|
||||
$parameters = new ConnectionParameters(array(
|
||||
'database' => '0',
|
||||
'password' => 'foobar'
|
||||
));
|
||||
|
||||
$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
|
||||
$connection->expects($this->never())
|
||||
$connection->expects($this->once())
|
||||
->method('getParameters')
|
||||
->will($this->returnValue($parameters));
|
||||
$connection->expects($this->never())
|
||||
->method('pushInitCommand');
|
||||
$connection->expects($this->at(1))
|
||||
->method('pushInitCommand')
|
||||
->with($this->isRedisCommand('AUTH', array('foobar')));
|
||||
$connection->expects($this->at(2))
|
||||
->method('pushInitCommand')
|
||||
->with($this->isRedisCommand('SELECT', array(0)));
|
||||
|
||||
$factory = new ConnectionFactory();
|
||||
|
||||
@@ -343,29 +317,6 @@ class ConnectionFactoryTest extends PredisTestCase
|
||||
$factory->aggregate($cluster, array());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @todo We might want to add a test for SingleConnectionInterface::pushInitCommand().
|
||||
*/
|
||||
public function testAggregatedConnectionWithProfileArgument()
|
||||
{
|
||||
list(, $connectionClass) = $this->getMockConnectionClass();
|
||||
|
||||
$cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
|
||||
$profile = $this->getMock('Predis\Profile\ProfileInterface');
|
||||
|
||||
$factory = $this->getMock('Predis\Connection\ConnectionFactory', array('create'), array($profile));
|
||||
$factory->expects($this->exactly(2))
|
||||
->method('create')
|
||||
->with($this->anything())
|
||||
->will($this->returnCallback(function ($_) use ($connectionClass) {
|
||||
return new $connectionClass();
|
||||
}));
|
||||
|
||||
$nodes = array('tcp://127.0.0.1:7001?password=foo', 'tcp://127.0.0.1:7002?password=bar');
|
||||
$factory->aggregate($cluster, $nodes);
|
||||
}
|
||||
|
||||
|
||||
// ******************************************************************** //
|
||||
// ---- HELPER METHODS ------------------------------------------------ //
|
||||
|
||||
Reference in New Issue
Block a user