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
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:
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:
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:
The parser gets passed in while creating the config object:
Using “dot” notation
You can use “dot” notation to specify configuration hierarchies:
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:
Addition
This package works great in combination with dotfiles. By using a package like phpdotenv, you can inject environment variables into the configuration file.
Conclusion
Lightweight configuration file loader which makes working with configuration files a pleasure. The package can be found on GitHub.