1. Go to this page and download the library: Download jan-di/config 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/ */
jan-di / config example snippets
use Dotenv\Dotenv;
use Jandi\Config\ConfigBuilder;
use Jandi\Config\Dotenv\VlucasDotenvAdapter;
use Jandi\Config\Entry\StringEntry;
// define all config entries via a fluent API:
$configBuilder = new ConfigBuilder([
(new StringEntry('APP_ENV', 'development')),
(new StringEntry('APP_TOKEN'))->setMinLength(20)
]);
// optional: Add a Dotenv loader to load .env files in environment before building config
$dotenv = Dotenv::createImmutable(__DIR__, '');
$configBuilder->enableDotEnv(new VlucasDotenvAdapter($dotenv));
// build config array from environment variables
// The values will be fetched validated against the rules from the entries.
$config = $configBuilder->build();
use Dotenv\Dotenv;
use Jandi\Config\ConfigBuilder;
use Jandi\Config\Dotenv\VlucasDotenvAdapter;
use Jandi\Config\Entry\StringEntry;
// build config, see above
$configBuilder = new ConfigBuilder([
(new StringEntry('APP_ENV'))->setDefaultValue('development'),
(new StringEntry('APP_TOKEN'))->setMinLength(20)
]);
$configBuilder
->enableCaching('/path/to/cache/file') // Activate caching
->enableDotEnv(new VlucasDotenvAdapter(Dotenv::createImmutable(__DIR__, '.env')));
// Since caching is enabled, the builder will check if the cache file exists.
// if the cache file exists, no values are read from .env or the real environment.
$config = $configBuilder->build();
// The cache file is not created automatically. Instead you have to provide a condition,
// when the cache has to be written. Often this is related to the values inside the config.
if (!$config->isCached() && $config->getValue('APP_ENV') === 'production') {
$configBuilder->dumpCache($config);
}
(new StringEntry('STRING', 'value1'))
->setMinLength(5)
->setMaxLength(10)
->setAllowedValues(['value1','value2'])
->setRegexPattern('/value.*/')
(new BoolEntry('BOOL', 'true'));
(new IntEntry('INT', '3'))
->setLowerLimit(0)
->setUpperLimit(8);
(new FloatEntry('FLOAT', '5.5'))
->setLowerLimit(3.4)
->setUpperLimit(7.8);
try {
$builder->build();
} catch(BuildExceptionTest $e) {
echo $e->getTextSummary();
exit;
}
// There where 2 error/s while building configuration:
//
// APP_ENV [string] Value is invalid: not allowed. Allowed values: abce.
// APP_TOKEN [string] Variable is missing and has no default value.