jascha030/config-lib

1.0.2 2021-08-08 19:13 UTC

This package is auto-updated.

Last update: 2024-09-09 01:36:14 UTC


README

描述:处理PHP数组配置文件的最小化库。

入门指南

先决条件

  • Php ^7.4 || ^8.0
  • Composer ^2

安装

composer require jascha030/config-lib

分发

构建分发的替代步骤。

composer require --no-dev jascha030/config-lib

使用方法

该库基于ConfigStoreInterface及其主要实现ConfigStore。以下是一些简单的示例,涵盖了所有功能。

<?php

use Jascha030\ConfigurationLib\Config\ConfigStore;

//  Load composers autoloader. 
require_once __DIR__ . '/vendor/autoload.php';

// Directory in which your config files reside.
$dir = __DIR__ . '/path/to/config/files';
$configStore = new ConfigStore($dir);

// Make sure to call the load method before retrieving values.
$configStore->load();

// Retrieve value firstName from any of the config files in your directory.
if ($configStore->keyExists('firstName')) {
    $firstName = $configStore->get('firstName');
}

// Alternatively specify the exact file, using dot notation to retrieve firstName from /path/to/config/files/user.php
$configStore->get('user.firstName');

// Retrieve all values from /path/to/config/files/user.php
if ($configStore->configFileExists('user')) {
    $userConfig = $configStore->getConfig('user');
    $firstName  = $userConfig['firstName'];
}

// You can get the config file from an option's key.
$configFile = $configStore->getFileByOptionKey('firstName');

// You can validate if an option exists in a given file
$configStore->hasKey('user', 'firstName');

测试

包含该包的是一套使用phpunit/phpunit编写的单元测试。为了方便使用,定义了一个Composer脚本来运行测试。

composer run phpunit

代码覆盖率报告生成在项目的根目录下,文件名为cov.xml。默认情况下,cov.xml文件不会被.gitignore忽略。

代码风格 & 格式化

包含了一个为friendsofphp/php-cs-fixer定义的代码风格配置,位于.php-cs-fixer.dist.php

要使用php-cs-fixer而无需全局安装,也包含了一个Composer脚本来格式化PHP代码,使用提供的配置文件和php-cs-fixer的供应商二进制文件。

composer run format

注意:https://mlocati.github.io/php-cs-fixer-configurator/#version:3.0 是一个实用的工具,可以组合并导出自定义的代码风格配置,我鼓励任何感兴趣的人使用这个工具。

许可证

此Composer包是开源软件,受MIT许可证许可。