Geek Culture
Published in

Geek Culture

EnvClient for Laravel 6+

Manage and validate environmental variables with artisan console commands, environmental rules, and facades

Photo by Christopher Gower on Unsplash

Installation

composer require lionix/envclient

Artisan commands

Basic usage

php artisan env:set EXAMPLE_ENV_VARIABLE "example value"

Validate environment variables

php artisan vendor:publish --provider="Lionix\EnvClient\Providers\EnvClientServiceProvider" --tag="config"
<?phpreturn [/**
* 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
]
];
<?phpnamespace App\Env;use Lionix\EnvValidator;class BaseEnvValidationRules extends EnvValidator
{
/**
* Validation rules that apply to the .env variables.
*
* @return array
*/
public function rules() : array
{
return [
//
];
}
}
...
public function rules() : array
{
return [
"DB_CONNECTION" => "required|in:mysql,sqlite"
];
}
...
$ php artisan env:set DB_CONNECTION SomeInvalidValue
The selected DB_CONNECTION is invalid.
$ php artisan env:check
The selected DB_CONNECTION is invalid.

Create a new environmental validation rules

Run the make:envrule command

Example:

php artisan make:envrule DatabaseEnvRules
<?phpnamespace App\Env;use Lionix\EnvValidator;class DatabaseEnvRules extends EnvValidator
{
/**
* Validation rules that apply to the .env variables.
*
* @return array
*/
public function rules() : array
{
return [
//
];
}
}

Specify validation rules:

...
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"
];
}
...

Apply the rules:

<?phpreturn [/**
* 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
]
];
...
$client = new \Lionix\Envclient();
$client
->useValidator(new \App\Env\DatabaseEnvRules())
->update($databaseCredintnails);
if ($client->errors()->isNotEmpty()) {
// handle errors
} else {
// success, the variables are updated
}
...

Facades

Lionix\EnvClient

Properties:

  • protected $getter : Lionix\EnvClient\Interfaces\EnvGetterInterface
  • protected $setter : Lionix\EnvClient\Interfaces\EnvSetterInterface
  • protected $validator : Lionix\EnvClient\Interfaces\EnvValidatorInterface

Methods:

  • void : __construct()
    Create a new instance of EnvClient using default dependencies
  • self : useGetter(Lionix\EnvClient\Interfaces\EnvGetterInterface $getter)
    Set client getter dependency
  • self : useSetter(Lionix\EnvClient\Interfaces\EnvSetterInterface $setter)
    Set setter dependency
  • self : useValidator(Lionix\EnvClient\Interfaces\EnvValidatorInterface $validator)
    Set validator dependency merging current errors with the validator errors
  • array : all()
    Get all env variables from the environmental file
  • bool : has(string $key)
    Check if the environmental file contains the key
  • mixed : get(string $key)
    Get the env variable using the key (returns the output of Illuminate\Support\Env get method)
  • self : set(array $values)
    Set the environmental variables at runtime if validation rules passed
  • self : save()
    Save previously set variables to the environmental file
  • self : update()
    If validation rules passed then set and save variables to the environmental file
  • bool : validate(array $values)
    Check values validness and retrieve passed status
  • Illuminate\Support\MessageBag : errors()
    Get all validation errors occurred during the class lifetime

Lionix\EnvGetter

Methods:

  • void : __construct()
    Create a new instance of EnvGetter
  • mixed : get(string $key)
    Get the env variable using the key (returns the output of Illuminate\Support\Env get method)
  • array : all()
    Get all env variables from the environmental file
  • bool : has(string $key)
    Check if the environmental file contains the key

Lionix\EnvSetter

Properties:

  • protected $variablesToSet : array

Methods:

  • void : __construct()
    Create a new instance of EnvSetter
  • void : set(array $values)
    Merge given values with variablesToSet property
  • void : save()
    Save all variables previously set by the set method to the environmental file
  • protected string : sanitize(string $value)
    Sanitize input values

Lionix\EnvValidator

Properties:

  • protected $errors : Illuminate\Support\MessageBag

Methods:

  • void : __construct()
    Create a new instance of EnvValidator
  • array : rules()
    Returns class validation rules
  • bool : validate(array $values)
    Validate given values
  • Illuminate\Support\MessageBag : errors()
    Get validator errors
  • void : mergeErrors(Illuminate\Support\MessageBag $errors)
    Merge given MessageBag with current errors

Credits:

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sergey Karakhanyan

CEO @ lionix.io | I help IT companies extend their tech teams and build their projects