amattu2 / ini-parser
A PHP-7 .ini file parser built with error-redundancies
v1.0.1
2023-03-09 22:26 UTC
This package is auto-updated.
Last update: 2025-01-10 02:24:54 UTC
README
This is a basic configuration/.ini
file parser built with PHP. It's designed to be error-safe, and provides the opportunity to use a default value if one is not found.
Usage
Install
composer require amattu/IniParser
Setup
Composer project usage
require(__DIR__ . "/vendor/autoload.php"); $config = new amattu\IniParser(__DIR__ . "/conf.ini");
Without composer
require(__DIR__ . "/src/IniParser.php"); $config = new amattu\IniParser(__DIR__ . "/conf.ini");
construct
The class constructor accepts the file location (path + name), and an optional boolean argument on parsing the individual sections (usually denoted by [NAME]
).
PHPDoc
/** * Configuration reader constructor * * @param string $file The path to the configuration file * @param ?bool $sections Whether to parse individual sections or not * @throws TypeError * @author Alec M. */ public function __construct(string $filename, ?bool $sections = false)
get
The get
function will attempt to pull the value from the section specified (... if specified), and if not found, will return the default parameter. All of the arguments except $key
are optional.
Usage
$usernameOrDefault = $config->get("USERNAME", "GMAIL_SECTION", "defaultUserName");
PHPDoc
/** * Get configuration file property * * @param string $key The property to get * @param string|null $section The section to get the property from * @param ?mixed $default value to return if the property is not found * @return $config[$key] || $default || null * @throws TypeError * @author Alec M. */ public function get(string $key, ?string $section = null, ?mixed $default = null) : mixed
Requirements & Dependencies
- PHP 7.x +