PHP code example of ddrv / env

1. Go to this page and download the library: Download ddrv/env library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ddrv / env example snippets


$_ENV = [
    'APP_VAR_1' => 'value1',
    'APP_VAR_2' => 'value2',
    'APP_VAR_BOOL_TRUE' => 'true',
    'APP_VAR_BOOL_FALSE' => '0',
    'APP_VAR_INT_ONE' => '1',
    'APP_VAR_FLOAT_PI' => '3.1415',
    'APP_VAR_INT_ENUM_ONE' => '1',
    'APP_VAR_STRING_ENUM_A' => 'a',
    'APP_VAR_ENUM_FOO' => 'Foo',
    'APP_VAR_ENUM_STRING_A' => 'A',
    'APP_VAR_ENUM_INT_1' => 'One',
];



use Ddrv\Env\Env;
use Ddrv\Env\VariableProvider\EnvVariableProvider;
use Ddrv\Env\Exception\VariableUndefined;
use Tests\Code\Ddrv\Env\IntEnum;
use Tests\Code\Ddrv\Env\StringEnum;
use Tests\Code\Ddrv\Env\UntypedEnum;

$env = new Env(new EnvVariableProvider());

(string)$env->optional('APP_VAR_1'); // returns 'value1'
(string)$env->optional('APP_VAR_2'); // returns 'value2'
(string)$env->optional('APP_VAR_3'); // returns null because $_ENV has not 'APP_VAR_3' key

(string)$env->>int(); // returns 3.1415
$env->ntEnum::One

/** Default value for optional */
$env->optional('APP_VAR_3')?->string() ?? 'default'; // returns "default"



use Ddrv\Env\Env;
use Ddrv\Env\VariableProvider\FileVariableProvider;

$env = new Env(new FileVariableProvider('/path/to/project/.env'));

$env->optional('APP_VAR_1'); // returns null because APP_VAR_3 not defined in /path/to/project/.env file
(string)$env->optional('APP_VAR_3'); // returns 'value3'



use Ddrv\Env\Env;
use Ddrv\Env\VariableProvider\MemoryVariableProvider;

$variableProvider = new MemoryVariableProvider([
    'APP_VAR_4' => 'value4',
])
$env = new Env($variableProvider);

(string)$env->optional('APP_VAR_4'); // returns 'value4'
$env->optional('APP_VAR_5'); // returns null
$variableProvider->set('APP_VAR_5', 'value5');
(string)$env->optional('APP_VAR_5'); // returns 'value5'



use Ddrv\Env\Env;
use Ddrv\Env\VariableProvider\EnvVariableProvider;
use Ddrv\Env\VariableProvider\PrefixedVariableProvider;

$env = new Env(new PrefixedVariableProvider(new EnvVariableProvider(), 'APP_'));

(string)$env->optional('VAR_1'); // returns 'value1'
$env->optional('VAR_3'); // returns null



use Ddrv\Env\Env;
use Ddrv\Env\VariableProvider\EnvVariableProvider;
use Ddrv\Env\VariableProvider\MemoryVariableProvider;

$env = new Env(new ResolveVariableProvider(new MemoryVariableProvider([
    'SCHEME_1' => 'http',
    'PATH_1' => '/path/to/file',
    'HOST_1' => '127.0.0.1',
    'PORT_1' => '8080',
    'TOKEN_1' => 'secret',
    'CYCLED_1' => '${CYCLED_2}',
    'CYCLED_2' => '${CYCLED_3}',
    'CYCLED_3' => '${CYCLED_1}',
    'URL_1' => '${SCHEME_1}://${HOST_1}:${PORT_1}${PATH_1}',
    'URL_2' => '${SCHEME_2:-https}://${HOST_2:-localhost}:${PORT_2:-1080}${PATH_1:-/}',
    'URL_3' => 'http://localhost:1080/api/version?token=\${TOKEN_1}',
    'URL_4' => 'http://localhost:1080/api/version?token=\\\\${TOKEN_1}',
    'URL_5' => 'http://localhost:1080/api/version?token=${TOKEN_2:?Token is 



use Ddrv\Env\Env;
use Ddrv\Env\VariableProvider\CompositeVariableProvider;
use Ddrv\Env\VariableProvider\EnvVariableProvider;
use Ddrv\Env\VariableProvider\FileVariableProvider;
use Ddrv\Env\VariableProvider\MemoryVariableProvider;

$env = new Env(new CompositeVariableProvider(
    new EnvVariableProvider(),
    new FileVariableProvider('/path/to/project/.env'),
    new MemoryVariableProvider([
        'APP_VAR_1' => 'other1',
        'APP_VAR_2' => 'other2',
        'APP_VAR_3' => 'other3',
        'APP_VAR_4' => 'other4',
    ]),
));

(string)$env->optional('APP_VAR_1'); // returns 'value1' because $_ENV has 'APP_VAR_1' key and priority of EnvVariableProvider is higher than that of MemoryVariableProvider
(string)$env->optional('APP_VAR_2'); // returns 'value2' because $_ENV has 'APP_VAR_2' key and priority of EnvVariableProvider is higher than that of MemoryVariableProvider
(string)$env->optional('APP_VAR_3'); // returns 'value3' because APP_VAR_3 defined in /path/to/project/.env file and priority of FileVariableProvider is higher than that of MemoryVariableProvider
(string)$env->optional('APP_VAR_4'); // returns 'other4' because MemoryVariableProvider has 'APP_VAR_4' variable and $_ENV has not 'APP_VAR_4' key and APP_VAR_4 not defined in /path/to/project/.env file
$env->optional('APP_VAR_5'); // returns null because none of the providers contain the variable APP_VAR_5



use Ddrv\Env\Env;
use Ddrv\Env\VariableProvider\CachedVariableProvider;
use Ddrv\Env\VariableProvider\EnvVariableProvider;

$env = new Env(new CachedVariableProvider(
    new EnvVariableProvider(),
));

(string)$env->optional('APP_VAR_1'); // returns 'value1' because $_ENV has 'APP_VAR_1' key and priority of EnvVariableProvider is higher than that of MemoryVariableProvider

putenv('APP_VAR_1=');
(string)$env->optional('APP_VAR_1'); // returns 'value1' because CachedVariableProvider cache this value.
$env->reload();
(string)$env->optional('APP_VAR_1'); // returns '' because reload() method clear cache.