jcambien/kengai

此包已被废弃且不再维护。未建议替代包。

PHP应用的配置工具

1.1.3 2014-07-18 14:00 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:21:26 UTC


README

Kengai

Kengai 是一个简单但强大的 PHP 应用配置工具。欢迎任何人为此项目提供帮助和贡献。

关于此项目

想法很简单

  • 以简单和优化的方式,通过树形结构(YAML、JSON、INI 等)管理任何格式的配置。
  • 所有数据都存储在一个按命名空间排序的通用树中。
  • 可以使用任何缓存系统(如APC)。
  • 事件系统,适用于高级用例,由 Symfony 2 事件调度器组件提供支持。

基础

以下是一个基本使用示例

<?php

use Kengai\Manager as Kengai;
use Kengai\SourceReader\YAML;
use Kengai\SourceReader\JSON;

// Create a Kengai manager instance
$kengai = new Kengai();

// Import your configuration files
$kengai->add(new YAML('myconfig.yml', 'myconfig'));

// Example of JSON support
$kengai->add(new JSON('composer.json', 'composer'));

// Fetch data
$kengai->fetch();

正如您在示例中看到的,在调用 fetch() 之前,所有来源都使用 add() 方法注册。当所有来源都注册完毕后,您可以使用 fetch() 来进行导入过程。之后,您就不能再使用 add() 了!如果已注册缓存管理器,导入过程将首先通过您的缓存管理器验证缓存的新鲜度,然后根据需要恢复或重新加载所有来源。

以下是一个配置读取示例

$bar = $kengai->get('foo.bar');

var_dump($bar);

缓存支持

您可以通过使用 CacheManagerInterface 来使用任何缓存系统

<?php

use Kengai\Manager as Kengai;
use Kengai\SourceReader\YAML;
use Kengai\CacheManager\APC;

// Create a Kengai manager instance with APC support
$kengai = new Kengai(new APC());

...

// Fetch data
$kengai->fetch();