Files
predis/lib/Predis/Connection/ConnectionFactory.php
T
Daniele Alessandri 5b7504b069 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.
2013-12-02 13:01:37 +01:00

135 lines
3.9 KiB
PHP

<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Connection;
use Predis\Command;
/**
* Provides a default factory for Redis connections that maps URI schemes
* to connection classes implementing Predis\Connection\SingleConnectionInterface.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class ConnectionFactory implements ConnectionFactoryInterface
{
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
* implementing Predis\Connection\SingleConnectionInterface. Optionally,
* callable objects are used for lazy initialization of connection objects.
*
* @param mixed $initializer FQN of a connection class or a callable for lazy initialization.
* @return mixed
*/
protected function checkInitializer($initializer)
{
if (is_callable($initializer)) {
return $initializer;
}
$initializerReflection = new \ReflectionClass($initializer);
if (!$initializerReflection->isSubclassOf('Predis\Connection\SingleConnectionInterface')) {
throw new \InvalidArgumentException(
'A connection initializer must be a valid connection class or a callable object'
);
}
return $initializer;
}
/**
* {@inheritdoc}
*/
public function define($scheme, $initializer)
{
$this->schemes[$scheme] = $this->checkInitializer($initializer);
}
/**
* {@inheritdoc}
*/
public function undefine($scheme)
{
unset($this->schemes[$scheme]);
}
/**
* {@inheritdoc}
*/
public function create($parameters)
{
if (!$parameters instanceof ConnectionParametersInterface) {
$parameters = new ConnectionParameters($parameters);
}
$scheme = $parameters->scheme;
if (!isset($this->schemes[$scheme])) {
throw new \InvalidArgumentException("Unknown connection scheme: $scheme");
}
$initializer = $this->schemes[$scheme];
if (is_callable($initializer)) {
$connection = call_user_func($initializer, $parameters, $this);
} else {
$connection = new $initializer($parameters);
$this->prepareConnection($connection);
}
if (!$connection instanceof SingleConnectionInterface) {
throw new \InvalidArgumentException(
'Objects returned by connection initializers must implement ' .
'Predis\Connection\SingleConnectionInterface'
);
}
return $connection;
}
/**
* {@inheritdoc}
*/
public function aggregate(AggregatedConnectionInterface $connection, array $parameters)
{
foreach ($parameters as $node) {
$connection->add($node instanceof SingleConnectionInterface ? $node : $this->create($node));
}
}
/**
* Prepares a connection object after its initialization.
*
* @param SingleConnectionInterface $connection Instance of a connection object.
*/
protected function prepareConnection(SingleConnectionInterface $connection)
{
$parameters = $connection->getParameters();
if (isset($parameters->password)) {
$command = new Command\RawCommand(array('AUTH', $parameters->password));
$connection->pushInitCommand($command);
}
if (isset($parameters->database)) {
$command = new Command\RawCommand(array('SELECT', $parameters->database));
$connection->pushInitCommand($command);
}
}
}