amattu2/ini-parser

一个用错误冗余构建的 PHP-7 .ini 文件解析器

v1.0.1 2023-03-09 22:26 UTC

This package is auto-updated.

Last update: 2024-09-10 01:39:16 UTC


README

这是一个使用 PHP 构建的简单配置/.ini 文件解析器。它旨在错误安全,并提供如果找不到值时使用默认值的机会。

用法

安装

composer require amattu/IniParser

设置

Composer 项目使用

require(__DIR__ . "/vendor/autoload.php");

$config = new amattu\IniParser(__DIR__ . "/conf.ini");

不使用 Composer

require(__DIR__ . "/src/IniParser.php");

$config = new amattu\IniParser(__DIR__ . "/conf.ini");

构造函数

类构造函数接受文件位置(路径+名称),以及一个可选的布尔参数来解析单独的部分(通常表示为 [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 函数将尝试从指定的部分(如果指定)中拉取值,如果没有找到,则返回默认参数。除了 $key 之外的所有参数都是可选的。

用法

$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

要求 & 依赖

  • PHP 7.x +