nbgrp/env-bundle

The bundle provides a few useful custom Symfony Environment Variable processors

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

v1.0.3 2021-08-02 18:21 UTC

This package is auto-updated.

Last update: 2021-09-02 18:29:48 UTC


README

Codecov Audit License Total Downloads

Overview

The bundle provides a few useful custom Symfony Environment Variable processors:

  • Array Cast processor for array values type casting.
  • CSV processor with customizable delimiter.

Installation / Configuration

composer require nbgrp/env-bundle

Enable the bundle in config/bundles.php:

return [
    // ...
    NbGroup\Symfony\NbgroupEnvBundle::class => ['all' => true],
];

All Environment Variable processors disabled by default. You should enable the required processors explicitly through the bundle config.

YAML config example:

# config/packages/nbgroup_env.yaml
nbgroup_env:
    array_cast: true  #  enable Array Cast processor
    csv:              #  enable CSV processor
        dot: '.'      #  csv-dot will parse env value into array with "." as a separator
        colon: ':'    #  csv-colon will parse env value into array with ":" as a separator

PHP config example (for Symfony 5):

// config/packages/nbgroup_env.php
return static function (Symfony\Config\NbgroupEnvConfig $config): void {
    $config->arrayCast()
        ->enabled(true)
    ;

    $config->csv()
        ->enabled(true)
        ->delimiter('dot', '.')
        ->delimiter('colon', ':')
    ;
};

Processors

ArrayCastEnvVarProcessor

Performs type casting of the env value to the one of the supported types:

  • bool
  • int
  • float
  • string

nb: If the csv value is not an array it will be casted to an array.

Example:

# config/services.yaml
parameters:
  env(CSV_BOOL_ENV): '1,0,no,"true"'
  env(CSV_INT_ENV): '1,"2","3"'
  env(JSON_FLOAT_ENV): '{"key1": 1.1,"key2": "2.2"}'
  env(JSON_STRING_ENV): '["foo", "foo \"bar\"", ""]'
...
  bools:   '%env(bool-array:csv:CSV_BOOL_ENV)%'        #  will contains [true, false, false, true]
  ints:    '%env(int-array:csv:CSV_INT_ENV)%'          #  will contains [1, 2, 3]
  floats:  '%env(float-array:csv:CSV_FLOAT_ENV)%'      #  will contains ['key1' => 1.1, 'key2' => 2.2]
  strings: '%env(string-array:json:JSON_STRING_ENV)%'  #  will contains ['foo', 'foo "bar"', '']

CsvEnvVarProcessor

Parses the env value into array. Unlike build-in csv processor, this one supports customization of the delimiter.

To use the CSV processor it should be configured: see config example how to specify available delimiters (and so env prefixes).

nb: Do not use backslash \ for escaping double quote " enclosure character (on PHP ^7.4 it will not work). For escape " just write it twice.

Example:

# config/packages/nbgroup_env.yaml
nbgroup_env:
    csv:
        semi: ';'

# config/services.yaml
parameters:
  env(CSV_SEMICOLON_ENV): 'Alice;alice@mail.me'
...
  person: '%env(csv-semi:CSV_SEMICOLON_ENV)%'  #  will contains ['Alice', 'alice@mail.me']