master

laravel/framework

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

RedisConnector.php

TLDR

This file is a part of the Illuminate\Queue\Connectors namespace and contains the RedisConnector class. This class is responsible for creating connections to Redis queues.

Methods

__construct

The __construct method is the constructor of the RedisConnector class. It accepts two parameters: $redis of type Illuminate\Contracts\Redis\Factory and $connection of type string|null. It initializes the class properties $redis and $connection with the provided values.

connect

The connect method is responsible for establishing a connection to a queue. It accepts an array parameter $config and returns an instance implementing the Illuminate\Contracts\Queue\Queue interface. It creates and returns a new RedisQueue instance, passing the $redis object, the value of $config['queue'], the value of $config['connection'] or the connection value from the constructor $connection if it is not provided in the $config array, the value of $config['retry_after'] or the default value 60, the value of $config['block_for'] or null, the value of $config['after_commit'] or null, and the value of $config['migration_batch_size'] or -1.

Classes

RedisConnector

The RedisConnector class is responsible for creating connections to Redis queues. It has two class properties: $redis of type Illuminate\Contracts\Redis\Factory and $connection of type string. The class has a constructor method that accepts an instance of the RedisFactory and an optional $connection parameter. It also has a connect method that establishes a connection to a queue and returns an instance implementing the Illuminate\Contracts\Queue\Queue interface.

<?php

namespace Illuminate\Queue\Connectors;

use Illuminate\Contracts\Redis\Factory as Redis;
use Illuminate\Queue\RedisQueue;

class RedisConnector implements ConnectorInterface
{
    /**
     * The Redis database instance.
     *
     * @var \Illuminate\Contracts\Redis\Factory
     */
    protected $redis;

    /**
     * The connection name.
     *
     * @var string
     */
    protected $connection;

    /**
     * Create a new Redis queue connector instance.
     *
     * @param  \Illuminate\Contracts\Redis\Factory  $redis
     * @param  string|null  $connection
     * @return void
     */
    public function __construct(Redis $redis, $connection = null)
    {
        $this->redis = $redis;
        $this->connection = $connection;
    }

    /**
     * Establish a queue connection.
     *
     * @param  array  $config
     * @return \Illuminate\Contracts\Queue\Queue
     */
    public function connect(array $config)
    {
        return new RedisQueue(
            $this->redis, $config['queue'],
            $config['connection'] ?? $this->connection,
            $config['retry_after'] ?? 60,
            $config['block_for'] ?? null,
            $config['after_commit'] ?? null,
            $config['migration_batch_size'] ?? -1
        );
    }
}