hackeresq/laravel-watcher

监视您的 Laravel 表单请求中的特定键并执行特定功能

v1.0.1 2021-03-13 04:34 UTC

This package is auto-updated.

Last update: 2024-09-08 06:47:28 UTC


README

Latest Version on Packagist Total Downloads Software License

设置监视器以监视 Laravel 表单请求中的特定键。如果这些键存在,则执行任意函数(s)。

安装

此软件包可用于 Laravel 5.4 或更高版本。

您可以通过 composer 安装此软件包

composer require hackeresq/laravel-watcher

Watcher 是一个特质,可以添加到您的 Laravel FormRequests(或者您可以使用包含的基类 FormRequest)。要开始使用 setWatcher() 方法,您必须通过将特质添加到您的 FormRequest 或在控制器中使用基类 WatcherRequest 来 'use' Watcher 特质。

自定义 FormRequest 实现的示例

<?php

namespace App\Http\Requests;

use HackerESQ\Watcher\Watcher;
use Illuminate\Foundation\Http\FormRequest;

class YourCustomFormRequest extends FormRequest
{
    use Watcher;
    
    // ...
}

或者,如果您没有使用自定义 FormRequests,您可以使用提供的 WatcherRequest,该请求已经添加了特质。这是控制器方法应该看起来像什么

    /**
     * Update the specified resource in storage.
     *
     * @param  HackerESQ\Watcher\Requests\WatcherRequest $request
     * @return \Illuminate\Http\Response
     */
    public function update(WatcherRequest $request)
    {   
        // ...
    }

成功! laravel-watcher 现已安装!

使用方法

一旦您将特质添加到您的自定义 FormRequest 或将 WatcherRequest 添加到您的控制器方法中,您将在请求上获得一个新的 setWatcher() 方法。这允许您设置您的监视器。

基本用法

setWatcher() 方法的基本用法是传递一个数组,其中 "触发" 是键,如下所示

        $request->setWatcher([
            'invoice_start_num_changed' => [
                'action' => fn($context) => DB::statement("ALTER TABLE `invoices` AUTO_INCREMENT = ".(int)$context->request->invoice_start_num),
            ],
        ]);

您将注意到键是 "监视" 触发器。如果 'invoice_start_num_changed' 存在(并且不是 falsy),则定义的 'action'(一个匿名函数)将被调用。

移除键

您可以选择通过传递 'removeKey' 属性从请求中删除触发器(例如,如果您正在将请求传递到其他地方并希望清理它),如下所示

        $request->setWatcher([
            'invoice_start_num_changed' => [
                'action' => fn($context) => DB::statement("ALTER TABLE `invoices` AUTO_INCREMENT = ".(int)$context->request->invoice_start_num),
                'removeKey' => true,
            ],
        ]);

传递上下文

您将注意到您可以将 $context 传递给匿名函数。此 $context 对象包含触发器名称(在 $context->trigger 对象中)和原始表单请求(在 $context->request 对象中)。

即使请求为空也触发

最后,如果您希望在触发器为 null 或空时触发操作,您可以使用 allowEmpty 选项。只需在配置触发器时将 allowEmpty 设置为 true

        $request->setWatcher([
            'empty_field_contains_nothing' => [
                'action' => fn($context) => Log::info($context),
                'allowEmpty' => true,
            ],
        ]);

调用多个函数

如果您不想创建和调用中间函数,并且希望在监视器内部调用多个函数,您可以选择使用标准匿名函数(而不是短箭头函数),如下所示

        $request->setWatcher([
            'invoice_start_num_changed' => [
                'action' => function($context) { 
                      Log::info($context); 
                      DB::statement("ALTER TABLE `invoices` AUTO_INCREMENT = ".(int)$context->request->invoice_start_num);
                      // do other stuff
                 },
                'removeKey' => true,
            ],
        ]);

完整示例

SettingsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use HackerESQ\Watcher\Requests\WatcherRequest;

class SettingsController extends Controller
{
    // ... other controller methods
    
    /**
     * Update the specified resource in storage.
     *
     * @param  \HackerESQ\Watcher\Requests\WatcherRequest $request
     * @return \Illuminate\Http\Response
     */
    public function update(WatcherRequest $request)
    {   
        $request->setWatcher([
            'invoice_start_num_changed' => [
                'action' => fn($context) => DB::statement("ALTER TABLE `invoices` AUTO_INCREMENT = ".(int)$context->request->invoice_start_num),
                'removeKey' => true,
            ],
            'should_log_action' => [
                'action' => fn($context) => Log::info($context),
            ],
        ]);

        Settings::set($request->all());

        return $request;
    }
}

最后

测试

您可以使用 composer test 命令运行测试。

贡献

如果您想做出贡献,请随时创建分支并提交拉取请求。

错误报告

如果在 GitHub 上发现某些内容损坏,请提出问题。