master

laravel/framework

Last updated at: 29/12/2023 09:23

PredisConnection.php

TLDR

This file contains the PredisConnection class, which is a Redis connection implementation that extends the Connection class and implements the Connection interface. It also includes the createSubscription method, which is used to subscribe to a set of channels for messages, and the parseParametersForEvent method, which is used to parse command parameters for event dispatching.

Methods

createSubscription

This method is used to subscribe to a set of given channels for messages. It takes three parameters:

  • $channels (array|string): The channels to subscribe to.
  • $callback (Closure): The callback function to be executed when a message is received.
  • $method (string): The method to use for subscription (default value: 'subscribe').

The method creates a pub/sub loop using the pubSubLoop method, subscribes to the channels using the specified method, and loops through incoming messages. If the message kind is 'message' or 'pmessage', the callback function is executed with the message payload and channel as arguments.

parseParametersForEvent

This protected method is used to parse the command's parameters for event dispatching. It takes an array of parameters as input and returns an array.

The method uses the collect function to transform each parameter in the array. If the parameter is an instance of ArrayableArgument, it calls the toArray method to convert it to an array. Otherwise, it returns the parameter as is. The transformed parameters are then converted back to an array using the all method.

Classes

PredisConnection

This class is a Redis connection implementation that extends the Connection class and implements the Connection interface. It has a protected property $client which holds an instance of the Predis\Client class.

The class has a constructor that accepts a Predis\Client instance as a parameter and assigns it to the $client property.

The class also includes the createSubscription method for subscribing to channels and the parseParametersForEvent method for parsing command parameters for event dispatching.

<?php

namespace Illuminate\Redis\Connections;

use Closure;
use Illuminate\Contracts\Redis\Connection as ConnectionContract;
use Predis\Command\Argument\ArrayableArgument;

/**
 * @mixin \Predis\Client
 */
class PredisConnection extends Connection implements ConnectionContract
{
    /**
     * The Predis client.
     *
     * @var \Predis\Client
     */
    protected $client;

    /**
     * Create a new Predis connection.
     *
     * @param  \Predis\Client  $client
     * @return void
     */
    public function __construct($client)
    {
        $this->client = $client;
    }

    /**
     * Subscribe to a set of given channels for messages.
     *
     * @param  array|string  $channels
     * @param  \Closure  $callback
     * @param  string  $method
     * @return void
     */
    public function createSubscription($channels, Closure $callback, $method = 'subscribe')
    {
        $loop = $this->pubSubLoop();

        $loop->{$method}(...array_values((array) $channels));

        foreach ($loop as $message) {
            if ($message->kind === 'message' || $message->kind === 'pmessage') {
                $callback($message->payload, $message->channel);
            }
        }

        unset($loop);
    }

    /**
     * Parse the command's parameters for event dispatching.
     *
     * @param  array  $parameters
     * @return array
     */
    protected function parseParametersForEvent(array $parameters)
    {
        return collect($parameters)
            ->transform(function ($parameter) {
                return $parameter instanceof ArrayableArgument
                    ? $parameter->toArray()
                    : $parameter;
            })->all();
    }
}