config
Lightweight configuration file loader. Currently supporting PHP and JSON files., (*1)
Requirements
This package requires PHP 7.1 or higher., (*2)
Install
Via composer:, (*3)
``` bash
$ composer require rkulik/config, (*4)
## Usage
### Instantiate basic configuration
``` php
<?php
// config.php
return [
'hello' => 'world',
];
``` php
<?php
// index.php, (*5)
require 'vendor/autoload.php';, (*6)
$configFactory = new \Rkulik\Config\ConfigFactory();, (*7)
$config = $configFactory->make('config.php');, (*8)
echo $config->get('hello'); // world, (*9)
### 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.
``` php
<?php
// config.php
return [
'hello' => 'world',
];
``` php
<?php
// CustomParser.php, (*10)
use Rkulik\Config\FileParser\FileParserInterface;, (*11)
class CustomParser implements FileParserInterface
{
/**
* {@inheritdoc}
*/
public function parse(string $file): array
{
$data = require $file;, (*12)
array_walk($data, function (&$item) {
$item = strrev($item);
});
return $data;
}
}, (*13)
``` php
<?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
Use "dot" notation to perform CRUD operations on configuration data., (*14)
``` php
<?php
// config.php, (*15)
return [
'hello' => [
'beautiful' => 'world',
],
];, (*16)
``` php
<?php
// index.php
require 'vendor/autoload.php';
$configFactory = new \Rkulik\Config\ConfigFactory();
$config = $configFactory->make('config.php');
echo $config->get('hello.beautiful'); // world
Testing
bash
$ composer test, (*17)
Changelog
Please see CHANGELOG for more information what has changed recently., (*18)
Contributing
Please see CONTRIBUTING for details., (*19)
Security
If you discover any security-related issues, please email rene@kulik.io instead of using the issue tracker., (*20)
Credits
License
The MIT License (MIT). Please see License File for more information., (*21)