master

laravel/framework

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

Manager.php

TLDR

The Manager.php file in the Illuminate\Queue\Capsule namespace is a class that acts as a manager for the queue system. It allows for the setup and configuration of the queue manager instance, as well as the registration of connectors. It provides methods for interacting with the queue, such as pushing jobs onto the queue, scheduling jobs for later execution, and retrieving a connection instance from the queue manager.

Methods

__construct

This method is the constructor of the Manager class. It sets up the container, configures the default queue configuration options, creates a new queue manager instance, and registers the default connectors.

setupDefaultConfiguration

This method sets up the default queue configuration options. It sets the default queue to 'default'.

setupManager

This method initializes the queue manager instance using the container.

registerConnectors

This method registers the default connectors that the component ships with. It creates a new QueueServiceProvider instance and uses it to register connectors with the queue manager.

connection

This static method returns a connection instance from the global manager, based on the specified connection name. If no connection name is provided, it uses the default connection.

push

This static method pushes a new job onto the queue. It takes the job class name, the data for the job, an optional queue name, and an optional connection name.

bulk

This static method pushes an array of jobs onto the queue. It takes an array of job class names, the data for the jobs, an optional queue name, and an optional connection name.

later

This static method pushes a new job onto the queue after a specified delay. It takes the delay (either a DateTimeInterface, a DateInterval, or an integer representing seconds), the job class name, the data for the job, an optional queue name, and an optional connection name.

getConnection

This method returns a registered connection instance from the queue manager, based on the specified connection name. If no connection name is provided, it returns the default connection.

addConnection

This method registers a connection with the queue manager. It takes an array of configuration options and an optional connection name.

getQueueManager

This method returns the queue manager instance.

__call

This magic method passes dynamically called instance methods to the queue manager instance, allowing for method chaining.

__callStatic

This magic method passes dynamically called static methods to the default connection instance, allowing for static method chaining.

Classes

None

<?php

namespace Illuminate\Queue\Capsule;

use Illuminate\Container\Container;
use Illuminate\Queue\QueueManager;
use Illuminate\Queue\QueueServiceProvider;
use Illuminate\Support\Traits\CapsuleManagerTrait;

/**
 * @mixin \Illuminate\Queue\QueueManager
 * @mixin \Illuminate\Contracts\Queue\Queue
 */
class Manager
{
    use CapsuleManagerTrait;

    /**
     * The queue manager instance.
     *
     * @var \Illuminate\Queue\QueueManager
     */
    protected $manager;

    /**
     * Create a new queue capsule manager.
     *
     * @param  \Illuminate\Container\Container|null  $container
     * @return void
     */
    public function __construct(Container $container = null)
    {
        $this->setupContainer($container ?: new Container);

        // Once we have the container setup, we will set up the default configuration
        // options in the container "config" bindings. This'll just make the queue
        // manager behave correctly since all the correct bindings are in place.
        $this->setupDefaultConfiguration();

        $this->setupManager();

        $this->registerConnectors();
    }

    /**
     * Setup the default queue configuration options.
     *
     * @return void
     */
    protected function setupDefaultConfiguration()
    {
        $this->container['config']['queue.default'] = 'default';
    }

    /**
     * Build the queue manager instance.
     *
     * @return void
     */
    protected function setupManager()
    {
        $this->manager = new QueueManager($this->container);
    }

    /**
     * Register the default connectors that the component ships with.
     *
     * @return void
     */
    protected function registerConnectors()
    {
        $provider = new QueueServiceProvider($this->container);

        $provider->registerConnectors($this->manager);
    }

    /**
     * Get a connection instance from the global manager.
     *
     * @param  string|null  $connection
     * @return \Illuminate\Contracts\Queue\Queue
     */
    public static function connection($connection = null)
    {
        return static::$instance->getConnection($connection);
    }

    /**
     * Push a new job onto the queue.
     *
     * @param  string  $job
     * @param  mixed  $data
     * @param  string|null  $queue
     * @param  string|null  $connection
     * @return mixed
     */
    public static function push($job, $data = '', $queue = null, $connection = null)
    {
        return static::$instance->connection($connection)->push($job, $data, $queue);
    }

    /**
     * Push a new an array of jobs onto the queue.
     *
     * @param  array  $jobs
     * @param  mixed  $data
     * @param  string|null  $queue
     * @param  string|null  $connection
     * @return mixed
     */
    public static function bulk($jobs, $data = '', $queue = null, $connection = null)
    {
        return static::$instance->connection($connection)->bulk($jobs, $data, $queue);
    }

    /**
     * Push a new job onto the queue after (n) seconds.
     *
     * @param  \DateTimeInterface|\DateInterval|int  $delay
     * @param  string  $job
     * @param  mixed  $data
     * @param  string|null  $queue
     * @param  string|null  $connection
     * @return mixed
     */
    public static function later($delay, $job, $data = '', $queue = null, $connection = null)
    {
        return static::$instance->connection($connection)->later($delay, $job, $data, $queue);
    }

    /**
     * Get a registered connection instance.
     *
     * @param  string|null  $name
     * @return \Illuminate\Contracts\Queue\Queue
     */
    public function getConnection($name = null)
    {
        return $this->manager->connection($name);
    }

    /**
     * Register a connection with the manager.
     *
     * @param  array  $config
     * @param  string  $name
     * @return void
     */
    public function addConnection(array $config, $name = 'default')
    {
        $this->container['config']["queue.connections.{$name}"] = $config;
    }

    /**
     * Get the queue manager instance.
     *
     * @return \Illuminate\Queue\QueueManager
     */
    public function getQueueManager()
    {
        return $this->manager;
    }

    /**
     * Pass dynamic instance methods to the manager.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->manager->$method(...$parameters);
    }

    /**
     * Dynamically pass methods to the default connection.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public static function __callStatic($method, $parameters)
    {
        return static::connection()->$method(...$parameters);
    }
}