quix-labs/laravel-hook-system

为 Laravel 添加钩子系统

1.1.2 2024-06-20 16:46 UTC

This package is auto-updated.

Last update: 2024-09-08 08:58:22 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

quix-labs/laravel-hook-system 包为 Laravel 提供了一个钩子系统。

此系统允许拦截和操作您的应用程序中的特定操作。

要求

  • PHP >= 8.1
  • Laravel 10.x|11.x

安装

您可以通过 Composer 安装此包

composer require quix-labs/laravel-hook-system

钩子使用

创建钩子

钩子是一个扩展 QuixLabs\LaravelHookSystem\Hook 的类

class GetString extends \QuixLabs\LaravelHookSystem\Hook
{
    public function __construct(public string &$string)
    {
    }
}

创建完全可缓存的钩子

完全可缓存的钩子在缓存生成期间执行拦截器,并在运行时防止其执行。拦截器可以绕过此行为。

class GetString extends \QuixLabs\LaravelHookSystem\Hook implements \QuixLabs\LaravelHookSystem\Interfaces\FullyCacheable
{
    public function __construct(public string &$string)
    {
    }
    
    public static function initialInstance(): static
    {
        $string = 'initial-state';
        return new static($string);
    }
}

注册钩子

在您的 ServiceProvider 的 register 方法中

use Workbench\App\Hooks\GetString;

class YourProvider{
    public function register()
    {
        \QuixLabs\LaravelHookSystem\HookRegistry::registerHook(GetString::class);
    }
}

执行钩子

要执行钩子,QuixLabs\LaravelHookSystem\Hook 实现了静态 send 方法

class YourController
{
    public function index()
    {
        $string = "";
        \Workbench\App\Hooks\GetString::send($string);
        return $string;
    }
}

拦截器使用

创建拦截器

拦截器是一个具有通过 #[Intercept] 属性拦截的静态方法的类

use Illuminate\Support\Str;
use QuixLabs\LaravelHookSystem\Enums\ActionWhenMissing;
use QuixLabs\LaravelHookSystem\Utils\Intercept;

class AppendRandomString
{
    #[Intercept(\Workbench\App\Hooks\GetString::class)]
    public static function appendRandomString(GetString $hook): void
    {
        $hook->string .= Str::random(16);
    }
    
    # You can specify action when hook not found (THROW_ERROR, SKIP or REGISTER_HOOK)
    #[Intercept(\Workbench\App\Hooks\GetString::class, ActionWhenMissing::THROW_ERROR)]
    public static function appendStringRequired(GetString $hook): void
    {
        $hook->string .= Str::random(16);
    }
    
    # You can also specify execution priority using third argument
    #[Intercept(\Workbench\App\Hooks\GetString::class, ActionWhenMissing::SKIP, 100)]
    public static function appendRandomStringAtTheEnd(GetString $hook): void
    {
        $hook->string .= Str::random(16);
    }
    # You can prevent full cache generation (useful if the interceptor depends on context request)
    #[Intercept(\Workbench\App\Hooks\GetString::class, ActionWhenMissing::SKIP, 100, false)]
    public static function appendRandomStringAtTheEnd(GetString $hook): void
    {
        $hook->string .= Str::random(16);
    }
}

注册拦截器

在您的 ServiceProvider 的 boot 方法中

class YourProvider{
    public function boot()
    {
        \QuixLabs\LaravelHookSystem\HookRegistry::registerInterceptor(\App\Interceptors\AppendRandomString::class);
    }
}

Artisan 命令

该包添加了三个 Artisan 命令来管理钩子

  • hooks:status : 显示钩子和拦截器的状态。
  • hooks:cache : 缓存钩子和拦截器。
  • hooks:clear : 清除钩子和拦截器缓存。

计划中的功能

以下功能计划在未来实现

  • 使用 app() 实例化拦截器类(添加对依赖注入容器的支持)。

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

鸣谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件