Disclosure: The documentation on this page was written with Claude AI.
Dikki Config is a PHP library that allows you to create and manage configuration files for your PHP applications. It supports multiple file formats including PHP arrays, .env
files, JSON, YAML, INI, and Neon. This library provides a unified interface to access configuration values regardless of the underlying file format.
- PHP Array (
Dikki\Config\PhpArrayParser
) - JSON (
Dikki\Config\JsonParser
) - YAML (
Dikki\Config\YamlParser
) - INI (
Dikki\Config\IniParser
) - Neon (
Dikki\Config\NeonParser
) - DotEnv (
Dikki\Config\DotEnvParser
)
To install Dikki Config, use Composer:
composer require dikki/config
If you are using more than one type of configuration file, you can use the ConfigFetcher
class to fetch and merge configurations from all supported file types.
use Dikki\Config\ConfigFetcher;
$configFetcher = new ConfigFetcher(__DIR__ . '/config');
// Fetch all configurations
$config = $configFetcher->fetch();
// Get a specific configuration value using dot notation
echo $configFetcher->get('app.timezone');
If you are using only one type of configuration file, you can use the Config
class with the appropriate parser.
First, ensure that you have a config folder in your project. Add all your config files in this folder.
use Dikki\Config\Config;
use Dikki\Config\YamlParser;
// Create an instance of the Config class
$config = new Config(new YamlParser(__DIR__ . '/config')); # pass either single file path or whole directory
// Get a configuration value (dot notation is supported)
echo $config->get('app.timezone');
OUTPUT: UTC
The ConfigFetcher
class is designed to recursively search for and parse all supported configuration file formats within a given directory.
public function __construct(string $path)
path
: The path to the directory containing configuration files.
fetch()
: Fetch and parse all supported config files.getFiles(string $dir)
: Recursively get all files from the given directory.mergeConfigs(array $baseConfig, array $newConfig)
: Merge two configuration arrays, preserving nested keys.get(string $key, mixed $default = null)
: Get a specific configuration value by key using dot notation.
The Config
class takes an instance of a class implementing ConfigInterface
and delegates the get()
method to it.
public function __construct(ConfigInterface $parser)
parser
: An instance of a class implementingConfigInterface
.
get(string $key, mixed $default = null)
: Get a value from the config array using dot notation.getAll()
: Get all configuration values.parse()
: Parse the configuration file(s) and return an array.
Each parser class implements the ConfigInterface
and provides methods to parse specific file formats.
parse()
: Parse the configuration file(s) and return an array.get(string $key, mixed $default = null)
: Get a value from the config array using dot notation.
Parses .env
files.
Parses INI files.
Parses JSON files.
Parses Neon files.
Parses PHP array files.
Parses YAML files.
To test the functionality of the Dikki Config library, you can use the provided test cases coded using Nette Tester.
use Dikki\Config\Config;
use Dikki\Config\ConfigFetcher;
use Dikki\Config\DotEnvParser;
use Dikki\Config\IniParser;
use Dikki\Config\JsonParser;
use Dikki\Config\NeonParser;
use Dikki\Config\PhpArrayParser;
use Dikki\Config\YamlParser;
use Tester\Assert;
require __DIR__ . '/vendor/autoload.php';
Tester\Environment::setup();
$configDir = __DIR__ . '/config';
$configFetcher = new ConfigFetcher($configDir);
$dotenvConfig = new Config(new DotEnvParser($configDir . '/.env'));
$iniConfig = new Config(new IniParser($configDir . '/iniconfig.ini'));
$jsonConfig = new Config(new JsonParser($configDir . '/jsonconfig.json'));
$neonConfig = new Config(new NeonParser($configDir . '/neonconfig.neon'));
$phpConfig = new Config(new PhpArrayParser($configDir . '/phpconfig.php'));
$yamlConfig = new Config(new YamlParser($configDir . '/yamlconfig.yaml'));
// Test individual parsers
Assert::equal($dotenvConfig->get('APP_NAME'), "Sample App");
Assert::equal($iniConfig->get('app.url'), "https://example.com");
Assert::equal($jsonConfig->get('app.author'), "Naman");
Assert::equal($neonConfig->get('app.debug'), false);
Assert::equal($phpConfig->get('app.timezone'), "Asia/Kolkata");
Assert::equal($yamlConfig->get('app.theme'), "Default");
// Test ConfigFetcher
Assert::equal($configFetcher->get('APP_NAME'), "Sample App");
Assert::equal($configFetcher->get('app.author'), "Naman");
Assert::equal($configFetcher->get('app.url'), "https://example.com");
Assert::equal($configFetcher->get('app.debug'), false);
Assert::equal($configFetcher->get('app.timezone'), "Asia/Kolkata");
Assert::equal($configFetcher->get('app.theme'), "Default");
This project is licensed under the MIT License - see the LICENSE file for details.