PHP library to create, read, update, add, remove and erase ini files.
Requires PHP 8.2 or later.
composer require magicalex/write-ini-file<?php
require_once 'vendor/autoload.php';
use WriteiniFile\ReadiniFile;
use WriteiniFile\WriteiniFile;
$data = [
'fruit' => ['orange' => '100g', 'fraise' => '10g'],
'legume' => ['haricot' => '20g', 'oignon' => '100g'],
'jus' => ['orange' => '1L', 'pomme' => '1,5L', 'pamplemousse' => '0,5L'],
];
(new WriteiniFile('file.ini'))
->create($data)
->add(['music' => ['rap' => true, 'rock' => false]])
->rm(['jus' => ['pomme' => '1,5L']])
->update(['fruit' => ['orange' => '200g']])
->write();file.ini now contains:
[fruit]
orange=200g
fraise=10g
[legume]
haricot=20g
oignon=100g
[jus]
orange=1L
pamplemousse=0,5L
[music]
rap=true
rock=falseRead it back:
var_dump(ReadiniFile::get('file.ini'));
/*
array(4) {
["fruit"]=> array(2) { ["orange"]=> string(4) "200g" ["fraise"]=> string(3) "10g" }
["legume"]=> array(2) { ["haricot"]=> string(3) "20g" ["oignon"]=> string(4) "100g" }
["jus"]=> array(2) { ["orange"]=> string(2) "1L" ["pamplemousse"]=> string(4) "0,5L" }
["music"]=> array(2) { ["rap"]=> string(4) "true" ["rock"]=> string(5) "false" }
}
*/Empty it:
(new WriteiniFile('file.ini'))->erase()->write();| Method | Description |
|---|---|
create(array $data) |
Replaces the whole content. |
update(array $data) |
Changes existing values and adds the missing ones. |
add(array $data) |
Adds values. A key that already exists keeps its value and gets the new one too, as an array. |
rm(array $data) |
Removes the given keys, whatever their values. rm(['jus' => true]) removes the whole section. |
erase() |
Removes all the content. |
write() |
Writes the file. Nothing is saved until you call it. |
Pass processSections: false to read or write a file whose keys are not grouped in sections:
name=WriteiniFile
debug=false(new WriteiniFile('config.ini', processSections: false))
->update(['debug' => true])
->write();
ReadiniFile::get('config.ini', processSections: false);
// ['name' => 'WriteiniFile', 'debug' => 'true']With sections (the default), keys that are not in a section are written at the top of the file.
Values are read as raw strings (INI_SCANNER_RAW) by default. Pass INI_SCANNER_TYPED to get booleans, integers and null, or INI_SCANNER_NORMAL for PHP's default behavior. See parse_ini_file().
ReadiniFile::get('file.ini', INI_SCANNER_TYPED);
new WriteiniFile('file.ini', INI_SCANNER_TYPED);Every error throws a WriteiniFile\IniFileException, which extends RuntimeException:
ReadiniFile::get()on a missing file;- a file that cannot be parsed;
- a file that cannot be written;
- a value nested deeper than one level of arrays, which the ini format cannot represent.
- PHP 8.2 or later is required.
- Errors throw
WriteiniFile\IniFileExceptioninstead ofException. It extendsException, so existingcatch (\Exception $e)blocks still work, and the messages are unchanged. - Parameters are typed: the path must be a string and the scanner mode an integer.
ReadiniFileisfinal, and the properties ofWriteiniFileare private. Fluent methods declare astaticreturn type andwrite()declaresbool, so subclasses that override them must declare the same.- Keys outside any section are written at the top of the file. Before, each one was written as an empty section and its value was lost.
rm()with a non-array value for a section removes the whole section. Before, it raised aTypeError.- A value nested too deeply throws an
IniFileException. Before,Arraywas written to the file.
composer install
composer test # PHPUnit
composer analyse # PHPStan
composer cs # check the code style, or `composer cs:fix` to fix itWriteiniFile is released under the GNU General Public License v3.0.