master

laravel/framework

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

PendingDispatch.php

TLDR

The provided file, PendingDispatch.php, is a class file that is part of the Illuminate\Foundation\Bus namespace. This file contains the definition for the PendingDispatch class, which is responsible for managing the dispatching of jobs in Laravel's queue system.

Methods

__construct

The __construct method is the class constructor for the PendingDispatch class. It takes a single parameter $job and initializes the protected $job property with its value.

onConnection

The onConnection method sets the desired connection for the job by calling the onConnection method on the $job property.

onQueue

The onQueue method sets the desired queue for the job by calling the onQueue method on the $job property.

allOnConnection

The allOnConnection method sets the desired connection for the chain by calling the allOnConnection method on the $job property.

allOnQueue

The allOnQueue method sets the desired queue for the chain by calling the allOnQueue method on the $job property.

delay

The delay method sets the desired delay in seconds for the job by calling the delay method on the $job property.

afterCommit

The afterCommit method indicates that the job should be dispatched after all database transactions have committed by calling the afterCommit method on the $job property.

beforeCommit

The beforeCommit method indicates that the job should not wait until database transactions have been committed before dispatching by calling the beforeCommit method on the $job property.

chain

The chain method sets the jobs that should run if this job is successful by calling the chain method on the $job property.

afterResponse

The afterResponse method indicates that the job should be dispatched after the response is sent to the browser by setting the $afterResponse property to true.

shouldDispatch

The shouldDispatch method determines if the job should be dispatched. If the job does not implement the ShouldBeUnique interface, it returns true. Otherwise, it creates a new UniqueLock instance and calls its acquire method with the job to determine if it should be dispatched.

__call

The __call method dynamically proxies methods to the underlying job. It receives the $method and $parameters arguments and calls the corresponding method on the $job property.

__destruct

The __destruct method handles the destruction of the PendingDispatch object. It determines whether the job should be dispatched by calling the shouldDispatch method. If it should be dispatched, it checks if the $afterResponse property is true and then dispatches the job either immediately or after the response is sent to the browser using the Laravel Dispatcher class.

<?php

namespace Illuminate\Foundation\Bus;

use Illuminate\Bus\UniqueLock;
use Illuminate\Container\Container;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Queue\ShouldBeUnique;

class PendingDispatch
{
    /**
     * The job.
     *
     * @var mixed
     */
    protected $job;

    /**
     * Indicates if the job should be dispatched immediately after sending the response.
     *
     * @var bool
     */
    protected $afterResponse = false;

    /**
     * Create a new pending job dispatch.
     *
     * @param  mixed  $job
     * @return void
     */
    public function __construct($job)
    {
        $this->job = $job;
    }

    /**
     * Set the desired connection for the job.
     *
     * @param  string|null  $connection
     * @return $this
     */
    public function onConnection($connection)
    {
        $this->job->onConnection($connection);

        return $this;
    }

    /**
     * Set the desired queue for the job.
     *
     * @param  string|null  $queue
     * @return $this
     */
    public function onQueue($queue)
    {
        $this->job->onQueue($queue);

        return $this;
    }

    /**
     * Set the desired connection for the chain.
     *
     * @param  string|null  $connection
     * @return $this
     */
    public function allOnConnection($connection)
    {
        $this->job->allOnConnection($connection);

        return $this;
    }

    /**
     * Set the desired queue for the chain.
     *
     * @param  string|null  $queue
     * @return $this
     */
    public function allOnQueue($queue)
    {
        $this->job->allOnQueue($queue);

        return $this;
    }

    /**
     * Set the desired delay in seconds for the job.
     *
     * @param  \DateTimeInterface|\DateInterval|int|null  $delay
     * @return $this
     */
    public function delay($delay)
    {
        $this->job->delay($delay);

        return $this;
    }

    /**
     * Indicate that the job should be dispatched after all database transactions have committed.
     *
     * @return $this
     */
    public function afterCommit()
    {
        $this->job->afterCommit();

        return $this;
    }

    /**
     * Indicate that the job should not wait until database transactions have been committed before dispatching.
     *
     * @return $this
     */
    public function beforeCommit()
    {
        $this->job->beforeCommit();

        return $this;
    }

    /**
     * Set the jobs that should run if this job is successful.
     *
     * @param  array  $chain
     * @return $this
     */
    public function chain($chain)
    {
        $this->job->chain($chain);

        return $this;
    }

    /**
     * Indicate that the job should be dispatched after the response is sent to the browser.
     *
     * @return $this
     */
    public function afterResponse()
    {
        $this->afterResponse = true;

        return $this;
    }

    /**
     * Determine if the job should be dispatched.
     *
     * @return bool
     */
    protected function shouldDispatch()
    {
        if (! $this->job instanceof ShouldBeUnique) {
            return true;
        }

        return (new UniqueLock(Container::getInstance()->make(Cache::class)))
                    ->acquire($this->job);
    }

    /**
     * Dynamically proxy methods to the underlying job.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return $this
     */
    public function __call($method, $parameters)
    {
        $this->job->{$method}(...$parameters);

        return $this;
    }

    /**
     * Handle the object's destruction.
     *
     * @return void
     */
    public function __destruct()
    {
        if (! $this->shouldDispatch()) {
            return;
        } elseif ($this->afterResponse) {
            app(Dispatcher::class)->dispatchAfterResponse($this->job);
        } else {
            app(Dispatcher::class)->dispatch($this->job);
        }
    }
}