beatlabs/load 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

beatlabs/load

Composer 安装命令:

composer require beatlabs/load

包简介

PHP library for config loading

README 文档

README

LOAD is a PHP library for configuration loading to APCu

Sources

Available sources for configuration loading are:

  • PHP file
  • Consul
  • Environment variables

Install

You can install load using Composer by running the following:

composer require beatlabs/load

Usage

PHP file:

You can read a PHP file that returns an array.

For example with config.php:

return [
    'var' => [
        'sub-var' => 'sub-val',
        'val',
    ]   
];

You can use the following:

$loader = new BeatLabs\Load\Loader\PHPLoader('config.php'); // Config file
$config = new BeatLabs\Load\Config([$loader]);
$config->load();

// Get configuration values
$val = $config->get('var');

Consul (Default server localhost:8500):

$loader = new BeatLabs\Load\Loader\ConsulLoader();
$config = new BeatLabs\Load\Config([$loader]);
$config->load();

// Get configuration values
$val = $config->get('var');

Consul (Custom server myhost:8000 with root path "services/my-service"):

// You can get all available options here: https://docs.guzzlephp.org/en/6.5/quickstart.html#creating-a-client
$options = [
    "base_uri" => "myhost:8000"
];
$loader = new BeatLabs\Load\Loader\ConsulLoader('services/my-service', $options);

$config = new BeatLabs\Load\Config([$loader]);
$config->load();

// Get configuration values
$val = $config->get('var');

Environment variables:

You can have a prefix for environment variables so that you only include environment variables that start with that prefix. That gives the ability to load only what needed instead of entire environment as a configuration.

// Set variable
$loader = new BeatLabs\Load\Loader\EnvLoader('PREFIX_'); // Define environment variables prefix to be loaded
$config = new BeatLabs\Load\Config([$loader]);
$config->load();

// Get configuration values
$val = $config->get('var');

Multiple loaders:

$consulLoader = new BeatLabs\Load\Loader\ConsulLoader();
$fileLoader = new BeatLabs\Load\Loader\PHPLoader('config.php');
$envLoader = new BeatLabs\Load\Loader\EnvLoader('PREFIX_');
$config = new BeatLabs\Load\Config([$consulLoader, $fileLoader, $envLoader]);
$config->load();

// Get configuration values
$val = $config->get('var');

Loaders are executed in the order they are defined. Each loader will override any configuration loaded from previous loaders.

Custom cache

By default, load uses APCu to cache configuration, but you can use your own cache (ex. Redis, Memcache etc.) by implementing the BeatLabs\Load\Interfaces\Cache interface and set it to Config constructor.

For example:

$cache = new CustomCache();
$loader = new BeatLabs\Load\Loader\PHPLoader('config.php'); // Config file
$config = new BeatLabs\Load\Config([$loader], $cache);
$config->load();

Configuration flattening

Configuration values that have nested sub-values are flattened and can be fetched without further process.

For example:

config.php

return [
    'var' => [
        'sub-var' => 'sub-val',
        'val',
    ]   
];

Will behave like this:

$loader = new BeatLabs\Load\Loader\PHPLoader('config.php'); // Config file
$config = new BeatLabs\Load\Config([$loader]);
$config->load();

// Get configuration values
$val1 = $config->get('var'); // $val = ['sub-var' => 'sub-val, 'val']
$val2 = $config->get('var.sub-var'); // $val = 'sub-val'

The default separator is ., but you can set your own at Config constructor.

For example:

$loader = new BeatLabs\Load\Loader\PHPLoader('config.php'); // Config file
$config = new BeatLabs\Load\Config([$loader], null, '_');
$config->load();

// Get configuration values
$val1 = $config->get('var'); // $val = ['sub-var' => 'sub-val, 'val']
$val2 = $config->get('var_sub-var'); // $val = 'sub-val'

Reload configuration

Configuration can be reloaded by sending SIGUSR2 to PHP running process for CLI scripts and in php-fpm master process for HTTP scripts.

Reloading can also be invoked in code by using the following code:

$config->reload();

How to Contribute

See Contribution Guidelines

Code of conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project and its community you agree to abide by those terms.

Changelog

You can see changelog here

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2021-09-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固