ngmy/laravel-async-await-bus

Laravel bus 装饰器,允许等待异步命令的响应。

0.2.0 2023-07-28 12:11 UTC

This package is auto-updated.

Last update: 2024-08-28 14:27:39 UTC


README

Latest Stable Version Test Status Lint Status Code Coverage Total Downloads

Laravel bus 装饰器,允许等待异步命令的响应。

安装

composer require ngmy/laravel-async-await-bus

用法

命令类必须实现 ShouldAwaitResponse 接口并使用 Respondable 特性

<?php

namespace App\Commands;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Ngmy\LaravelAsyncAwaitBus\Concerns\Respondable;
use Ngmy\LaravelAsyncAwaitBus\Contracts\ShouldAwaitResponse;

class CreateNewArticleCommand implements ShouldQueue, ShouldAwaitResponse
{
    use InteractsWithQueue, Queueable, SerializesModels, Respondable;

    public function __construct(
        public readonly User $user,
        public readonly string $title,
        public readonly string $body,
        public readonly bool $published,
    ) {
    }
}

处理类必须包含 handle 方法或 __invoke 方法,并且必须使用命令实例的 respond 方法进行响应

<?php

namespace App\Handlers\Commands;

use App\Commands\CreateNewArticleCommand;

class CreateNewArticleCommandHandler
{
    public function handle(CreateNewArticleCommand $command): void
    {
        $article = $command->user->articles()->create([
            'title' => $command->title,
            'body' => $command->body,
            'published' => $command->published,
            'published_at' => $command->published ? now() : null,
        ]);

        $command->respond($article->id);
    }
}

您需要注册命令和处理器的映射。例如,您可以在 AppServiceProvider 类的 boot 方法中注册

use App\Commands\CreateNewArticleCommand;
use App\Handlers\Commands\CreateNewArticleCommandHandler;
use Illuminate\Contracts\Bus\Dispatcher as Bus;

$bus = $this->app->make(Bus::class);
$bus->map([
    CreateNewArticleCommand::class => CreateNewArticleCommandHandler::class,
]);

现在,您可以等待异步命令的响应

<?php

namespace App\Http\Controllers;

use App\Commands\CreateNewArticleCommand;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateNewArticleRequest;
use Illuminate\Contracts\Bus\Dispatcher as Bus;
use Illuminate\Http\RedirectResponse;

class CreateNewArticle extends Controller
{
    public function __invoke(CreateNewArticleRequest $request, Bus $bus): RedirectResponse
    {
        $command = new CreateNewArticleCommand(
            $request->user(),
            $request->string('title'),
            $request->string('body'),
            $request->boolean('published'),
        );
        $id = $bus->dispatch($command);

        return redirect("articles/{$id}");
    }
}

当然,您也可以使用自处理命令

<?php

namespace App\Commands;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Ngmy\LaravelAsyncAwaitBus\Concerns\Respondable;
use Ngmy\LaravelAsyncAwaitBus\Contracts\ShouldAwaitResponse;

class CreateNewArticleCommand implements ShouldQueue, ShouldAwaitResponse
{
    use InteractsWithQueue, Queueable, SerializesModels, Respondable;

    public function __construct(
        public readonly User $user,
        public readonly string $title,
        public readonly string $body,
        public readonly bool $published,
    ) {
    }

    public function handle(): void
    {
        $article = $this->user->articles()->create([
            'title' => $this->title,
            'body' => $this->body,
            'published' => $this->published,
            'published_at' => $this->published ? now() : null,
        ]);

        $this->respond($article->id);
    }
}

变更日志

请参阅变更日志

许可证

Laravel Async Await Bus 是开源软件,受MIT 许可证许可。