fereydooni/laravel-observable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

fereydooni/laravel-observable

Composer 安装命令:

composer require fereydooni/laravel-observable

包简介

A robust and feature-rich observation system for dynamically observing Laravel objects

README 文档

README

A robust and feature-rich observation system for dynamically observing Laravel objects (models, events, or other objects) and triggering callback functions using PHP attributes.

Installation

You can install the package via composer:

composer require fereydooni/laravel-observable

After installing, publish the configuration file:

php artisan vendor:publish --provider="Fereydooni\LaravelObservable\DynamicObserverServiceProvider"

Run the migrations:

php artisan migrate

Usage

Using Attributes

Observe Attribute

Use the Observe attribute to define observation rules:

use Fereydooni\LaravelObservable\Attributes\Observe;

#[Observe(target: 'App\Models\User', event: 'created', callback: 'App\Services\Logger@log', priority: 10)]
class UserObserver
{
    public function handle($model) 
    {
        // Custom logic when User model is created
    }
}

Observable Attribute

Mark classes or methods as observable:

use Fereydooni\LaravelObservable\Attributes\Observable;

#[Observable(name: 'user_activity')]
class UserService
{
    public function createUser($data)
    {
        // This method can be observed
        return User::create($data);
    }
}

Using the ObservationManager

use Fereydooni\LaravelObservable\ObservationManager;

// Get an instance of the manager
$manager = app(ObservationManager::class);

// Dynamically observe a model
$manager->observe('App\Models\User', 'updated', function($model) {
    \Log::info('User updated: ' . $model->id);
});

// Query observation logs
$logs = $manager->getObservationLogs('App\Models\User');

// Enable/disable specific observation
$manager->toggleObservation('App\Models\User', 'created', false); // Disable
$manager->toggleObservation('App\Models\User', 'created', true);  // Enable

Conditional Observation

use Fereydooni\LaravelObservable\Attributes\Observe;

#[Observe(
    target: 'App\Models\User', 
    event: 'created', 
    callback: 'App\Services\AdminLogger@log',
    condition: 'isAdmin'
)]
class AdminUserObserver
{
    public function isAdmin($model)
    {
        return $model->role === 'admin';
    }
    
    public function handle($model) 
    {
        // This will only trigger for admin users
    }
}

Configuration

The package comes with a configuration file that allows you to:

  • Enable/disable observation globally
  • Set default priority for observations
  • Configure logging backend (database or file)
  • Enable/disable conditional observation

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-24