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,27 +12,28 @@
namespace Predis\PubSub;
use Iterator;
use ReturnTypeWillChange;
/**
* Base implementation of a PUB/SUB consumer abstraction based on PHP iterators.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
abstract class AbstractConsumer implements \Iterator
abstract class AbstractConsumer implements Iterator
{
const SUBSCRIBE = 'subscribe';
const UNSUBSCRIBE = 'unsubscribe';
const PSUBSCRIBE = 'psubscribe';
const PUNSUBSCRIBE = 'punsubscribe';
const MESSAGE = 'message';
const PMESSAGE = 'pmessage';
const PONG = 'pong';
public const SUBSCRIBE = 'subscribe';
public const UNSUBSCRIBE = 'unsubscribe';
public const PSUBSCRIBE = 'psubscribe';
public const PUNSUBSCRIBE = 'punsubscribe';
public const MESSAGE = 'message';
public const PMESSAGE = 'pmessage';
public const PONG = 'pong';
const STATUS_VALID = 1; // 0b0001
const STATUS_SUBSCRIBED = 2; // 0b0010
const STATUS_PSUBSCRIBED = 4; // 0b0100
public const STATUS_VALID = 1; // 0b0001
public const STATUS_SUBSCRIBED = 2; // 0b0010
public const STATUS_PSUBSCRIBED = 4; // 0b0100
private $position = null;
private $statusFlags = self::STATUS_VALID;
protected $position;
protected $statusFlags = self::STATUS_VALID;
/**
* Automatically stops the consumer when the garbage collector kicks in.
@ -56,9 +58,9 @@ abstract class AbstractConsumer implements \Iterator
/**
* Subscribes to the specified channels.
*
* @param mixed $channel,... One or more channel names.
* @param string ...$channel One or more channel names.
*/
public function subscribe($channel /*, ... */)
public function subscribe($channel /* , ... */)
{
$this->writeRequest(self::SUBSCRIBE, func_get_args());
$this->statusFlags |= self::STATUS_SUBSCRIBED;
@ -67,9 +69,9 @@ abstract class AbstractConsumer implements \Iterator
/**
* Unsubscribes from the specified channels.
*
* @param string ... One or more channel names.
* @param string ...$channel One or more channel names.
*/
public function unsubscribe(/* ... */)
public function unsubscribe(...$channel)
{
$this->writeRequest(self::UNSUBSCRIBE, func_get_args());
}
@ -77,9 +79,9 @@ abstract class AbstractConsumer implements \Iterator
/**
* Subscribes to the specified channels using a pattern.
*
* @param mixed $pattern,... One or more channel name patterns.
* @param string ...$pattern One or more channel name patterns.
*/
public function psubscribe($pattern /* ... */)
public function psubscribe(...$pattern)
{
$this->writeRequest(self::PSUBSCRIBE, func_get_args());
$this->statusFlags |= self::STATUS_PSUBSCRIBED;
@ -88,9 +90,9 @@ abstract class AbstractConsumer implements \Iterator
/**
* Unsubscribes from the specified channels using a pattern.
*
* @param string ... One or more channel name patterns.
* @param string ...$pattern One or more channel name patterns.
*/
public function punsubscribe(/* ... */)
public function punsubscribe(...$pattern)
{
$this->writeRequest(self::PUNSUBSCRIBE, func_get_args());
}
@ -103,7 +105,7 @@ abstract class AbstractConsumer implements \Iterator
*/
public function ping($payload = null)
{
$this->writeRequest('PING', array($payload));
$this->writeRequest('PING', [$payload]);
}
/**
@ -149,8 +151,9 @@ abstract class AbstractConsumer implements \Iterator
abstract protected function writeRequest($method, $arguments);
/**
* {@inheritdoc}
* @return void
*/
#[ReturnTypeWillChange]
public function rewind()
{
// NOOP
@ -162,22 +165,25 @@ abstract class AbstractConsumer implements \Iterator
*
* @return array
*/
#[ReturnTypeWillChange]
public function current()
{
return $this->getValue();
}
/**
* {@inheritdoc}
* @return int|null
*/
#[ReturnTypeWillChange]
public function key()
{
return $this->position;
}
/**
* {@inheritdoc}
* @return int|null
*/
#[ReturnTypeWillChange]
public function next()
{
if ($this->valid()) {
@ -192,6 +198,7 @@ abstract class AbstractConsumer implements \Iterator
*
* @return bool
*/
#[ReturnTypeWillChange]
public function valid()
{
$isValid = $this->isFlagSet(self::STATUS_VALID);

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.
@ -14,18 +15,16 @@ namespace Predis\PubSub;
use Predis\ClientException;
use Predis\ClientInterface;
use Predis\Command\Command;
use Predis\Connection\AggregateConnectionInterface;
use Predis\Connection\Cluster\ClusterInterface;
use Predis\NotSupportedException;
/**
* PUB/SUB consumer abstraction.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
* PUB/SUB consumer.
*/
class Consumer extends AbstractConsumer
{
private $client;
private $options;
protected $client;
protected $options;
/**
* @param ClientInterface $client Client instance used by the consumer.
@ -35,7 +34,7 @@ class Consumer extends AbstractConsumer
{
$this->checkCapabilities($client);
$this->options = $options ?: array();
$this->options = $options ?: [];
$this->client = $client;
$this->genericSubscribeInit('subscribe');
@ -60,19 +59,19 @@ class Consumer extends AbstractConsumer
*
* @throws NotSupportedException
*/
private function checkCapabilities(ClientInterface $client)
protected function checkCapabilities(ClientInterface $client)
{
if ($client->getConnection() instanceof AggregateConnectionInterface) {
if ($client->getConnection() instanceof ClusterInterface) {
throw new NotSupportedException(
'Cannot initialize a PUB/SUB consumer over aggregate connections.'
'Cannot initialize a PUB/SUB consumer over cluster connections.'
);
}
$commands = array('publish', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');
$commands = ['publish', 'subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe'];
if ($client->getProfile()->supportsCommands($commands) === false) {
if (!$client->getCommandFactory()->supports(...$commands)) {
throw new NotSupportedException(
'The current profile does not support PUB/SUB related commands.'
'PUB/SUB commands are not supported by the current command factory.'
);
}
}
@ -82,7 +81,7 @@ class Consumer extends AbstractConsumer
*
* @param string $subscribeAction Type of subscription.
*/
private function genericSubscribeInit($subscribeAction)
protected function genericSubscribeInit($subscribeAction)
{
if (isset($this->options[$subscribeAction])) {
$this->$subscribeAction($this->options[$subscribeAction]);
@ -129,25 +128,25 @@ class Consumer extends AbstractConsumer
// no break
case self::MESSAGE:
return (object) array(
return (object) [
'kind' => $response[0],
'channel' => $response[1],
'payload' => $response[2],
);
];
case self::PMESSAGE:
return (object) array(
return (object) [
'kind' => $response[0],
'pattern' => $response[1],
'channel' => $response[2],
'payload' => $response[3],
);
];
case self::PONG:
return (object) array(
return (object) [
'kind' => $response[0],
'payload' => $response[1],
);
];
default:
throw new ClientException(

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,11 +12,11 @@
namespace Predis\PubSub;
use InvalidArgumentException;
/**
* Method-dispatcher loop built around the client-side abstraction of a Redis
* PUB / SUB context.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class DispatcherLoop
{
@ -30,7 +31,7 @@ class DispatcherLoop
*/
public function __construct(Consumer $pubsub)
{
$this->callbacks = array();
$this->callbacks = [];
$this->pubsub = $pubsub;
}
@ -39,12 +40,12 @@ class DispatcherLoop
*
* @param mixed $callable A callback.
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
protected function assertCallback($callable)
{
if (!is_callable($callable)) {
throw new \InvalidArgumentException('The given argument must be a callable object.');
throw new InvalidArgumentException('The given argument must be a callable object.');
}
}
@ -91,11 +92,11 @@ class DispatcherLoop
* Binds a callback to a channel.
*
* @param string $channel Channel name.
* @param Callable $callback A callback.
* @param callable $callback A callback.
*/
public function attachCallback($channel, $callback)
{
$callbackName = $this->getPrefixKeys().$channel;
$callbackName = $this->getPrefixKeys() . $channel;
$this->assertCallback($callback);
$this->callbacks[$callbackName] = $callback;
@ -109,7 +110,7 @@ class DispatcherLoop
*/
public function detachCallback($channel)
{
$callbackName = $this->getPrefixKeys().$channel;
$callbackName = $this->getPrefixKeys() . $channel;
if (isset($this->callbacks[$callbackName])) {
unset($this->callbacks[$callbackName]);
@ -128,7 +129,7 @@ class DispatcherLoop
if ($kind !== Consumer::MESSAGE && $kind !== Consumer::PMESSAGE) {
if (isset($this->subscriptionCallback)) {
$callback = $this->subscriptionCallback;
call_user_func($callback, $message);
call_user_func($callback, $message, $this);
}
continue;
@ -136,10 +137,10 @@ class DispatcherLoop
if (isset($this->callbacks[$message->channel])) {
$callback = $this->callbacks[$message->channel];
call_user_func($callback, $message->payload);
call_user_func($callback, $message->payload, $this);
} elseif (isset($this->defaultCallback)) {
$callback = $this->defaultCallback;
call_user_func($callback, $message);
call_user_func($callback, $message, $this);
}
}
}