Skip to main content

Special Types

Returning parsed value from Static Files​

By default, any value in the static files (*.env) are parsed as string.

You can parse to a specific types using this syntax:

PARAM1=!parser VALUE

Where !parser is one of the pre-defined parsers:

ParserDescriptionExample
!boolParse to booleanPARAM=!bool true
!intParse to integerPARAM=!int 10
!floatParse to floatPARAM=!float 3.5
!jsondecodeParse to JSON and transform to an arrayPARAM=!jsondecode {"a":"b"}
!arrayParse to arrayPARAM=!array 1,2,3,4
!dictParse to dictionary (associative array)PARAM=!dict a=1,b=2
!unescUnescape the valuePARAM=!unesc a\nb
!fileLoad the content of a filePARAM=!file /path/to/file

A relative !file path is resolved from the library's own directory, not the project. Use {__ROOT__} to point at a project file: PARAM=!file {__ROOT__}/config/key.pem (see Paths relative to the project root).

Adding a new Parser​

You can add a new special type:

<?php
use ByJG\Config\ParamParser;

if (!ParamParser::isParserExists('mytype')) {
ParamParser::addParser('mytype', function ($value) {
return 'mytype:' . $value;
});
}

Then you can use:

PARAM1=!mytype 123

Referencing other entries in a binding​

To use another entry inside a binding — a value parsed here, another service, or the container itself — use Param::get() or Param::container(), never Config::get(): the configuration is evaluated while the container is being built. See Referencing other entries: Param.


Open source ByJG