Predis to v2.2.2

This commit is contained in:
the-djmaze 2024-04-02 20:15:23 +02:00
parent f6b440adef
commit 84ffe1e552
259 changed files with 2407 additions and 9937 deletions

View file

@ -3,7 +3,8 @@
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
@ -11,21 +12,27 @@
namespace Predis\Connection;
use InvalidArgumentException;
use Predis\Client;
use Predis\Command\RawCommand;
use ReflectionClass;
use UnexpectedValueException;
/**
* Standard connection factory for creating connections to Redis nodes.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class Factory implements FactoryInterface
{
protected $schemes = array(
private $defaults = [];
protected $schemes = [
'tcp' => 'Predis\Connection\StreamConnection',
'unix' => 'Predis\Connection\StreamConnection',
'tls' => 'Predis\Connection\StreamConnection',
'redis' => 'Predis\Connection\StreamConnection',
'rediss' => 'Predis\Connection\StreamConnection',
'http' => 'Predis\Connection\WebdisConnection',
);
];
/**
* Checks if the provided argument represents a valid connection class
@ -34,9 +41,8 @@ class Factory implements FactoryInterface
*
* @param mixed $initializer FQN of a connection class or a callable for lazy initialization.
*
* @throws \InvalidArgumentException
*
* @return mixed
* @throws InvalidArgumentException
*/
protected function checkInitializer($initializer)
{
@ -44,10 +50,10 @@ class Factory implements FactoryInterface
return $initializer;
}
$class = new \ReflectionClass($initializer);
$class = new ReflectionClass($initializer);
if (!$class->isSubclassOf('Predis\Connection\NodeConnectionInterface')) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'A connection initializer must be a valid connection class or a callable object.'
);
}
@ -83,7 +89,7 @@ class Factory implements FactoryInterface
$scheme = $parameters->scheme;
if (!isset($this->schemes[$scheme])) {
throw new \InvalidArgumentException("Unknown connection scheme: '$scheme'.");
throw new InvalidArgumentException("Unknown connection scheme: '$scheme'.");
}
$initializer = $this->schemes[$scheme];
@ -96,8 +102,8 @@ class Factory implements FactoryInterface
}
if (!$connection instanceof NodeConnectionInterface) {
throw new \UnexpectedValueException(
'Objects returned by connection initializers must implement '.
throw new UnexpectedValueException(
'Objects returned by connection initializers must implement ' .
"'Predis\Connection\NodeConnectionInterface'."
);
}
@ -106,13 +112,26 @@ class Factory implements FactoryInterface
}
/**
* {@inheritdoc}
* Assigns a default set of parameters applied to new connections.
*
* The set of parameters passed to create a new connection have precedence
* over the default values set for the connection factory.
*
* @param array $parameters Set of connection parameters.
*/
public function aggregate(AggregateConnectionInterface $connection, array $parameters)
public function setDefaultParameters(array $parameters)
{
foreach ($parameters as $node) {
$connection->add($node instanceof NodeConnectionInterface ? $node : $this->create($node));
}
$this->defaults = $parameters;
}
/**
* Returns the default set of parameters applied to new connections.
*
* @return array
*/
public function getDefaultParameters()
{
return $this->defaults;
}
/**
@ -124,7 +143,17 @@ class Factory implements FactoryInterface
*/
protected function createParameters($parameters)
{
return Parameters::create($parameters);
if (is_string($parameters)) {
$parameters = Parameters::parse($parameters);
} else {
$parameters = $parameters ?: [];
}
if ($this->defaults) {
$parameters += $this->defaults;
}
return new Parameters($parameters);
}
/**
@ -136,15 +165,29 @@ class Factory implements FactoryInterface
{
$parameters = $connection->getParameters();
if (isset($parameters->password)) {
if (isset($parameters->password) && strlen($parameters->password)) {
$cmdAuthArgs = isset($parameters->username) && strlen($parameters->username)
? [$parameters->username, $parameters->password]
: [$parameters->password];
$connection->addConnectCommand(
new RawCommand(array('AUTH', $parameters->password))
new RawCommand('AUTH', $cmdAuthArgs)
);
}
if (isset($parameters->database)) {
if ($parameters->client_info ?? false && !$connection instanceof RelayConnection) {
$connection->addConnectCommand(
new RawCommand(array('SELECT', $parameters->database))
new RawCommand('CLIENT', ['SETINFO', 'LIB-NAME', 'predis'])
);
$connection->addConnectCommand(
new RawCommand('CLIENT', ['SETINFO', 'LIB-VER', Client::VERSION])
);
}
if (isset($parameters->database) && strlen($parameters->database)) {
$connection->addConnectCommand(
new RawCommand('SELECT', [$parameters->database])
);
}
}