xy2z/lite-config

A lightweight PHP static class with zero dependencies.

Fund package maintenance!
xy2z

1.2.2 2023-09-24 18:22 UTC

README

A lightweight PHP static class with zero dependencies.

Supports multiple configs and multidimensional arrays.

Built-in support for PHP, INI and JSON files. Also supports YAML and anything else you can parse to an array, see custom_handler() below.

Requirements

  • PHP 7.0 +

Install

composer require xy2z/lite-config

Examples

require 'path/to/vendor/autoload.php';
use xy2z\LiteConfig\LiteConfig as Config;

Array

Config::loadArray([
  'version' => '1.0',
  'app' => [
      'name' => 'Example'
  ]
]);

echo Config::get('version'); # 1.0
echo Config::get('app'); # Array('name' => 'Example')
echo Config::get('app.name', 'default name'); # Example
echo Config::get('app.type', 'Desktop'); # Desktop

Directory

# config/settings.php
return [
  'app_name' => 'Example',
];

# index.php
Config::loadDir('config/', true);
echo Config::get('settings.app_name'); # key is 'filename.key'

Single file

# No key prefix
Config::loadFile('config/settings.php');
echo Config::get('key');

# Prefix filename to key
Config::loadFile('config/db.ini', true);
echo Config::get('db.key');

YAML

To load a single file as YAML see below. If you need to use loadDir() with yaml (or other) files, then read about "Custom Handler" below.

composer require symfony/yaml
use xy2z\LiteConfig\LiteConfig as Config;
use Symfony\Component\Yaml\Yaml;

Config::loadArray(Yaml::parseFile(__DIR__ . '/config/file.yml'));

echo Config::get('key');

Custom Handler

A custom handler can be used for file extensions other than the built in (.php, .json and .ini). This will automatically work when using the loadFile() and loadDir() functions.

Here's how you use the static custom_handler() function if you want YAML support.

use xy2z\LiteConfig\LiteConfig;
use Symfony\Component\Yaml\Yaml;

class CustomLiteConfig extends LiteConfig {

    protected static function custom_handler(string $extension, string $path) {
        if (($extension === 'yml') || ($extension === 'yaml')) {
            return Yaml::parseFile($path);
        }

        // Handle other extensions here...
    }

}

CustomLiteConfig::loadDir(__DIR__ . '/config', true);
var_dump(CustomLiteConfig::all());

If you want to modify the existing handling of all files, you can overwrite the complete getFileContent() function in your child class, which is used by loadDir() and loadFile().

Public Methods

  • get(string $key, $default = null) Get value of key.
  • all() Returns a complete array.
  • exists(string $key) Does key exist?
  • loadDir(string $path, bool $prefix_filename = false, string $prefix = null) Loads all files in dir.
  • loadFile(string $path, bool $prefix_filename = false, string $custom_prefix = null) Load a single file.
  • loadArray(array $array, string $prefix = null) Loads a php array.

Public Properties

  • ini_process_sections (bool, default true) - Argument used in file_parse_ini()
  • ini_scanner_mode (int, default INI_SCANNER_TYPED) - Argument used in file_parse_ini()
  • ignore_unsupported_filestypes (bool, default true) - If true, files with unsupported extensions will be ignored. If false, an exception will be thrown.