senhung/config-read-write

该包已被废弃且不再维护。未建议替代包。

用于读写配置文件的包

v2.0.0 2018-05-15 14:54 UTC

This package is not auto-updated.

Last update: 2020-01-24 17:53:06 UTC


README

描述

一个用于快速、方便地读取和设置配置的PHP包。

设置

  1. 添加依赖
$ composer require senhung/config-read-write
  1. 添加配置文件

创建文件并输入配置信息

例如

# Some Comments
APP_NAME=config-read
VERSION=2.0.0

如何使用

初始化

Configruation::initializeConfigs(
    [string $configFilePath [, bool $absolutePath [, string $separator]]]
): void

$configFilePath:配置文件所在的位置(默认:'.env'

$absolutePath:定义的路径是绝对路径还是相对路径(默认:true

$separator:配置键和值之间的分隔符(默认:'='

配置条目

如果您想指定默认以外的配置文件名或分隔符,请在程序的主入口中添加以下代码

<?php

require_once 'vendor/autoload.php';

use Senhung\Config\Configuration;

/* Initialize config array in Configuration class */
Configuration::initializeConfigs('config_file_path', true, 'separator');

注意:如果您使用.env作为配置路径且=作为分隔符,则不需要此文件。

注释配置

在配置文件中使用#注释一行

# This is a comment, will not be read by the tool
OTHER_CONFIG=will-be-read

注意:设置配置将覆盖原始配置文件中的注释和空行

读取配置

Configuration::read('<config-you-want-to-read>');

写入配置

Configuration::set('<config-you-want-to-write>', '<change-to>');

注意:设置配置将覆盖原始配置文件中的注释和空行

示例

配置文件

# App Configs
APP_NAME=config-read
VERSION=2.0.0

配置读取和写入

<?php

require_once 'vendor/autoload.php';

use Senhung\Config\Configuration;

/* Read config APP_NAME */
echo Configuration::read('APP_NAME') . "\n";

/* Read config VERSION */
echo Configuration::read('VERSION') . "\n";

/* Set APP_NAME to config-write */
Configuration::set('APP_NAME', 'config-write');

/* Read APP_NAME again to see the changes */
echo Configuration::read('APP_NAME') . "\n";

输出

config-read
2.0.0
config-write