承接 phoole/config 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

phoole/config

Composer 安装命令:

composer require phoole/config

包简介

A slim configuration loader library for PHP.

README 文档

README

Build Status Scrutinizer Code Quality Code Climate PHP 7 Latest Stable Version License

A slim configuration loader library for PHP. It loads PHP, JSON, YAML config files, easy to use, yet powerful. It requires PHP 7.2+ and is compliant with PSR-1, PSR-4, PSR-12.

Installation

Install via the composer utility.

composer require "phoole/config=1.*"

or add the following lines to your composer.json

{
    "require": {
       "phoole/config": "1.*"
    }
}

Features

  • Simple interface, get($id), has($id), with($id, $value).

  • One central place for all config files for ease of management.

    config/
     |
     |___ production/
     |       |
     |       |___ host1/
     |       |      |___ db.php
     |       |      |___ redis.php
     |       |
     |       |___ db.php
     |
     |___ dev/
     |     |
     |     |___ redis.php
     |     |___ db.php
     |
     |___ db.php
     |___ redis.php
     |___ system.php
    
  • May use an environment value, such as production or production/host1 for switching between different configurations.

  • Use of references in configuration value is fully supported, such as ${system.tmpdir}.

  • Get environment values using $config->get('ENV.USER') or ${ENV.USER}

  • Hierarchy configuration structure with dot notation like db.auth.host.

  • Support .php, .json, .yml(need yaml extension installed) type of config files.

Usage

  • Use environment value

    Usually application running environment is different on different servers. A good practice is setting environment in a .env file somewhere on the host, and put all configuration files in one central config/ directory.

    A sample .env file,

    # installation base
    BASE_DIR=/www
    
    # app directory
    APP_DIR=${BASE_DIR}/app
    
    # config directory
    CONFIG_DIR=${APP_DIR}/config
    
    # app env for current host
    APP_ENV=production/host1

    In a sample bootstrap.php file,

    use Phoole\Config\Config;
    use Phoole\Env\Environment;
    
    // load server environment from '.env' file
    (new Environment())->load(__DIR__ . '/.env');
    
    // create config instance with the config file loader
    $config = new Config(getenv('CONFIG_DIR'), getenv('APP_ENV'));
    
    // object access of $config
    $db_config = $config->get('db');
  • Central config directory and configuration grouping

    • Configuration grouping

      Configurations are gathered into one directory and are grouped into files and subdirectories for ease of management.

      For example, the config/system.php holds system.* configurations

      // system.php
      return [
          'tmpdir' => '/usr/local/tmp',
          // ...
      ];

      Later, system related configs can be retrieved as

      $dir = $config->get('system.tmpdir');

      Or being used in other configs as references.

    • Configuration files loading order

      If the environment is set to production/host1, the config files loading order are (assume config files are *.php),

      1. config/config/*.php

      2. config/production/*.php

      3. config/production/host1/*.php

      Configuration values are overwritten and replaced those from later loaded files.

  • Use of references

    References make your configuration easy to manage.

    For example, in the system.php

    return [
        'tmpdir' => '/var/local/tmp',
        ...
    ];

    In your cache.php file,

    return [
        // a local filesystem cache driver
        'local' => [
            'driver' => 'filesystem',
            'params' => [
                'root_dir'   => '${system.tmpdir}/cache', // use reference here
                'hash_level' => 2
            ]
        ],
        ...
    ];

    You may reset the reference start and ending matching pattern as follows,

    // now reference is something like '%{system.tmpdir}%'
    $config->setReferencePattern('%{', '}%');
  • Access environment values

    Environment values can be accessed through special node 'ENV'. e.g.

    $tmpdir = $config->get('ENV.APP_TMPDIR');

    or in reference format,

    return [
        'tmpdir' => '${ENV.APP_TMPDIR}'
    ];
  • DOT notation

    Hierarchy configuration structure with dot notation like db.auth.host.

    // returns the db config array
    $db_config = $config->get('db');
    
    // returns a string
    $db_host = $config->get('db.auth.host');

    Both flat notation and array notation are supported and can co-exist at the same time.

    // db config file
    return [
        // array notation
        'auth' => [
            'host' => 'localhost',
            'port' => 3306
        ],
    
        // flat notation
        'auth.user' => 'dbuser'
    ];
  • After initial loading, $config is immutable. If you want to add new conf values. You may use,

    $newconf = $config->with('redis', ['host' => 'localhost']);

    where $newconf is a new configuration object.

APIs

  • ConfigInterface API

    • get(string $id): mixed

      The return value might be a string, array, or even object. Returns null if not found.

    • has(string $id): bool

      Test if $id exists or not. Returns a boolean value.

    • with(string $id, mixed $value): Config

      Returns a new config object with $id set.

  • ReferenceInterface API

    • setReferencePattern(string $start, string $end): $this

      Reset the reference start chars and ending chars. The default are '${' and '}'

Testing

$ composer test

Dependencies

  • PHP >= 7.2.0

License

phoole/config 适用场景与选型建议

phoole/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 134 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 10 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「library」 「config」 「phoole」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phoole/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phoole/config 我们能提供哪些服务?
定制开发 / 二次开发

基于 phoole/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 134
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 9
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2019-10-07