rkulik/config

Lightweight configuration file loader

1.2.0 2019-02-10 09:53 UTC

This package is auto-updated.

Last update: 2024-04-10 21:22:54 UTC


README

Lightweight configuration file loader. Currently supporting PHP and JSON files.

Requirements

This package requires PHP 7.1 or higher.

Install

Via composer:

$ composer require rkulik/config

Usage

Instantiate basic configuration

<?php
// config.php

return [
    'hello' => 'world',
];
<?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.

<?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;
    }
}
<?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.

<?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

$ composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email rene@kulik.io instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.