spacetab-io/configuration-php

PHP微服务的配置模块。

4.0.1 2022-04-11 14:27 UTC

This package is auto-updated.

Last update: 2024-09-11 19:28:10 UTC


README

CircleCI codecov

支持多个应用程序阶段(如devprod或其他),并覆盖defaults阶段的PHP配置模块。

安装

composer require spacetab-io/configuration

版本映射

  • 版本为4.*的包需要PHP >= 8.1
  • 版本为3.*的包需要PHP >= 7.4 且 <= 8.0
  • 版本为2.*的包需要PHP < 7.4

使用

默认情况下,配置目录的路径和应用程序阶段从/app/configuration加载,使用local阶段。

  1. 简单
<?php
use Spacetab\Configuration\Configuration;

$conf = new Configuration();
$conf->load();

var_dump($conf->all()); // get all config
echo $conf->get('foo.bar'); // get nested key use dot notation
echo $conf['foo.bar']; // the same, but use ArrayAccess interface.

支持带星号的点符号语法,在get方法中使用。您可以在这里了解更多:[https://github.com/spacetab-io/obelix-php](https://github.com/spacetab-io/obelix-php)

  1. 如果需要覆盖默认值,可以在类构造函数中传递两个参数或设置使用setter。
<?php
use Spacetab\Configuration\Configuration;

$conf = new Configuration(__DIR__ . '/configuration', 'test');
$conf->load();

$conf->get('key'); // full example on the top
  1. 如果操作系统有环境变量CONFIG_PATHSTAGE,则包的值将从那里获取。
export CONFIG_PATH=/configuration
export STAGE=prod
<?php
use Spacetab\Configuration\Configuration;

$conf = new Configuration();
$conf->load(); // loaded files from /configuration for prod stage.

$conf->get('key'); // full example on the top
  1. 如果您想查看日志和了解加载过程,请将应用程序日志传递到以下方法中
<?php
use Spacetab\Configuration\Configuration;

$conf = new Configuration();
$conf->setLogger($monolog); // PSR compatible logger.
$conf->load();

$conf->get('key'); // full example on the top

如果您想使用此包进行日志记录

composer require spacetab-io/logger

就是这样。

CLI工具

您还可以安装一个简单的cli-utility,以转储合并后的配置的总结果。有多种方式可以实现

  1. 将其安装到/usr/local/bin作为全局二进制文件
L=/usr/local/bin/st-conf && sudo curl -L https://github.com/spacetab-io/configuration-php/releases/download/2.2.0/st-conf.phar -o $L && sudo chmod +x $L
  1. 将库作为全局composer要求安装

第一步

composer global require spacetab-io/configuration-php

它将被安装到~/.composer目录。

如果您在全局路径中具有~/.composer/vendor/bin,您可以尝试运行以下命令

st-conf help dump

否则,您可以注册该目录

echo 'export PATH=~/.composer/vendor/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile

CLI使用

Description:
  Dump loaded configuration

Usage:
  dump [options] [--] [<path> [<stage>]]

Arguments:
  path                   Configuration directory path
  stage                  Configuration $STAGE

Options:
  -l, --inline[=INLINE]  The level where you switch to inline YAML [default: 10]
  -s, --indent[=INDENT]  The amount of spaces to use for indentation of nested nodes [default: 2]
  -d, --debug            Debug
  -h, --help             Display this help message
  -q, --quiet            Do not output any message
  -V, --version          Display this application version
      --ansi             Force ANSI output
      --no-ansi          Disable ANSI output
  -n, --no-interaction   Do not ask any interactive question
  -v|vv|vvv, --verbose   Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  Example of usage: `st-conf dump`. Options --inline=10 (nesting level) and --indent=2. If [path] and [stage] arguments not passed will be used global env variables CONFIG_PATH and STAGE.

依赖关系

  • >= PHP 8.1
  • 使用Composer安装包

许可

MIT许可

版权所有 © 2022 spacetab.io, Inc. [https://spacetab.io](https://spacetab.io)

特此授予任何获得本软件及其相关文档文件(“软件”)副本的人免费使用软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许向软件提供的人这样做,但受以下条件约束

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是在合同行为、侵权行为或其他行为中产生的,是否因软件或其使用或其他方式与之相关。