lionix/envclient

Laravel 环境客户端和控制台命令。

1.1.3 2021-01-21 07:39 UTC

This package is auto-updated.

Last update: 2024-09-08 21:17:03 UTC


README

使用 artisan 控制台命令、环境规则和外观管理并验证环境变量

安装

composer require lionix/envclient

重大变更

从 1.0.0 到 1.1.0

  • 使用 Laravel 服务容器构建 \Lionix\Envclient,而不是使用简单的构造。

Artisan 命令

基本用法

使用 env:set artisan 命令设置环境变量。

php artisan env:set EXAMPLE_ENV_VARIABLE 'example value'

该命令将通过替换或添加给定的键来修改您的环境文件。

验证环境变量

如果您想在 env:set 命令修改文件之前应用验证规则,您将必须发布命令包配置文件。

php artisan vendor:publish --provider='Lionix\EnvClient\Providers\EnvClientServiceProvider' --tag='config'

该命令将创建 config/env.php

<?php

return [

    /**
     * Validation classes which contain environment rules
     * applied by env artisan commands.
     *
     * Add your validation classes created by
     * `php artisan make:envrule` command to apply their rules
     *
     * @var array
     */
    'rules' => [
        \App\Env\BaseEnvValidationRules::class
    ]
];

app/Env/BaseEnvValidationRules.php

<?php

namespace App\Env;

use Lionix\EnvValidator;

class BaseEnvValidationRules extends EnvValidator
{
    /**
     * Validation rules that apply to the .env variables.
     *
     * @return array
     */
    public function rules() : array
    {
        return [
            //
        ];
    }
}

通过将验证规则添加到 rules 方法的返回值中,您将应用它们到 env:set 命令。

...
public function rules() : array
{
    return [
        'DB_CONNECTION' => 'required|in:mysql,sqlite'
    ];
}
...

这样,如果您尝试使用 env:set 命令将无效的值设置到 DB_CONNECTION 变量,控制台将打印出错误

$ php artisan env:set DB_CONNECTION SomeInvalidValue
The selected DB_CONNECTION is invalid.

如果您的环境文件已被修改,您可以运行 env:check 命令,该命令将检查所有变量的有效性并打印出结果。

$ php artisan env:check
The selected DB_CONNECTION is invalid.

创建新的环境验证规则

运行 make:envrule 命令

默认情况下,脚本将在 App/Env 命名空间中生成一个类。

示例

php artisan make:envrule DatabaseEnvRules

app/Env/DatabaseEnvRules.php

<?php

namespace App\Env;

use Lionix\EnvValidator;

class DatabaseEnvRules extends EnvValidator
{
    /**
     * Validation rules that apply to the .env variables.
     *
     * @return array
     */
    public function rules() : array
    {
        return [
            //
        ];
    }
}

指定验证规则

...
public function rules() : array
{
    return [
        'DB_CONNECTION' => 'requried|in:mysql,sqlite,pgsql,sqlsrv'
        'DB_HOST' => 'requried',
        'DB_PORT' => 'requried|numeric',
        'DB_DATABASE' => 'requried',
        'DB_USERNAME' => 'requried',
        'DB_PASSWORD' => 'requried'
    ];
}
...

应用规则

您可以将 DatabaseEnvRules 类添加到 env.php 配置文件中的 rules 键。这样,类中指定的所有规则都将影响包 artisan 命令。

config/env.php

<?php

return [

    /**
     * Validation classes which contain environment rules
     * applied by env artisan commands.
     *
     * Add your validation classes created by
     * `php artisan make:envrule` command to apply their rules
     *
     * @var array
     */
    'rules' => [
        \App\Env\BaseEnvValidationRules::class
        \App\Env\DatabaseEnvRules::class // <- our database rules
    ]
];

或者,您可以使用 Lionix\EnvClient Facade 使用给定的验证规则验证输入

...
$client = app()->make(\Lionix\Envclient::class);

$client->useValidator(new \App\Env\DatabaseEnvRules())->update($databaseCredentials);

if ($client->errors()->isNotEmpty()) {
    // handle errors
} else {
    // success, the variables are updated
}
...

外观

Lionix\EnvClient

属性

  • protected $getter : Lionix\EnvClient\Interfaces\EnvGetterInterface

  • protected $setter : Lionix\EnvClient\Interfaces\EnvSetterInterface

  • protected $validator : Lionix\EnvClient\Interfaces\EnvValidatorInterface

方法

  • void : __construct()
    使用默认依赖创建 EnvClient 的新实例

  • self : useGetter(Lionix\EnvClient\Interfaces\EnvGetterInterface $getter)
    设置客户端 getter 依赖

  • self : useSetter(Lionix\EnvClient\Interfaces\EnvSetterInterface $setter)
    设置 setter 依赖

  • self : useValidator(Lionix\EnvClient\Interfaces\EnvValidatorInterface $validator)
    设置 validator 依赖,合并当前错误与 validator 错误

  • array : all()
    从环境文件获取所有环境变量

  • bool : has(string $key)
    检查环境文件是否包含该键

  • mixed : get(string $key)
    使用键获取环境变量(返回 Illuminate\Support\Env get 方法的输出)

  • self : set(array $values)
    如果验证规则通过,则在运行时设置环境变量

  • self : save()
    将之前设置的变量保存到环境文件中

  • self : update()
    如果验证规则通过,则设置并保存变量到环境文件

  • bool : validate(array $values)
    检查值的合法性并检索通过状态

  • Illuminate\Support\MessageBag : errors()
    获取类生命周期中发生的所有验证错误

Lionix\EnvGetter

方法

  • void : __construct()
    创建EnvGetter的新实例

  • mixed : get(string $key)
    使用键获取环境变量(返回 Illuminate\Support\Env get 方法的输出)

  • array : all()
    从环境文件获取所有环境变量

  • bool : has(string $key)
    检查环境文件是否包含该键

Lionix\EnvSetter

属性

  • 受保护 $variablesToSet : array

方法

  • void : __construct()
    创建EnvSetter的新实例

  • void : set(array $values)
    将给定的值与variablesToSet属性合并

  • void : save()
    将之前通过set方法设置的变量保存到环境文件中

  • 受保护 string : sanitize(string $value)
    清理输入值

Lionix\EnvValidator

属性

  • 受保护 $errors : Illuminate\Support\MessageBag

方法

  • void : __construct()
    创建EnvValidator的新实例

  • array : rules()
    返回类的验证规则

  • bool : validate(array $values)
    验证给定的值

  • Illuminate\Support\MessageBag : errors()
    获取验证器错误

  • void : mergeErrors(Illuminate\Support\MessageBag $errors)
    将给定的MessageBag与当前错误合并

致谢