定制 corneltek/configkit 二次开发

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

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

corneltek/configkit

最新稳定版本:1.6.6

Composer 安装命令:

composer require corneltek/configkit

包简介

Fast config toolkit, which provides super lightweight config accessor and loader.

README 文档

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

ConfigKit compiles your readable YAML config file to PHP files automatically.

YAML format is clean, smart, easy. but in PHP, you have to parse yaml config file in every request. Parsing YAML costs too much CPU time. How to improve it ?

php-ConfigKit is a library for config files and which is designed for performance, it parses yaml config files for the first time, then compiles the config files into php files, so these config files can be cached in PHP, and it can also be in APC or any other cache backend system.

ConfigKit is simple and fast, all you have to do is defining your config file in YAML format, then use ConfigKit to load the config file.

ConfigKit uses static methods because static methods are faster than object methods.

It checks if a {config file}.php exists, if so, then checks the file modification time to decide whether to recompile yaml files.

When APC extension is enabled, PHP source code can be cached in APC, so when you require the pure php source file, it will be faster then reparsing it from yaml.

A generated config PHP file is like:

<?php return array (
      'ApplicationName' => 'Phifty',
      'ApplicationID' => 'phifty',
      'ApplicationUUID' => '9fc933c0-70f9-11e1-9095-3c07541dfc0c',
      'Domain' => 'phifty.dev',

Requirement

YAML extension

Installation

Composer:

{
    "require": { 
        "corneltek/configkit": "~1.5"
    }
}

Usage

ConfigCompiler

To compile a yaml config file and get the config stash:

$config = ConfigCompiler::load('tests/ConfigKit/data/framework.yml');
print_r( $config );

To disable stats check (mtime checking):

ConfigCompiler::$statCheck = false;
$config = ConfigCompiler::load('tests/ConfigKit/data/framework.yml');

If you want to compile the config file manually, you may call the compile static function to do that:

$compiledFile = ConfigCompiler::compile('config/framework.yml');

You may also specify the compiled filename in the arguments:

$compiledFile = ConfigCompiler::compile('config/framework.yml', 'config/framework.php');

You can also override some config values during the compilation, by using the override_compile function:

$compiledFile = ConfigCompiler::override_compile('config/framework.yml', array( 
    'something_should_not_be_in_config_file' => 123123123,
    'something_should_not_be_in_git' => 123123123,
    'something_generated_in_the_runtime' => random(),
));

To test if a compiled file needs to be updated (re-compile):

if ( ConfigCompiler::test('config/framework.yml','config/framework.php')) ) {
    ConfigCompiler::compile(....);
} else {
    // already up to date.
}

ConfigLoader

You can manage multiple config files with ConfigLoader

$loader = new ConfigLoader;
$loader->load( 'framework', 'config/framework.yml' );
$loader->load( 'database', 'config/database.yml' );

To get config stash

$paths = $loader->get('framework','web.paths');
$templates = $loader->get('framework','web.templates');
foreach( $paths as $path ) {
    echo $path, "\n";
}

To write all config stash into one cache file:

$loader->writeStashes('all.php');

To load all config stash back:

$loader->loadStashes('all.php');

Generate AppConfigLoader class

$loader = new ConfigKit\ConfigLoader;
$loader->load('database','tests/data/database.yml');
$loader->load('framework','tests/data/framework.yml');
$appClass = $loader->generateAppClass('MyApp\\AppConfigLoader');
$path = $appClass->generatePsr4ClassUnder('tests');
require_once($path); 
$appConfigLoader = new \MyApp\AppConfigLoader;

统计信息

  • 总下载量: 14.16k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 6
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-03-21