1. Go to this page and download the library: Download caseyamcl/configula 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/ */
caseyamcl / configula example snippets
use Configula\ConfigFactory as Config;
// Load all .yml, .php, .json, and .ini files from directory (recursive)
// Supports '.local' and '.dist' modifiers to load config in correct order
$config = Config::loadPath('/path/to/config/files', ['optional' => 'defaults', ...]);
// Load all .yml, .php, .json, and .in files from directory (non-recursive)
// Supports '.local' and '.dist' modifiers to load config in correct order
$config = Config::loadSingleDirectory('/path/to/config/files', ['optional' => 'defaults', ...]);
// Load from array
$config = Config::fromArray(['some' => 'values']);
// Chain loaders -- performs deep merge
$config = Config::fromArray(['some' => 'values'])
->merge(Config::loadPath('/some/path'))
->merge(Config::loadEnv('MY_APP'));
$config = new Configula\ConfigValues(['array' => 'values']);
$config = (new Configula\Loader\FileListLoader(['file-1.yml', 'file-2.json']))->load();
// get method - throws exception if value does not exist
$config->get('some_value');
// get method with default - returns default if value does not exist
$config->get('some_value', 'default');
// find method - returns NULL if value does not exist
$config->find('some_value');
// has method - returns TRUE or FALSE
$config->has('some_value');
// hasValue method - returns TRUE if value exists and is not empty (NULL, [], "")
$config->hasValue('some_value');
// Here is a nested array:
$values = [
'debug' => true,
'db' => [
'platform' => 'mysql',
'credentials' => [
'username' => 'some',
'password' => 'thing'
]
],
];
// Load it into Configula
$config = new \Configula\ConfigValues($values);
// Access top-level item
$values->get('debug'); // bool; TRUE
// Access nested item
$values->get('db.platform'); // string; 'mysql'
// Access deeply nested item
$values->get('db.credentials.username'); // string: 'some'
// Get item as array
$values->get('db'); // array ['platform' => 'mysql', 'credentials' => ...]
// has/hasValue work too
$values->has('db.credentials.key'); // false
$values->hasValue('db.credentials.key'); // false
// Access configuration values
$config = Config::loadPath('/path/to/config/files');
// Throws exception if value does not exist
$some_value = $config->some_key;
// Returns TRUE or FALSE
isset($config->some_key);
// Basic iteration
foreach ($config as $item => $value) {
echo "<li>{$item} is {$value}</li>";
}
// Count
count($config); /* or */ $config->count();
// Throws exception if value does not exist
$value = $config('some_value');
// Returns default value if value does not exist
$value = $config('some_value', 'default');
// Throws exception if value does not exist
$some_value = $config['some_key'];
// Returns TRUE or FALSE
$exists = isset($config['some_key']);
// Not allowed; always throws exception (config is immutable)
$config['some_key'] = 'foobar'; // Configula\Exception\ConfigLogicException
unset($config['some_key']); // Configula\Exception\ConfigLogicException
use Configula\ConfigValues;
$config = new ConfigValues(['foo' => 'bar', 'baz' => 'biz']);
// Merge configuration using merge()
$newConfig = $config->merge(new ConfigValues(['baz' => 'buzz', 'cad' => 'cuzz']));
// For convenience, you can pass in an array using mergeValues()
$newConfig = $config->mergeValues(['baz' => 'buzz', 'cad' => ['some' => 'thing']]);
use Configula\Loader\JsonEnvLoader;
$values = (new JsonEnvLoader('MY_ENV_VAR'))->load();
echo $values->foo;
echo $values->get('foo'); // "bar"
use Configula\ConfigFactory as Config;
use Configula\Loader;
$config = Config::loadMultiple([
new Loader\EnvLoader('My_APP'), // Instance of LoaderInterface
['some' => 'values'], // Array of config vaules
'/path/to/some/file.yml', // Path to file (must exist)
new \SplFileInfo('/path/to/another/file.json') // SplFileInfo
]);
// Alternatively, you can pass an iterator of `Configula\ConfigLoaderInterface` instances to
// `Configula\Loader\CascadingConfigLoader`.
// These throw a ConfigValueNotFoundException
$config->get('non_existent_value');
$config['non_existent_value'];
$config->non_existent_value;
// This will not throw an exception, but instead return NULL
$config->find('non_existent_value');
// This will not throw an exception, but instead return 'default'
$config->get('non_existent_value', 'default');
use Configula\ConfigValues;
use Configula\Exception\InvalidConfigValueException;
class AppConfig extends ConfigValues
{
/**
* Is the app running in development mode?
*
* @return bool
*/
public function isDevMode(): bool
{
// Get the value or assume false
return (bool) $this->get('devmode', false);
}
/**
* Get the encryption key (as 32-character alphanumeric string)
*
* @return string
*/
public function getEncryptionKey(): string
{
// If the value doesn't exist, a `ConfigValueNotFoundException` is thrown
$key = $this->get('encryption_key');
// Let's do a little validation...
if (strlen($key) != 32) {
throw new InvalidConfigValueException('Encryption key must be 32 characters');
}
return $key;
}
}
use Configula\ConfigFactory;
// Build it
$config = AppConfig::fromConfigValues(ConfigFactory::loadPath('/some/path'));
// Use it (and enjoy the type-hinting in your IDE)
$config->getEncryptionKey();
$config->isDevMode();
// etc...
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class ConfigTree implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->getRootNode();
$rootNode->children()
->boolean('devmode')->defaultValue(false)->end()
->scalarNode('encryption_key')->isRequired()->cannotBeEmpty()->end()
->arrayNode('db')
->children()
->scalarNode('host')->cannotBeEmpty()->defaultValue('localhost')->end()
->integerNode('port')->min(0)->defaultValue(3306)->end()
->scalarNode('driver')->cannotBeEmpty()->defaultValue('mysql')->end()
->scalarNode('dbname')->cannotBeEmpty()->end()
->scalarNode('user')->cannotBeEmpty()->end()
->scalarNode('password')->end()
->end()
->end() // End DB
-end();
return $treeBuilder;
}
}
use Configula\ConfigFactory;
use Configula\Util\SymfonyConfigFilter;
// Setup your config tree, and load your configuration
$configTree = new ConfigTree();
$config = ConfigFactory::loadPath('/path/to/config');
// Validate the configuration by filtering it through the allowed values
// If anything goes wrong here, a Symfony exception will be thrown (not a Configula exception)
$config = SymfonyConfigFilter::filter($configTree, $config);
use Configula\Loader\AbstractFileLoader;
class MyFileLoader extends AbstractFileLoader
{
/**
* Parse file contents
*
* @param string $rawFileContents
* @return array
*/
protected function parse(string $rawFileContents): array
{
// Parse the file contents and return an array of values.
}
}
use Configula\ConfigFactory;
// use the factory..
$config = ConfigFactory::load(new MyFileLoader('/path/to/file'));
// ..or don't..
$config = (new MyFileLoader('/path/to/file'))->load();
use Configula\Loader\FileLoader;
use Configula\Loader\FolderLoader;
// Map my custom file loader to the 'conf' extension type (case-insensitive)
$extensionMap = array_merge(FileLoader::DEFAULT_EXTENSION_MAP, [
'conf' => MyFileLoader::class
]);
// Now any files encountered in the folder with .conf extension will use my custom file loader
$config = (new FolderLoader('/path/to/folder', true, $extensionMap))->load();
use Configula\Loader\ConfigLoaderInterface;
use Configula\Exception\ConfigLoaderException;
use Configula\ConfigValues;
class MyLoader implements ConfigLoaderInterface
{
public function load(): ConfigValues
{
if (! $arrayOfValues = doWorkToLoadValuesHere()) {
throw new ConfigLoaderException("Something went wrong..");
}
return new ConfigValues($arrayOfValues);
}
}
use Configula\ConfigFactory;
// use the factory..
$config = ConfigFactory::load(new MyLoader());
// ..or use it directly.
$config = (new MyLoader())->load();