Revisions

Outside HTTP

Render resources for webhooks, jobs and commands in the revision of their recipient.

Webhooks, jobs and console commands have no request to read a revision from. Choose it explicitly.

Render in a revision

using() makes a revision current for the duration of a closure, and returns what the closure returns:

$revision = $account->api_revision ?? ApiRevision::latest();

ApiRevision::using($revision, function () use ($account, $order): void {
    Http::post($account->webhook_url, [
        'type' => 'order.updated',
        'data' => OrderResource::make($order)->resolve(),
    ]);
});

Render inside the closure. A resource returned from it and serialized later renders in the outer revision.

The current revision is, in order: the innermost using(), else the revision the middleware resolved, else the configured default when it is a fixed revision, else the head.

Queued jobs

A queued job implementing Laravel\Queue\CapturesRevision stores the revision current at dispatch, and runs handle(), its middleware and failed() in it.

app/Jobs/SendOrderWebhook.php
use Ark4ne\ApiRevisions\Laravel\Queue\CapturesRevision;

final class SendOrderWebhook implements ShouldQueue, CapturesRevision
{
    use Dispatchable;
    use Queueable;

    public function __construct(
        public readonly string $url,
        public readonly Order $order,
    ) {}

    public function handle(): void
    {
        Http::post($this->url, [
            'type' => 'order.updated',
            'data' => OrderResource::make($this->order)->resolve(),
        ]);
    }
}
$revision = $account->api_revision ?? ApiRevision::latest();

ApiRevision::using($revision, function () use ($account, $order): void {
    SendOrderWebhook::dispatch($account->webhook_url, $order);
});
Dispatch inside a block body, never from an arrow function: fn () => SendOrderWebhook::dispatch(...) returns the PendingDispatch, which is pushed when it is destroyed, after using() has restored the outer revision.
  • Only ShouldQueue jobs are captured. Send a queued notification, mailable or listener from a marked job.
  • A job captured in a revision you have since retired fails with UnknownRevisionException.