In this post I will present a configuration file loader I wrote in PHP. The purpose of this package is to make working with different kinds of configuration files easy and intuitive. Currently supported file types are PHP and JSON, but the package is easily extandable to satisfy special requirements. The API is sleek and simple and the package itself comes without any dependencies.
Requirements
This package requires PHP 7.1 or higher.
Install
Via composer
$ composer require rkulik/config
Usage
In the following section I provide you some examples for different scenarios. These examples use PHP for their configurations.
Instantiate basic configuration
This is the basic workflow for using the config package. First you create a configuration file which returns an array:
<?php
// config.php
return [
'hello' => 'world',
];
After that you instantiate the configuration factory and create a config object based on the configuration file. This config object is able to perform CRUD operations on the configuration:
<?php
// index.php
require 'vendor/autoload.php';
$configFactory = new \Rkulik\Config\ConfigFactory();
$config = $configFactory->make('config.php');
echo $config->get('hello'); // world
Instantiate configuration using custom parser
For special requirements, such as working with unsupported types of configuration files, using a custom parser is the suggested way to go. In this example we create a custom parser to retrieve reversed configuration values:
<?php
// config.php
return [
'hello' => 'world',
];
<?php
// CustomParser.php
use Rkulik\Config\FileParser\FileParserInterface;
class CustomParser implements FileParserInterface
{
/**
* {@inheritdoc}
*/
public function parse(string $file): array
{
$data = require $file;
array_walk($data, function (&$item) {
$item = strrev($item);
});
return $data;
}
}
The parser gets passed in while creating the config object:
<?php
// index.php
require 'vendor/autoload.php';
require 'CustomParser.php';
$configFactory = new \Rkulik\Config\ConfigFactory();
$customParser = new CustomParser();
$config = $configFactory->make('config.php', $customParser);
echo $config->get('hello'); // dlrow
Using “dot” notation
You can use “dot” notation to specify configuration hierarchies:
<?php
// config.php
return [
'hello' => [
'beautiful' => 'world',
],
];
<?php
// index.php
require 'vendor/autoload.php';
$configFactory = new \Rkulik\Config\ConfigFactory();
$config = $configFactory->make('config.php');
echo $config->get('hello.beautiful'); // world
Testing
As you might already know from one of my previous posts, testing is very important to me. That’s why this package has a 100% code coverage.
You can run the tests using following:
$ composer test
Addition
This package works great in combination with dotfiles. By using a package like phpdotenv, you can inject environment variables into the configuration file.
<?php
// config.php
return [
'hello' => getenv('WORLD'),
];
Conclusion
Lightweight configuration file loader which makes working with configuration files a pleasure. The package can be found on GitHub.