承接 thtg88/journalism 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thtg88/journalism

Composer 安装命令:

composer require thtg88/journalism

包简介

Journalism is a Laravel package providing a simple way to log data to your database

README 文档

README

Journalism is a Laravel package providing a simple way to log data to your database.

Installation

composer require thtg88/journalism

You can publish the configuration file and views by running:

php artisan vendor:publish --provider="Thtg88\Journalism\JournalismServiceProvider"

Usage

Journalism is particularly useful when tracking changes to models.

You can therefore apply it to either a generic model observer for every model event (create, update, and destroy) or, if you use the repository pattern to all your CRUD methods to track what, when, and by whom certain changes have occurred.

Make sure you register the helper as a singleton in your AppServiceProvider:

// app/Providers/AppServiceProvider.php

use Thtg88\Journalism\Helpers\JournalEntryHelper;

public function register(): void
{
    $this->app->singleton(JournalEntryHelper::class, static function ($app) {
        return $app->make(JournalEntryHelper::class);
    });
}

Or you can simply use it in whichever class you prefer:

use Thtg88\Journalism\Helpers\JournalEntryHelper;

(new JournalEntryHelper())->createJournalEntry('create', $model, ['foo' => 'bar']);

Using Model Observers

For more documentation on model observer, see the Laravel docs

First, create a base model observer:

<?php

// app/Observers/Observer.php

namespace App\Observers;

use Illuminate\Database\Eloquent\Model;
use Thtg88\Journalism\Helpers\JournalEntryHelper;
use Thtg88\Journalism\Models\JournalEntry;

abstract class Observer
{
    /**
     * Handle the Model "created" event.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function created(Model $model): void
    {
        if (config('journalism.enabled') === false) {
            return;
        }

        // Create journal entry only if not creating journal entry i.e. infinite recursion
        if ($model instanceof JournalEntry) {
            return;
        }

        app(JournalEntryHelper::class)->createJournalEntry(
            'create',
            $model,
            $model->toArray(),
        );
    }

    /**
     * Handle the Model "updated" event.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function updated(Model $model): void
    {
        if (config('journalism.enabled') === false) {
            return;
        }

        // Create journal entry only if not creating journal entry i.e. infinite recursion
        if ($model instanceof JournalEntry) {
            return;
        }

        app(JournalEntryHelper::class)->createJournalEntry(
            'update',
            $model,
            $model->toArray(),
        );
    }

    /**
     * Handle the Model "deleted" event.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function deleted(Model $model): void
    {
        if (config('journalism.enabled') === false) {
            return;
        }

        // Create journal entry only if not creating journal entry i.e. infinite recursion
        if ($model instanceof JournalEntry) {
            return;
        }

        app(JournalEntryHelper::class)->createJournalEntry('delete', $model);
    }

    /**
     * Handle the Model "forceDeleted" event.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function forceDeleted(Model $model): void
    {
        if (config('journalism.enabled') === false) {
            return;
        }

        // Create journal entry only if not creating journal entry i.e. infinite recursion
        if ($model instanceof JournalEntry) {
            return;
        }

        app(JournalEntryHelper::class)->createJournalEntry('delete', $model);
    }
}

Then, create an actual model observer, extending your base one:

<?php

// app/Observers/UserObserver.php

namespace App\Observers;

class UserObserver extends Observer
{
}

Register it in EventServiceProvider:

use App\Models\User;
use App\Observers\UserObserver;

public function boot()
{
    User::observe(UserObserver::class);
}

Now you can perform a database operation using the user model. A database row should appear in the journal_entries table!

Using the Repository Pattern

Coming soon!

License

Journalism is open-sourced software licensed under the MIT license.

Security Vulnerabilities

If you discover a security vulnerability within Journalism, please send an e-mail to Marco Marassi at security@marco-marassi.com. All security vulnerabilities will be promptly addressed.

thtg88/journalism 适用场景与选型建议

thtg88/journalism 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.54k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 thtg88/journalism 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 thtg88/journalism 我们能提供哪些服务?
定制开发 / 二次开发

基于 thtg88/journalism 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 12.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-21