panoscape/tracker

此包已被弃用且不再维护。作者建议使用 panoscape/history 包。

Laravel 的 Eloquent CRUD 记录追踪

1.0.0 2016-11-21 02:19 UTC

This package is not auto-updated.

Last update: 2022-02-01 13:03:04 UTC


README

Laravel 的 Eloquent CRUD 记录追踪

安装

您可以通过 composer 安装此包

composer require panoscape/tracker

首先,注册服务提供者

config/app.php

'providers' => [
    ...
    Panoscape\Tracker\TrackerServiceProvider::class,
];

接下来,您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Panoscape\Tracker\TrackerServiceProvider" --tag=config

迁移

php artisan vendor:publish --provider="Panoscape\Tracker\TrackerServiceProvider" --tag=migrations
php artisan migrate

发布本地化

php artisan vendor:publish --provider="Panoscape\Tracker\TrackerServiceProvider" --tag=translations

使用方法

Panoscape\Tracker\Context 特性添加到您想追踪的任何模型中。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Panoscape\Tracker\Context;

class Project extends Model
{
    use Context;

    public function getContextLabel()
    {
        return $this->display_name;
    }

    ...
}

请记住,您需要实现特性中的抽象 getContextLabel 方法。这将添加指定的名称值到记录中。

创建新项目 project_001

上下文

Context 是一个在 CURD 操作中被追踪的模型。例如,在 User 001 删除了 Project 001 中,Project 001 是我们在这里讨论的 Context

要获取上下文化模型的全部记录

$project->records();

或通过动态属性

$project->records;

您可以根据需要更改 records 的名称(或者为了解决方法名冲突)

use Context { records as logs; }

代理

Agent 是执行动作的用户。例如,在 User 001 删除了 Project 001 中,User 001Agent

记录中的代理始终从 Auth 获取,即执行动作时的授权用户。

您的应用程序可能有多种用户类型,并且此包对此有很好的处理。

要能够从代理获取相关记录,您只需要将 Panoscape\Tracker\Agent 特性添加到该模型。

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Panoscape\Tracker\Agent;

class User extends Authenticatable
{
    use SoftDeletes, Notifiable, Agent;

    ...
}

获取代理执行的记录

$user->records();

或通过动态属性

$user->records;

您可以根据需要更改 records 的名称(或者为了解决方法名冲突)

use Agent { records as logs; }

如果在不认证的情况下追踪,则记录的代理可能为 null

记录

记录的默认结构

字段 类型 可为空
id 大无符号整数 N
context_id 无符号整数 N
context_type 字符串 N
agent_id 无符号整数 Y
agent_type 字符串 Y
message 字符串 N
meta 文本 Y
performed_at 时间戳 N

message 字段是动作信息的简要。示例

创建新项目 my_project

更新项目 my_project

删除项目 my_project

恢复项目 my_project

meta 字段是模型修改属性的 json 上下文。它仅在使用 更新 ... 时可用。

performed_at 是一个时间戳,表示记录创建的时刻,也就是动作执行的时刻。

获取记录的上下文

$record->context();

或通过动态属性

$record->context;

检查记录的代理

$record->hasAgent();

获取记录的代理

$record->agent();

或通过动态属性

$record->agent;

Record 上的 meta 属性被反序列化为一个数组,其中包含修改历史

$record->meta;
[
    ['key' => 'name', 'old' => 'myName', 'new' => 'myNewName'],
    ['key' => 'age', 'old' => 10, 'new' => 100],
    ...
]

本地化

您可以将记录输出本地化

这是默认的本地化

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Tracker Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines are used across application for various
    | messages that we need to display to the user. You are free to modify
    | these language lines according to your application's requirements.
    |
    */

    'created' => 'Created new :context :name',

    'updating' => 'Updating :context :name',

    'deleting' => 'Deleting :context :name',

    'restored' => 'Restored :context :name',
];

简体中文

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Tracker Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines are used across application for various
    | messages that we need to display to the user. You are free to modify
    | these language lines according to your application's requirements.
    |
    */

    'created' => '创建新的:context :name',

    'updating' => '更新:context :name',

    'deleting' => '删除:context :name',

    'restored' => '恢复:context :name',
];

:context 默认是类的基名。

要本地化它,可以在本地化文件中添加一个新的语言行,键是 基名使用蛇形命名法

<?php

return [

    ...

    'project' => '项目',
    'component_template' => '组件模板',
];

:namegetContextLabel 提供,我们上面已经提到过。

配置

以下是默认配置

<?php

return [

    /*
    |--------------------------------------------------------------
    | Literally
    |--------------------------------------------------------------
    |
    |
    */
    'enabled' => true,

    /*
    |--------------------------------------------------------------
    | Record table name
    |--------------------------------------------------------------
    |
    |
    */
    'records_table' => 'tracker_records',

    /*
    |--------------------------------------------------------------
    | Operatiopn whitelist
    |--------------------------------------------------------------
    |
    | Operations in this array will be recorded.
    | Available operations are: created, updating, deleting, restored
    |
    */
    'operations' => [
        'created', 'updating', 'deleting', 'restored',
    ],

    /*
    |--------------------------------------------------------------
    | Agent blacklist
    |--------------------------------------------------------------
    |
    | Operations performed by agents in this array will NOT be recorded.
    | Please add the whole class names. Example: \App\User
    | Use 'nobody' to bypass unauthenticated operations
    |
    */
    'agent_ignore' => [
        
    ],

    /*
    |--------------------------------------------------------------
    | Enabled when application running in console
    |--------------------------------------------------------------
    |
    | When application is running in console(include seeding)
    |
    */
    'console' => false,

    /*
    |--------------------------------------------------------------
    | Enabled when application running in unit tests
    |--------------------------------------------------------------
    |
    | When application is running unit tests
    |
    */
    'unit_test' => false,

    /*
    |--------------------------------------------------------------
    | Enviroments blacklist
    |--------------------------------------------------------------
    |
    | When application's environment is in the list, tracker will be disabled
    |
    */
    'env_ignore' => [
        
    ],
    
];

要更改持有记录的表,设置 records_table。记得之后更新您的迁移。

要应用过滤器到操作,设置 operations。只有列表中的操作会被追踪。

要应用过滤器到代理,设置 agent_ignore。列表中的代理执行的操作将 不会 被追踪。

如果您想绕过未认证的操作,将 'nobody' 添加到 agent_ignore 中。

如果您需要绕过特定的代理类型,将其类名添加到 agent_ignore 中。