achrome/conphig

简单的PHP配置管理器

0.2.0 2014-06-17 17:51 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:16:44 UTC


README

这是一个简单的配置生成器,可以将不同类型的配置文件解析成对象。其背后的想法是标准化配置对象,并允许您根据需要混合匹配配置文件。

目前支持的文件类型只有INI、XML和JSON。可以通过自定义配置添加YAML支持

Build Status Coverage Status

安装

使用Composer

只需将此添加到您的 composer.json

{
	"require": {
		"achrome/conphig": "*"
	}
}

或者,您可以使用composer CLI如下

$ php composer.phar require 'achrome/conphig=*'

或者

$ composer require 'achrome/conphig=*'

然后,在应用引导中,只需 require 'vendor/autoload.php'

不使用Composer(说实话,为什么?)

您可以直接克隆此存储库并使用它,如下所示

$ mkdir vendor && cd vendor
$ git clone https://github.com/Achrome/Conphig.git

然后,您只需在引导文件中 require 'Conphig/autoload.php'

用法

在应用引导中,require 'Conphig/autoload.php' 设置自动加载器,或者如果您使用Composer,只需 require 'vendor/autoload.php'

use Conphig\Factories\ConfigurationFactory;

$configCreator = new ConfigurationFactory;
$configuration = $configCreator->setConfigPath('/path/to/config/dir')
					       ->setConfigFileName('configFileName')
					       ->setConfigType('filetype')
					       ->create();

或者,如果您更喜欢走更简单的路线

use Conphig\Factories\ConfigurationFactory;

$configCreator = new ConfigurationFactory;
$configuration = $configCreator->create('/path/to/config/file.ext');

默认情况下,它将使用文件名 config 和类型 ini,因此它只需要路径。

如果是这种情况,您只需这样做。

use Conphig\Factories\ConfigurationFactory;

$configCreator = new ConfigurationFactory('/path/to/config/dir');
$configuration = $configCreator->create();

例如,如果config.ini看起来像这样,

[database]
engine = mysql
host = localhost
dbname = test
user = root
password = FooBar1234

当通过Conphig解析时

use Conphig\Factories\ConfigurationFactory;

$configCreator = new ConfigurationFactory('/path/to/config/dir');
$configuration = $configCreator->create();

echo $configuration->database->engine; //Will output mysql

自定义配置器

您可以通过扩展 Conphig\Configurators\AbstractConfigurator 来注册自己的配置系统,如下所示

namespace Foo;

use Conphig\Configurators\AbstractConfigurator;

class BarConfigurator extends AbstractConfigurator {

	public function parseConfig() {
		//The file name is saved in AbstractConfigurator::filePath and can be used here to write your own logic to parse the file.
		//Save the configuration in AbstractConfigurator::configuration for the factory to be able to return it.
	}
}

然后,您需要注册自定义处理程序,它将被设置为将要使用的配置器

use Conphig\Factories\ConfigurationFactory;

$configCreator = new ConfigurationFactory('/path/to/custom/config/dir');
$configuration = $configCreator->registerConfigHandler('custom', 'Foo\\BarConfigurator')->create();

许可

MIT

贡献

只需添加您想要的任何新功能并提交拉取请求!如果您看到问题,只需在 issues 中打开它 注意:请在提交之前运行 grunt 以确保代码风格和单元测试通过

联系

Github 邮件