Laravel EnvClient
Manage and validate environmental variables with artisan console commands, environmental rules and facades
Installation
Breaking changes
From 1.0.0 to 1.1.0
To construct the
\Lionix\Envclient
use Laravel Service Container instead of plain construct.
Artisan commands
Basic usage
Set an environment variable using env:set
artisan command.
The command will modify your environment file by replacing or adding the given key to it.
Validate environment variables
If you want to apply validation rules to environmental variables before env:set
command will modify the file you will have to publish command package configuration files.
The command will create config/env.php
and app/Env/BaseEnvValidationRules.php
By adding validation rules into rules
method return value you will apply them to env:set
command.
This way if you try to set an invalid value to the DB_CONNECTION
variable with env:set
command, the console will print out an error
If your environment file was modified you can run env:check
command which will check all variables for validness and print out the results.
Create a new environmental validation rules
Run the make:envrule
command
make:envrule
commandBy default, the script will generate a class in App/Env
namespace.
Example:
app/Env/DatabaseEnvRules.php
Specify validation rules:
Apply the rules:
You can add the DatabaseEnvRules
class to env.php
configuration file at the rules
key. That way all the rules specified in the class will affect package artisan commands.
config/env.php
Or you can use Lionix\EnvClient
Facade to validate the input with given validation rules:
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 dependenciesself
: useGetter(Lionix\EnvClient\Interfaces\EnvGetterInterface $getter) Set client getter dependencyself
: useSetter(Lionix\EnvClient\Interfaces\EnvSetterInterface $setter) Set setter dependencyself
: useValidator(Lionix\EnvClient\Interfaces\EnvValidatorInterface $validator) Set validator dependency merging current errors with the validator errorsarray
: all() Get all env variables from the environmental filebool
: has(string $key) Check if the environmental file contains the keymixed
: get(string $key) Get the env variable using the key (returns the output ofIlluminate\Support\Env
get method)self
: set(array $values) Set the environmental variables at runtime if validation rules passedself
: save() Save previously set variables to the environmental fileself
: update() If validation rules passed then set and save variables to the environmental filebool
: validate(array $values) Check values validness and retrieve passed statusIlluminate\Support\MessageBag
: errors() Get all validation errors occurred during the class lifetime
Lionix\EnvGetter
Methods:
void
: __construct() Create a new instance of EnvGettermixed
: get(string $key) Get the env variable using the key (returns the output ofIlluminate\Support\Env
get method)array
: all() Get all env variables from the environmental filebool
: 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 EnvSettervoid
: set(array $values) Merge given values with variablesToSet propertyvoid
: save() Save all variables previously set by the set method to the environmental fileprotected
string
: sanitize(string $value) Sanitize input values
Lionix\EnvValidator
Properties:
protected $errors : Illuminate\Support\MessageBag
Methods:
void
: __construct() Create a new instance of EnvValidatorarray
: rules() Returns class validation rulesbool
: validate(array $values) Validate given valuesIlluminate\Support\MessageBag
: errors() Get validator errorsvoid
: mergeErrors(Illuminate\Support\MessageBag $errors) Merge given MessageBag with current errors
Credits:
Last updated