Predis to v2.2.2

This commit is contained in:
the-djmaze 2024-04-08 16:29:55 +02:00
parent 9510347d3a
commit 969dca5f7e
449 changed files with 22013 additions and 2 deletions

View file

@ -0,0 +1,114 @@
<?php
/*
* This file is part of the Predis package.
*
* (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.
*/
namespace Predis\Configuration\Option;
use InvalidArgumentException;
use Predis\Configuration\OptionInterface;
use Predis\Configuration\OptionsInterface;
use Predis\Connection\AggregateConnectionInterface;
use Predis\Connection\NodeConnectionInterface;
/**
* Client option for configuring generic aggregate connections.
*
* The only value accepted by this option is a callable that must return a valid
* connection instance of Predis\Connection\AggregateConnectionInterface when
* invoked by the client to create a new aggregate connection instance.
*
* Creation and configuration of the aggregate connection is up to the user.
*/
class Aggregate implements OptionInterface
{
/**
* {@inheritdoc}
*/
public function filter(OptionsInterface $options, $value)
{
if (!is_callable($value)) {
throw new InvalidArgumentException(sprintf(
'%s expects a callable object acting as an aggregate connection initializer',
static::class
));
}
return $this->getConnectionInitializer($options, $value);
}
/**
* Wraps a user-supplied callable used to create a new aggregate connection.
*
* When the original callable acting as a connection initializer is executed
* by the client to create a new aggregate connection, it will receive the
* following arguments:
*
* - $parameters (same as passed to Predis\Client::__construct())
* - $options (options container, Predis\Configuration\OptionsInterface)
* - $option (current option, Predis\Configuration\OptionInterface)
*
* The original callable must return a valid aggregation connection instance
* of type Predis\Connection\AggregateConnectionInterface, this is enforced
* by the wrapper returned by this method and an exception is thrown when
* invalid values are returned.
*
* @param OptionsInterface $options Client options
* @param callable $callable Callable initializer
*
* @return callable
* @throws InvalidArgumentException
*/
protected function getConnectionInitializer(OptionsInterface $options, callable $callable)
{
return function ($parameters = null, $autoaggregate = false) use ($callable, $options) {
$connection = call_user_func_array($callable, [&$parameters, $options, $this]);
if (!$connection instanceof AggregateConnectionInterface) {
throw new InvalidArgumentException(sprintf(
'%s expects the supplied callable to return an instance of %s, but %s was returned',
static::class,
AggregateConnectionInterface::class,
is_object($connection) ? get_class($connection) : gettype($connection)
));
}
if ($parameters && $autoaggregate) {
static::aggregate($options, $connection, $parameters);
}
return $connection;
};
}
/**
* Adds single connections to an aggregate connection instance.
*
* @param OptionsInterface $options Client options
* @param AggregateConnectionInterface $connection Target aggregate connection
* @param array $nodes List of nodes to be added to the target aggregate connection
*/
public static function aggregate(OptionsInterface $options, AggregateConnectionInterface $connection, array $nodes)
{
$connections = $options->connections;
foreach ($nodes as $node) {
$connection->add($node instanceof NodeConnectionInterface ? $node : $connections->create($node));
}
}
/**
* {@inheritdoc}
*/
public function getDefault(OptionsInterface $options)
{
return;
}
}

View file

@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Predis package.
*
* (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.
*/
namespace Predis\Configuration\Option;
use InvalidArgumentException;
use Predis\Cluster\Hash;
use Predis\Configuration\OptionInterface;
use Predis\Configuration\OptionsInterface;
/**
* Configures an hash generator used by the redis-cluster connection backend.
*/
class CRC16 implements OptionInterface
{
/**
* Returns an hash generator instance from a descriptive name.
*
* @param OptionsInterface $options Client options.
* @param string $description Identifier of a hash generator (`predis`, `phpiredis`)
*
* @return callable
*/
protected function getHashGeneratorByDescription(OptionsInterface $options, $description)
{
if ($description === 'predis') {
return new Hash\CRC16();
} elseif ($description === 'phpiredis') {
return new Hash\PhpiredisCRC16();
} else {
throw new InvalidArgumentException(
'String value for the crc16 option must be either `predis` or `phpiredis`'
);
}
}
/**
* {@inheritdoc}
*/
public function filter(OptionsInterface $options, $value)
{
if (is_callable($value)) {
$value = call_user_func($value, $options);
}
if (is_string($value)) {
return $this->getHashGeneratorByDescription($options, $value);
} elseif ($value instanceof Hash\HashGeneratorInterface) {
return $value;
} else {
$class = get_class($this);
throw new InvalidArgumentException("$class expects a valid hash generator");
}
}
/**
* {@inheritdoc}
*/
public function getDefault(OptionsInterface $options)
{
return function_exists('phpiredis_utils_crc16')
? new Hash\PhpiredisCRC16()
: new Hash\CRC16();
}
}

View file

@ -0,0 +1,99 @@
<?php
/*
* This file is part of the Predis package.
*
* (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.
*/
namespace Predis\Configuration\Option;
use InvalidArgumentException;
use Predis\Cluster\RedisStrategy;
use Predis\Configuration\OptionsInterface;
use Predis\Connection\Cluster\PredisCluster;
use Predis\Connection\Cluster\RedisCluster;
/**
* Configures an aggregate connection used for clustering
* multiple Redis nodes using various implementations with
* different algorithms or strategies.
*/
class Cluster extends Aggregate
{
/**
* {@inheritdoc}
*/
public function filter(OptionsInterface $options, $value)
{
if (is_string($value)) {
$value = $this->getConnectionInitializerByString($options, $value);
}
if (is_callable($value)) {
return $this->getConnectionInitializer($options, $value);
} else {
throw new InvalidArgumentException(sprintf(
'%s expects either a string or a callable value, %s given',
static::class,
is_object($value) ? get_class($value) : gettype($value)
));
}
}
/**
* Returns a connection initializer from a descriptive name.
*
* @param OptionsInterface $options Client options
* @param string $description Identifier of a replication backend (`predis`, `sentinel`)
*
* @return callable
*/
protected function getConnectionInitializerByString(OptionsInterface $options, string $description)
{
switch ($description) {
case 'redis':
case 'redis-cluster':
return function ($parameters, $options, $option) {
return new RedisCluster($options->connections, new RedisStrategy($options->crc16));
};
case 'predis':
return $this->getDefaultConnectionInitializer();
default:
throw new InvalidArgumentException(sprintf(
'%s expects either `predis`, `redis` or `redis-cluster` as valid string values, `%s` given',
static::class,
$description
));
}
}
/**
* Returns the default connection initializer.
*
* @return callable
*/
protected function getDefaultConnectionInitializer()
{
return function ($parameters, $options, $option) {
return new PredisCluster();
};
}
/**
* {@inheritdoc}
*/
public function getDefault(OptionsInterface $options)
{
return $this->getConnectionInitializer(
$options,
$this->getDefaultConnectionInitializer()
);
}
}

View file

@ -0,0 +1,146 @@
<?php
/*
* This file is part of the Predis package.
*
* (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.
*/
namespace Predis\Configuration\Option;
use InvalidArgumentException;
use Predis\Command\FactoryInterface;
use Predis\Command\RawFactory;
use Predis\Command\RedisFactory;
use Predis\Configuration\OptionInterface;
use Predis\Configuration\OptionsInterface;
/**
* Configures a connection factory to be used by the client.
*/
class Commands implements OptionInterface
{
/**
* {@inheritdoc}
*/
public function filter(OptionsInterface $options, $value)
{
if (is_callable($value)) {
$value = call_user_func($value, $options);
}
if ($value instanceof FactoryInterface) {
return $value;
} elseif (is_array($value)) {
return $this->createFactoryByArray($options, $value);
} elseif (is_string($value)) {
return $this->createFactoryByString($options, $value);
} else {
throw new InvalidArgumentException(sprintf(
'%s expects a valid command factory',
static::class
));
}
}
/**
* Creates a new default command factory from a named array.
*
* The factory instance is configured according to the supplied named array
* mapping command IDs (passed as keys) to the FCQN of classes implementing
* Predis\Command\CommandInterface.
*
* @param OptionsInterface $options Client options container
* @param array $value Named array mapping command IDs to classes
*
* @return FactoryInterface
*/
protected function createFactoryByArray(OptionsInterface $options, array $value)
{
/**
* @var FactoryInterface
*/
$commands = $this->getDefault($options);
foreach ($value as $commandID => $commandClass) {
if ($commandClass === null) {
$commands->undefine($commandID);
} else {
$commands->define($commandID, $commandClass);
}
}
return $commands;
}
/**
* Creates a new command factory from a descriptive string.
*
* The factory instance is configured according to the supplied descriptive
* string that identifies specific configurations of schemes and connection
* classes. Supported configuration values are:
*
* - "predis" returns the default command factory used by Predis
* - "raw" returns a command factory that creates only raw commands
* - "default" is simply an alias of "predis"
*
* @param OptionsInterface $options Client options container
* @param string $value Descriptive string identifying the desired configuration
*
* @return FactoryInterface
*/
protected function createFactoryByString(OptionsInterface $options, string $value)
{
switch (strtolower($value)) {
case 'default':
case 'predis':
return $this->getDefault($options);
case 'raw':
return $this->createRawFactory($options);
default:
throw new InvalidArgumentException(sprintf(
'%s does not recognize `%s` as a supported configuration string',
static::class,
$value
));
}
}
/**
* Creates a new raw command factory instance.
*
* @param OptionsInterface $options Client options container
*/
protected function createRawFactory(OptionsInterface $options): FactoryInterface
{
$commands = new RawFactory();
if (isset($options->prefix)) {
throw new InvalidArgumentException(sprintf(
'%s does not support key prefixing', RawFactory::class
));
}
return $commands;
}
/**
* {@inheritdoc}
*/
public function getDefault(OptionsInterface $options)
{
$commands = new RedisFactory();
if (isset($options->prefix)) {
$commands->setProcessor($options->prefix);
}
return $commands;
}
}

View file

@ -0,0 +1,152 @@
<?php
/*
* This file is part of the Predis package.
*
* (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.
*/
namespace Predis\Configuration\Option;
use InvalidArgumentException;
use Predis\Configuration\OptionInterface;
use Predis\Configuration\OptionsInterface;
use Predis\Connection\Factory;
use Predis\Connection\FactoryInterface;
use Predis\Connection\PhpiredisSocketConnection;
use Predis\Connection\PhpiredisStreamConnection;
use Predis\Connection\RelayConnection;
/**
* Configures a new connection factory instance.
*
* The client uses the connection factory to create the underlying connections
* to single redis nodes in a single-server configuration or in replication and
* cluster configurations.
*/
class Connections implements OptionInterface
{
/**
* {@inheritdoc}
*/
public function filter(OptionsInterface $options, $value)
{
if (is_callable($value)) {
$value = call_user_func($value, $options);
}
if ($value instanceof FactoryInterface) {
return $value;
} elseif (is_array($value)) {
return $this->createFactoryByArray($options, $value);
} elseif (is_string($value)) {
return $this->createFactoryByString($options, $value);
} else {
throw new InvalidArgumentException(sprintf(
'%s expects a valid connection factory', static::class
));
}
}
/**
* Creates a new connection factory from a named array.
*
* The factory instance is configured according to the supplied named array
* mapping URI schemes (passed as keys) to the FCQN of classes implementing
* Predis\Connection\NodeConnectionInterface, or callable objects acting as
* lazy initializers and returning new instances of classes implementing
* Predis\Connection\NodeConnectionInterface.
*
* @param OptionsInterface $options Client options
* @param array $value Named array mapping URI schemes to classes or callables
*
* @return FactoryInterface
*/
protected function createFactoryByArray(OptionsInterface $options, array $value)
{
/**
* @var FactoryInterface
*/
$factory = $this->getDefault($options);
foreach ($value as $scheme => $initializer) {
$factory->define($scheme, $initializer);
}
return $factory;
}
/**
* Creates a new connection factory from a descriptive string.
*
* The factory instance is configured according to the supplied descriptive
* string that identifies specific configurations of schemes and connection
* classes. Supported configuration values are:
*
* - "phpiredis-stream" maps tcp, redis, unix to PhpiredisStreamConnection
* - "phpiredis-socket" maps tcp, redis, unix to PhpiredisSocketConnection
* - "phpiredis" is an alias of "phpiredis-stream"
* - "relay" maps tcp, redis, unix, tls, rediss to RelayConnection
*
* @param OptionsInterface $options Client options
* @param string $value Descriptive string identifying the desired configuration
*
* @return FactoryInterface
*/
protected function createFactoryByString(OptionsInterface $options, string $value)
{
/**
* @var FactoryInterface
*/
$factory = $this->getDefault($options);
switch (strtolower($value)) {
case 'phpiredis':
case 'phpiredis-stream':
$factory->define('tcp', PhpiredisStreamConnection::class);
$factory->define('redis', PhpiredisStreamConnection::class);
$factory->define('unix', PhpiredisStreamConnection::class);
break;
case 'phpiredis-socket':
$factory->define('tcp', PhpiredisSocketConnection::class);
$factory->define('redis', PhpiredisSocketConnection::class);
$factory->define('unix', PhpiredisSocketConnection::class);
break;
case 'relay':
$factory->define('tcp', RelayConnection::class);
$factory->define('redis', RelayConnection::class);
$factory->define('unix', RelayConnection::class);
break;
case 'default':
return $factory;
default:
throw new InvalidArgumentException(sprintf(
'%s does not recognize `%s` as a supported configuration string', static::class, $value
));
}
return $factory;
}
/**
* {@inheritdoc}
*/
public function getDefault(OptionsInterface $options)
{
$factory = new Factory();
if ($options->defined('parameters')) {
$factory->setDefaultParameters($options->parameters);
}
return $factory;
}
}

View file

@ -0,0 +1,39 @@
<?php
/*
* This file is part of the Predis package.
*
* (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.
*/
namespace Predis\Configuration\Option;
use Predis\Configuration\OptionInterface;
use Predis\Configuration\OptionsInterface;
/**
* Configures whether consumers (such as the client) should throw exceptions on
* Redis errors (-ERR responses) or just return instances of error responses.
*/
class Exceptions implements OptionInterface
{
/**
* {@inheritdoc}
*/
public function filter(OptionsInterface $options, $value)
{
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
/**
* {@inheritdoc}
*/
public function getDefault(OptionsInterface $options)
{
return true;
}
}

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Predis package.
*
* (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.
*/
namespace Predis\Configuration\Option;
use Predis\Command\Processor\KeyPrefixProcessor;
use Predis\Command\Processor\ProcessorInterface;
use Predis\Configuration\OptionInterface;
use Predis\Configuration\OptionsInterface;
/**
* Configures a command processor that apply the specified prefix string to a
* series of Redis commands considered prefixable.
*/
class Prefix implements OptionInterface
{
/**
* {@inheritdoc}
*/
public function filter(OptionsInterface $options, $value)
{
if (is_callable($value)) {
$value = call_user_func($value, $options);
}
if ($value instanceof ProcessorInterface) {
return $value;
}
return new KeyPrefixProcessor((string) $value);
}
/**
* {@inheritdoc}
*/
public function getDefault(OptionsInterface $options)
{
// NOOP
}
}

View file

@ -0,0 +1,126 @@
<?php
/*
* This file is part of the Predis package.
*
* (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.
*/
namespace Predis\Configuration\Option;
use InvalidArgumentException;
use Predis\Configuration\OptionsInterface;
use Predis\Connection\AggregateConnectionInterface;
use Predis\Connection\Replication\MasterSlaveReplication;
use Predis\Connection\Replication\SentinelReplication;
/**
* Configures an aggregate connection used for master/slave replication among
* multiple Redis nodes.
*/
class Replication extends Aggregate
{
/**
* {@inheritdoc}
*/
public function filter(OptionsInterface $options, $value)
{
if (is_string($value)) {
$value = $this->getConnectionInitializerByString($options, $value);
}
if (is_callable($value)) {
return $this->getConnectionInitializer($options, $value);
} else {
throw new InvalidArgumentException(sprintf(
'%s expects either a string or a callable value, %s given',
static::class,
is_object($value) ? get_class($value) : gettype($value)
));
}
}
/**
* Returns a connection initializer (callable) from a descriptive string.
*
* Each connection initializer is specialized for the specified replication
* backend so that all the necessary steps for the configuration of the new
* aggregate connection are performed inside the initializer and the client
* receives a ready-to-use connection.
*
* Supported configuration values are:
*
* - `predis` for unmanaged replication setups
* - `redis-sentinel` for replication setups managed by redis-sentinel
* - `sentinel` is an alias of `redis-sentinel`
*
* @param OptionsInterface $options Client options
* @param string $description Identifier of a replication backend
*
* @return callable
*/
protected function getConnectionInitializerByString(OptionsInterface $options, string $description)
{
switch ($description) {
case 'sentinel':
case 'redis-sentinel':
return function ($parameters, $options) {
return new SentinelReplication($options->service, $parameters, $options->connections);
};
case 'predis':
return $this->getDefaultConnectionInitializer();
default:
throw new InvalidArgumentException(sprintf(
'%s expects either `predis`, `sentinel` or `redis-sentinel` as valid string values, `%s` given',
static::class,
$description
));
}
}
/**
* Returns the default connection initializer.
*
* @return callable
*/
protected function getDefaultConnectionInitializer()
{
return function ($parameters, $options) {
$connection = new MasterSlaveReplication();
if ($options->autodiscovery) {
$connection->setConnectionFactory($options->connections);
$connection->setAutoDiscovery(true);
}
return $connection;
};
}
/**
* {@inheritdoc}
*/
public static function aggregate(OptionsInterface $options, AggregateConnectionInterface $connection, array $nodes)
{
if (!$connection instanceof SentinelReplication) {
parent::aggregate($options, $connection, $nodes);
}
}
/**
* {@inheritdoc}
*/
public function getDefault(OptionsInterface $options)
{
return $this->getConnectionInitializer(
$options,
$this->getDefaultConnectionInitializer()
);
}
}