alexssander-cusin/panic-control-laravel

这是我的包 panic-control-laravel

v1.3.3 2024-04-20 13:59 UTC

README

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

安装

您可以通过 composer 安装此包

composer require alexssander-cusin/panic-control-laravel

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

php artisan vendor:publish --tag="panic-control-laravel-config"

以下是已发布的配置文件的内容

return [
    /** 
     *--------------------------------------------------------------------------
     * Set up what store will be used
     *--------------------------------------------------------------------------
    */
    
    'default' => 'database',
    
    'drivers' => [
        'database' => [
            /**
             *--------------------------------------------------------------------------
             * Defines which registered connections
             *--------------------------------------------------------------------------
             * The storage listed in /config/database.php should be used
             */
            
            'connection' => config('database.default'),

            /** 
             *--------------------------------------------------------------------------
             * Define the table name will be created in database
             *--------------------------------------------------------------------------
            */
            
            'table' => 'panic_controls',
        ],
        'file' => [
            /** 
             *--------------------------------------------------------------------------
             * Defines which registered disk
             *--------------------------------------------------------------------------
             * The storage listed in /config/filesystem.php should be used
             * 
             * Supported Drivers: "local", "ftp", "sftp", "s3"
            */
            
            'disk' => config('filesystems.default'),
            
            /** 
             *--------------------------------------------------------------------------
             * Defines the name of the file that will be created
             *--------------------------------------------------------------------------
            */
            
            'path' => 'panic-control.json',
        ],
        'endpoint' => [
            /**
             *--------------------------------------------------------------------------
             * Defines the URL of the endpoint
             *--------------------------------------------------------------------------
             */
            'url' => 'https:///panic-control.json',
        ],
    ],
    'cache' => [
        /** 
         *--------------------------------------------------------------------------
         * Activates the cache usage for the panic controls
         *--------------------------------------------------------------------------
        */
        
        'enabled' => true,
        
        /** 
         *--------------------------------------------------------------------------
         * Defines what cache store should be used
         *--------------------------------------------------------------------------
         * The storage listed in /config/cache.php should be used
         * 
         * Supported drivers: "apc", "array", "database", "file",
         *      "memcached", "redis", "dynamodb", "octane", "null"
        */
        
        'store' => env('CACHE_DRIVER', 'file'),

        /**
         *--------------------------------------------------------------------------
         * Cache Key Prefix
         *--------------------------------------------------------------------------
         *
         * When utilizing the APC, database, memcached, Redis, or DynamoDB cache
         * stores there might be other applications using the same cache. For
         * that reason, you may prefix every cache key to avoid collisions.
         *
        */

        'key' => 'panic-control',

        /**
         *--------------------------------------------------------------------------
         * Sets the time the cache will expire
         *--------------------------------------------------------------------------
        */

        'ttl' => 60,
    ],

    /**
     *--------------------------------------------------------------------------
     * List custom rules
     *--------------------------------------------------------------------------
    */

    'rules' => [
        'route-name' => PanicControl\Rules\RouteName::class,
        'url-path' => PanicControl\Rules\UrlPath::class,
        'sampling' => PanicControl\Rules\Sampling::class,
        'user' => PanicControl\Rules\User::class,
    ],
];

您可以使用以下命令发布和运行迁移(仅适用于数据库存储)

php artisan vendor:publish --tag="panic-control-laravel-migrations"
php artisan migrate

使用方法

外观

创建 Panic Control 1

use PanicControl\Facades\PanicControl;

PanicControl::create([
    'name' => 'panic-control-name',
    'description' => 'Description for Panic Control',
    'status' => false,
]);

更新 Panic Control 1

use PanicControl\Facades\PanicControl;

$panic = 'panic-control-name'; //Panic Control Name or ID

PanicControl::edit($panic, [
    'name' => 'new-panic-control-name',
]);

获取所有 Panic Control

use PanicControl\Facades\PanicControl;

PanicControl::all();

获取一个 Panic Control

use PanicControl\Facades\PanicControl;

PanicControl::find('panic-control-name');

检查 Panic Control 是否激活

use PanicControl\Facades\PanicControl;

PanicControl::check('panic-control-name');

辅助工具

检查 Panic Control 是否激活

getPanicControlActive('panic-control-name');

命令

列出所有 Panic Control

php artisan panic-control:list

详细一个 Panic Control

php artisan panic-control:show panic-control-name

激活 Panic Control 1

php artisan panic-control:active panic-control-name

禁用 Panic Control 1

php artisan panic-control:desactive panic-control-name

规则

我们可以添加补充规则,这些规则将尊重主要状态

所有规则都必须返回 true 以激活恐慌,如果没有注册或返回 null|false,则忽略。

路由名称

检查 Route::currentRouteName() 返回是否列在 route-name 键内。

use PanicControl\Facades\PanicControl;

PanicControl::create([
    'name' => 'panic-control-name',
    'description' => 'Description for Panic Control',
    'status' => true,
    'rules' => [
        'route-name' => [
            'route.name.home',
            'route.name.contact'
        ],
    ],
]);

URL 路径

检查 Request::path() 返回是否列在 url-path 键内。

use PanicControl\Facades\PanicControl;

PanicControl::create([
    'name' => 'panic-control-name',
    'description' => 'Description for Panic Control',
    'status' => true,
    'rules' => [
        'url-path' => [
            'url/path/home',
            'url/path/contact'
        ],
    ],
]);

抽样

将为部分用户激活,基于机会的数量和“出样本”的数量。在下面的示例中,恐慌控制将激活 5 个用户中的 5 个,即一半的用户。

重要:机会是概率,可能存在更多和更少的微小变化。

use PanicControl\Facades\PanicControl;

PanicControl::create([
    'name' => 'panic-control-name',
    'description' => 'Description for Panic Control',
    'status' => true,
    'rules' => [
        'rules' => [
            'sampling' => [
                'chance' => 5,
                'out_of' => 10,
            ],
        ],
    ],
]);

用户登录

检查用户登录的 idemail 是否列在 user 键内。

use PanicControl\Facades\PanicControl;

PanicControl::create([
    'name' => 'panic-control-name',
    'description' => 'Description for Panic Control',
    'status' => true,
    'rules' => [
        'rules' => [
            'user' => [
                1, //User ID
                'user@test.com', //User EMAIL
            ],
        ],
    ],
]);

自定义规则

要创建自定义规则,请参考以下示例

use PanicControl\Rules\Rule;
use PanicControl\Contracts\RuleContract;

class ClassName extends Rule implements RuleContract
{
    public function rule(array $parameters): bool|null
    {
        return false|true|null;
    }
}

该类必须在 config/panic-control.php 下的 rules 键中注册。

return [
    ...
    'rules' => [
        'class-name' => Namespace/ClassName::class,
    ],
];

在数据库的 rules 列中,添加在 config/panic-control.php 中注册的键以及将发送到类的参数。

use PanicControl\Facades\PanicControl;

PanicControl::create([
    'name' => 'panic-control-name',
    'description' => 'Description for Panic Control',
    'status' => true,
    'rules' => [
        'class-name' => 'parameters',
    ],
]);

驱动程序

默认情况下,配置在 config('panic-control.default') 中的驱动程序,但可以通过支持以下内容进行更改: databasefileendpoint

use PanicControl\Facades\PanicControl;

PanicControl::driver('file')->count()

扩展驱动程序

此功能处于测试阶段

如果您想包含对其他驱动程序的支持,您可以在 AppServiceProvider 中轻松注册新驱动程序,如下所示

use PanicControl\Facades\PanicControl;

PanicControl::extend('other', function(){
  return return new OtherDrive();
});

测试

composer test

变更日志

请参阅 CHANGELOG 以获取有关最近更改的更多信息。

安全漏洞

请参阅 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 以获取更多信息。