inisiatif/laravel-event-history

0.5.0 2023-08-31 06:37 UTC

README

Latest Version on Packagist PHPUnit Laravel Pint Psalm Total Downloads

本包用于记录事件,如“申请已批准”等。

安装

您可以通过Composer安装此包

composer require inisiatif/laravel-event-history

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="event-history-migrations"
php artisan migrate

用法

准备

AppServiceProviderboot方法中添加以下代码以注册用户模型

use Inisiatif\EventHistory\EventHistories;

EventHistories::useUserModelClassName(User::class);

然后,所有具有历史记录的模型都必须实现接口Inisiatif\EventHistory\Concerns\HasEventHistories,我们还可以使用默认实现,即trait Inisiatif\EventHistory\InteractWithEventHistories

use Illuminate\Database\Eloquent\Model;
use Inisiatif\EventHistory\Concerns\HasEventHistories;
use Inisiatif\EventHistory\InteractWithEventHistories;

class ExampleModel extends Model implements HasEventHistories {
    use InteractWithEventHistories;
}

使用监听器

您可以使用Inisiatif\EventHistory\RecordEventHistoryListener来记录每个事件的history,需要确保的是,您的事件必须实现Inisiatif\EventHistory\Concerns\EventHistoryAwareInterface

<?php

declare(strict_types=1);

namespace Inisiatif\EventHistory\Concerns;

interface EventHistoryAwareInterface
{
    public function getModelAwareHistories(): mixed;

    public function getHistoryDescription(): string;
}

方法getModelAwareHistories必须返回一个实现了Inisiatif\EventHistory\Concerns\HasEventHistories接口的模型或集合。在底层,监听器将调用作业Inisiatif\EventHistory\Jobs\NewEventHistoryJob

测试

composer test

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全漏洞

请查看我们的安全策略以了解如何报告安全漏洞。

鸣谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。