nikoutel/helionconfig

一个多功能的配置解析器。可以处理多种配置格式。

v2.0 2019-07-16 08:46 UTC

This package is not auto-updated.

Last update: 2024-09-21 11:25:41 UTC


README

一个多功能的配置解析器。可以处理多种配置格式。

HelionConfig 是一个用于读取(很快还将用于写入)不同配置类型(INI、XML、JSON、apache、PHP数组、通用conf)的工具,返回整个配置、一个部分或特定的值。

用法

$helionConfig = new HelionConfig();
$configReader = $helionConfig->getConfigReader(ConfigType::XML, $options);
$configSrc = 'file.xml';
$config = $configReader->getConfig($configSrc);
$value = $configReader->getConfigValue($key, $config);
$configAsArray = $config->asArray();
$configAsArrayFlat = $config->asArrayFlat();

参数

ConfigType

一个 ConfigType 参数。

可用类型包括

  • ConfigType::INI
  • ConfigType::XML
  • ConfigType::JSON
  • ConfigType::PHPARRAY (配置存储在一个PHP数组中) *
  • ConfigType::APACHE apache2.conf / httpd.conf 类型的配置)
  • ConfigType::CONF (可配置的通用配置类型,见选项和示例)

* PHPArray文件应仅包含一个配置数据的数组,以及一个返回数组的return语句。

方法 $helionConfig->listConfigTypes() 返回所有可用的配置类型。

configSrc

配置源可以是文件路径、URL或配置字符串。

选项 (可选)

可以将一个选项数组传递给HelionConfiggetConfigReader方法。

数组元素

  • rootName:根对象元素的名称 - 默认:configRoot
  • sectionSeparator:用于表示子部分和值的分隔符 - 默认:.
  • libxmlOptions:用于指定可选的 libxml 参数 - 例如: LIBXML_PARSEHUGE, LIBXML_NOBLANKS
  • jsonOptions:用于指定可选的 json 常量 - 例如: JSON_PRETTY_PRINT, JSON_NUMERIC_CHECK
  • curlOptions:一个指定额外 curlopt 常量的数组 - 例如:
    • CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    • CURLOPT_USERPWD => 'AzureDiamond:hunter2'
  • genericConf:用于指定可配置的通用配置格式的符号 - 默认:
    • 'sectionStart' => "["
    • 'sectionEnd' => "]"
    • 'equals' => "="
    • 'multiLineSeparator' => "\\"
    • 'commentStart' => ";"

选项示例

$options = array(
    'sectionSeparator' => ':',
    'rootName' => 'myConf',
    'libxmlOptions' => array(
        LIBXML_PARSEHUGE, LIBXML_NOBLANKS
    ),
    'jsonOptions' => array(
        JSON_PRETTY_PRINT, JSON_NUMERIC_CHECK
    ),
    'curlOptions' => array(
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
        CURLOPT_USERPWD => 'AzureDiamond:hunter2'
    ),
    'genericConf' => array(
        'sectionStart' => "[",
        'sectionEnd' => "]",
        'equals' => "=",
        'multiLineSeparator' => "\\",
        'commentStart' => "#",
    )
);

返回值

HelionConfig 返回一个表示配置数据的 HelionConfigValue 对象。所有数据都包含在一个名为 configRoot 的根对象元素中(可以通过选项数组进行更改)。

每个指令和每个部分都由一个 HelionConfigValue 对象表示,多个子部分和值被组织在 HelionConfigValue 对象的数组中。

可以通过 HelionConfigValue 对象的 getConfigValue() 方法返回特定的值或子部分。可以通过将子部分与 sectionSeparator 选项数组元素(默认为 .)连接来访问嵌套部分(例如:section1.section2.key

要将 HelionConfigValue 对象转换为数组,可以提供 asArray() 方法。另外,方法 asArrayFlat() 返回一个扁平的数组。部分和值使用 sectionSeparator 分隔。

要求

  • PHP 7(最低)
  • SimpleXML php 扩展
  • libxml php 扩展
  • JavaScript Object Notation php 扩展(json)
  • Client URL Library Object Notation php 扩展(curl)

安装

composer

composer require nikoutel/helionconfig

示例

require '/../vendor/autoload.php';
use Nikoutel\HelionConfig\HelionConfig;
use Nikoutel\HelionConfig\ConfigType\ConfigType;

示例 #1(简单的.ini文件)

; db.ini
[owner]
name=John Doe
organization=Acme Widgets Inc.

[database]
server=192.0.2.62
port=143
$configSrc = 'db.ini';

$helionConfig = new HelionConfig();
$configReader = $helionConfig->getConfigReader(ConfigType::INI, $options);
$config = $configReader->getConfig($configSrc);

$port = $configReader->getConfigValue('database.port', $config);

将输出

$config:
Nikoutel\HelionConfig\HelionConfigValue Object
(
    [helionConfigName] => configRoot
    [helionConfigValue] => Array
        (
            [owner] => Nikoutel\HelionConfig\HelionConfigValue Object
                (
                    [helionConfigName] => owner
                    [helionConfigValue] => Array
                        (
                            [name] => Nikoutel\HelionConfig\HelionConfigValue Object
                                (
                                    [helionConfigName] => name
                                    [helionConfigValue] => John Doe
                                    [helionConfigAttributes] => 
                                )
                            [organization] => Nikoutel\HelionConfig\HelionConfigValue Object
                                (
                                    [helionConfigName] => organization
                                    [helionConfigValue] => Acme Widgets Inc.
                                    [helionConfigAttributes] => 
                                )
                        )
                    [helionConfigAttributes] => 
                )
            [database] => Nikoutel\HelionConfig\HelionConfigValue Object
                (
                    [helionConfigName] => database
                    [helionConfigValue] => Array
                        (
                            [server] => Nikoutel\HelionConfig\HelionConfigValue Object
                                (
                                    [helionConfigName] => server
                                    [helionConfigValue] => 192.0.2.62
                                    [helionConfigAttributes] => 
                                )
                            [port] => Nikoutel\HelionConfig\HelionConfigValue Object
                                (
                                    [helionConfigName] => port
                                    [helionConfigValue] => 143
                                    [helionConfigAttributes] => 
                                )
                        )
                    [helionConfigAttributes] => 
                )
        )
    [helionConfigAttributes] => 
)

$port = $configReader->getConfigValue('database.port', $config);

将输出

$port: 143

示例 #2(作为字符串传递的类似配置的json)

$configSrc = '{ "isbn": "0-13-110362-8",
                "author": [
                     {"firstname": "Brian", "lastname": "Kernighan"},
                     {"firstname": "Dennies", "lastname": "Ritchie"}],
                 "title": "The C Programming Language",
                 "category": ["Programming", "Technology"]}';

$helionConfig = new HelionConfig();
$configReader = $helionConfig->getConfigReader(ConfigType::JSON, $options);

$array = $configReader->getConfig($configSrc)->asArrayFlat();

将输出

Array
(
    [isbn] => 0-13-110362-8
    [author.0.firstname] => Brian
    [author.0.lastname] => Kernighan
    [author.1.firstname] => Dennies
    [author.1.lastname] => Ritchie
    [title] => The C Programming Language
    [category.0] => Programming
    [category.1] => Technology
)

示例 #3(可配置的通用配置类型)

# /etc/mysql/my.cnf
[mysqld_safe]
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
# * Basic Settings
user		= mysql
port		= 3306

MySQL使用INI类型配置文件,但使用非标准的‘#’标记注释

$options = array(
    'genericConf' => array(
        'commentStart' => "#",
    )
);

$helionConfig = new HelionConfig();
$configReader = $helionConfig->getConfigReader(ConfigType::CONF, $options);
$config = $configReader->getConfig('/etc/mysql/my.cnf');

// get only the 'mysqld' section
$mysqldCofigSection = $configReader->getConfigValue('mysqld', $config);

将输出

$mysqldCofigSection:
Nikoutel\HelionConfig\HelionConfigValue Object
(
    [helionConfigName] => mysqld
    [helionConfigValue] => Array
        (
            [user] => Nikoutel\HelionConfig\HelionConfigValue Object
                (
                    [helionConfigName] => user
                    [helionConfigValue] => mysql
                    [helionConfigAttributes] => 
                )
            [port] => Nikoutel\HelionConfig\HelionConfigValue Object
                (
                    [helionConfigName] => port
                    [helionConfigValue] => 3306
                    [helionConfigAttributes] => 
                )
        )
    [helionConfigAttributes] => 
)

// get the 'port' from the 'mysqld' section
$port = $configReader->getConfigValue('port', $mysqldCofigSection);

将输出

$port: 3306

示例 #4 (Apache 虚拟主机配置)

<VirtualHost *:80>
    DocumentRoot /var/www/api
    ServerName api.example.com
    <Directory /var/www/api>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
$helionConfig = new HelionConfig();
$configReader = $helionConfig->getConfigReader(ConfigType::APACHE);
$config = $configReader->getConfig($configSrc);

$array = $config->asArray();
$arrayFlat = $config->asArrayFlat()

将输出

$array:
Array
(
    [helionConfigName] => configRoot
    [helionConfigValue] => Array
        (
            [VirtualHost] => Array
                (
                    [helionConfigName] => VirtualHost
                    [helionConfigValue] => Array
                        (
                            [DocumentRoot] => Array
                                (
                                    [helionConfigName] => DocumentRoot
                                    [helionConfigValue] => /var/www/api
                                )

                            [ServerName] => Array
                                (
                                    [helionConfigName] => ServerName
                                    [helionConfigValue] => api.example.com
                                )

                            [Directory] => Array
                                (
                                    [helionConfigName] => Directory
                                    [helionConfigValue] => Array
                                        (
                                            [AllowOverride] => Array
                                                (
                                                    [helionConfigName] => AllowOverride
                                                    [helionConfigValue] => All
                                                )
                                            [Require] => Array
                                                (
                                                    [helionConfigName] => Require
                                                    [helionConfigValue] => all granted
                                                )
                                        )
                                    [helionConfigAttributes] => /var/www/api
                                )
                        )
                    [helionConfigAttributes] => *:80
                )
        )
)

$arrayFlat:
Array
(
    [VirtualHost.DocumentRoot] => /var/www/api
    [VirtualHost.ServerName] => api.example.com
    [VirtualHost.Directory.AllowOverride] => All
    [VirtualHost.Directory.Require] => all granted
    [VirtualHost.Directory.@attribute] => /var/www/api
    [VirtualHost.@attribute] => *:80
)

待办事项

  • YAML
  • TOML
  • 添加编写/编辑功能(getConfigWriter())
  • 合并资源

许可证

本软件根据MPL 2.0 许可协议授权

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/.