定制 ckr/config 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ckr/config

Composer 安装命令:

composer require ckr/config

包简介

Simple access to multi dimensional arrays, useful for configuration data

README 文档

README

Build Status

Update 2026

CAUTION: this project is not maintained anymore! DO NOT USE IN PRODUCTION.

Description

This package provides a class to manage hierarchical data. A common use case is configuration data.

Note that this package does not provide any readers or writers for config files of different formats. Instead it wraps php arrays and provides easier access to its values.

Why?

I was tired of using too many isset throghout all my code. The main advantage of this library is, that you can request an arbitrary deeply nested value, and provide a default value which should be returned in case the requested value does not exist:

$myConfig = [
  'root' => [
    'specific' => ['key' => 'val'],
  ],
];

// plain php is a bit ugly...
$theSpecific = isset($myConfig['root']['specific']['key']) ?
     $myConfig['root']['specific']['key'] : null;

$theGeneral = isset($myConfig['root']['key']) ?
     $myConfig['root']['key'] : false;

// ... but with a `Config` it looks nicer:
$config = new Ckr\Config\Config($myConfig);
$theSpecific = $config->get('root/specific/key');
$theGeneral = $config->get('root/key', false);

Actually, if you're using PHP7, this problem is solved:

// valid code in PHP7
$theSpecific= $myConfig['root']['specific']['key'] ?? null;
$theGeneral = $myConfig['root']['key'] ?? false;

However, as I prefer to specify the path as root/specific/key instead of ['root']['specific']['key'], I still use this library in my PHP7 projects.

Usage

You start with a hierarchical data structure, defined as multi dimensional array:

$myConfig = [
  'mode' => 'production',
  'logging' => [
    'factory' => 'Your\\Logging\\Factory',
    'loggers' => [
      [
        'type' => 'file',
        'path' => '/path/to/logs',
        'level' => 'warn',
      ],
      [
        'type' => 'email',
        'addr' => 'someone@somewhere.com',
        'level' => 'critical',
      ]
    ],
  ]
];

Then you can wrap it in a Config object:

use Ckr\Config\Config;

$c = new Config($myConfig);

You can ask for a simple value with get:

$mode = $c->get('mode', 'dev');

In the example above, "dev" is the default mode. It is returned, if the key 'mode' does not exist in the config data.

You can ask for a value which is nested in a child array by providing a path of keys, separated by a Slash /:

$loggingFactoryClass = $c->get('logging/factory');

If any of the used path parts (keys) don't exist, the default value (null if not explicitly specified) is returned.

Say you want to use all of the logging data, e.g. to instantiate your logging instance. For this, you can get a child config object:

$loggingConfig = $c->child('logging'); /* @var $loggingConfig Config */

// we can now provide only the relevant data to thr logging factory
$myLoggingFactory = new $loggingFactoryClass;
$logger = $myLoggingFactory->create($loggingConfig);

// the logging factory "sees" only the data of the "logging" child array
public function create(Config $cfg)
{
  $loggers = $cfg->get('loggers'); // $loggers is an array
  foreach ($loggers as $logger) { /* ... */ }
  // ...
}

To summarize:

  • Config::get will always return the value as it was defined originally. No matter, if the value is an array, a class instance, a function or a scalar value.
  • Config::child expects the given path to point to an (associative) array. It wraps this array in a Config and returns that object.

A new value can be set using Config::set:

$newConfig = $c->set('logging/factory', 'AnotherFactoryClass');

This method returns a new instance with a specific part of the config updated (or newly set). This may be useful if you want to construct a complete configuration object in multiple steps from different parts of your code, e.g. to allow different modules to register their configuration or factories. In the above example, $newConfig has a value of 'AnotherFactoryClass' at the path logging/factory, where the $c instance is not modified and still has the old value. (Note that this behaviour is new in Version 2.0, former versions did update the data in place).

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-11