magicalex / write-ini-file
PHP library to create, read, update, add, remove and erase ini files
Requires
- php: ^8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.5 || ^12.0 || ^13.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
PHP library to create, read, update, add, remove and erase ini files.
Installation
Requires PHP 8.2 or later.
composer require magicalex/write-ini-file
Usage
<?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=false
Read 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();
Methods
| 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. |
Ini files without sections
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.
Scanner mode
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);
Errors
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.
Upgrading from 2.x
- 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.
Contributing
composer install composer test # PHPUnit composer analyse # PHPStan composer cs # check the code style, or `composer cs:fix` to fix it
License
WriteiniFile is released under the GNU General Public License v3.0.