jsc-php/configs

Config file parser and writer

Maintainers

Package info

github.com/jsc-php/Configs

pkg:composer/jsc-php/configs

Fund package maintenance!

jsc-php

Statistics

Installs: 33

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.1 2026-03-05 13:21 UTC

This package is auto-updated.

Last update: 2026-03-05 13:32:48 UTC


README

A simple and flexible PHP library for parsing and writing configuration files. Supports JSON, YAML, INI, and XML formats with ease.

Features

  • Multi-format Support: Seamlessly handle .json, .yaml, .ini, .php, and .xml files.
  • Magic Access: Access and modify configuration data using PHP magic properties.
  • Auto-save: Automatically persists changes back to the file when the object is destroyed (can be disabled)1.
  • Format Conversion: Easily convert configuration files between supported formats.
  • Type Safety: Built for PHP 8.5+ with modern syntax.

Installation

Install via Composer:

composer require jsc-php/configs

Note: Requires the ext-yaml PHP extension for YAML support.

Usage

Basic Example

use JscPhp\Configs\Config;

// Load a config file (format is determined by file extension)
$config = new Config('test.php');

// Get values using magic methods
$db = $config->database;
$db_host = $config->database['host'];

// Get values using get function
$db = $config->get('database');
$db_host = $config->get('database')['host'];
$db_host = $config->get('database','host');
$db_host = $config->get('database.host');


// Set values
$config->debug = true;
$config->set('debug', true);
$config->set('debug.level', 'info');
$config->set(['debug','level'], 'info');

// Changes are automatically saved when $config goes out of scope

Options

You can disable autosave in the constructor:

$config = new Config('config.yaml', ['autosave' => false]);

$config->theme = 'dark';

// Manual save required if autosave is false
$config->save();

Converting Formats

Convert an existing configuration to a different format:

use JscPhp\Configs\Config;
use JscPhp\Configs\Types\Type;

$config = new Config('settings.ini');

// Save as JSON
$config->saveAs('settings.json');

Deleting Keys

$config->delete('key1'); //Deletes array key1
$config->delete('key1.key2'); //Deletes array key2 within key1
$config->delete('key1','key2'); //Deletes array key2 within key1

Supported Formats

  • JSON: Uses native json_encode and json_decode.
  • YAML: Uses yaml_emit and yaml_parse (requires ext-yaml).
  • INI: Uses parse_ini_file with typed scanning and a custom writer supporting sections and multi-value keys.
  • PHP: Uses var_export() to create a return value
  • XML: Uses SimpleXML for parsing and DOM for formatted output with proper indentation.

License

This project is licensed under the GPL-3.0 License.

Authors

Footnotes

  1. To save changes, the config file must be writable