PHP code example of abdeslam / envator

1. Go to this page and download the library: Download abdeslam/envator 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/ */

    

abdeslam / envator example snippets




use Abdeslam\Envator\EnvatorFactory;

ath/to/.env'],
    [] // no filter will be loaded
);




use Abdeslam\Envator\EnvatorFactory;

ath/to/.env'],
    null // (default value) the default 5 filters will be loaded
);




use Abdeslam\Envator\EnvatorFactory;
use Abdeslam\Envator\Filters\BooleanValueFilter;


    [BooleanValueFilter::class]
);




use Abdeslam\Envator\Contracts\FilterInterface;

class MyCustomFilter implements FilterInterface
{
    /**
     * @inheritDoc
     */
    public static function filter(array $oldItems, string $key, $value): array
    {
        $prefix = 'MY_PREFIX_';
        // the key exists in the items parsed previously
        // so it will be overwritten
        // we add a prefix to the key
        if (array_key_exists($key, $oldItems)) {
            $key = $prefix . $key;
        }
        return ['key' => $key, 'value' => $value];
    }
}

 



use Abdeslam\Envator\EnvatorFactory;
use MyCustomFilter;

/ loading needed filters
    [MyCustomFilter::class]
);
$envator->populate();

echo getenv('USERNAME'); // output : 'user'
echo getenv('MY_PREFIX_USERNAME'); // output : 'admin'




use Abdeslam\Envator\EnvatorFactory;

o/.env']);
// populate the variables to the super global $_ENV only
$envator->populate([
    Envator::GLOBAL_ENV => true,
    Envator::PUT_ENV => false, // populate using putenv() function (risky)
    Envator::APACHE => false, // populates using apache_setenv() (for apache environment)
    Envator::SERVER => false // populates to the super global $_SERVER
]);

/**
 * the default configuration :
 * [
 *  Envator::GLOBAL_ENV => true,
 *  Envator::PUT_ENV => true,
 *  Envator::APACHE => false,
 *  Envator::SERVER => true,
 * ]
*/



use Abdeslam\Envator\Envator;

oviding the directory to use for caching
$cacheManager = new CacheManager(__DIR__);
$envator = new Envator();
// setting a cache manager enables the cache automatically
$envator->setCacheManager($cacheManager);
$envator->load('/path/to/.env')->populate();



use Abdeslam\Envator\EnvatorFactory;

ath/to/.env'],
    null,
    __DIR__ // cache directory
);
$envator->populate();



use Abdeslam\Envator\Envator;

$parser = new MyCustomParser();
$envator = new Envator($resolver, $parser);

php composer.phar 
 php


use Abdeslam\Envator\Envator;

('/path/to/.env');
$envator->populate();

echo getenv('APP_NAME'); // output: 'my_awesome_application'
echo $_ENV['APP_NAME']; // output: 'my_awesome_application'
echo $_SERVER['APP_NAME']; // output: 'my_awesome_application'
 php


use Abdeslam\Envator\Envator;

tiple .env files
$envator->load('/path/to/.env', '/path/to/another/.env');
$envator->populate();
 php


use Abdeslam\Envator\EnvatorFactory;

ath/to/.env'
]);
$envator->populate();

echo getenv('APP_NAME'); // output: 'my_awesome_application'
echo $_ENV['APP_NAME']; // output: 'my_awesome_application'
echo $_SERVER['APP_NAME']; // output: 'my_awesome_application'
 php


use Abdeslam\Envator\Envator;
use Abdeslam\Envator\Filters\TrimQuotesFilter;
use Abdeslam\Envator\Filters\BooleanValueFilter;
use Abdeslam\Envator\Filters\NumericValueFilter;
use Abdeslam\Envator\Filters\VariableFilter;
use Abdeslam\Envator\Filters\EmptyStringToNullFilter;

NT'); // output: 'development' instead of '"development"'

echo $_ENV['DEBUG']; // output: true (boolean) instead of 'true' (string)

echo $_ENV['VERBOSITY_LEVEL']; // output: 2 (integer) instead of '2' (string)

echo $_SERVER['DATABASE_PASSWORD']; // output: 'user1234'

echo getenv('DEBUG'); // output: '1' 
// getenv() does not support booleans and NULL
echo getenv('EMPTY'); // output: '' 
// getenv() does not support booleans and NULL

echo $_ENV['EMPTY']; // output: NULL

echo $_SERVER['EMPTY2']; // output: NULL