ofcold/configuration

配置项支持Laravel扩展。

1.0.1 2018-06-01 03:16 UTC

This package is auto-updated.

Last update: 2024-08-29 05:06:12 UTC


README

配置项支持Laravel扩展。


简体中文文档


功能

  • 支持组件配置,配置文件可放置在任何位置。
  • 覆盖配置,灵活。

环境

php >= 7.1 Laravel >= 5.1

安装

    composer require ofcold/configuration

说明

我们可能遇到这样的场景,在Laravel组件的开发过程中,需要一些配置,或多个配置项。原始的Laravel可能要求你合并配置并发布到根目录。随着组件数量的增加,配置文件的数量也会增加。

使用

use Ofcold\Configuration\LoaderConfiguration;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Config\Repository;

$loader = new LoaderConfiguration(
	$config = new Repository,
	new Filesystem
);

$loader->addNamespace('test', __DIR__ . '/tests/config');

print_r(json_encode($config->all()));
// print_r($config->get('test::test.foo') . "\r\n");

$loader->addNamespaceOverrides('test', __DIR__ . '/tests/overrides');

print_r(json_encode($config->all()));
// print_r($config->get('test::test.foo')  . "\r\n");

结果

{
	"test::test":{
		"foo":"example"
	}
}

{
	"test::test":{
		"foo":"overrides"
	}
}

Laravel

use Ofcold\Configuration\LoaderConfiguration;

class Foo
{
	/**
	 * Create an a new Foo instance.
	 *
	 * @param LoaderConfiguration $loader
	 */
	public function __construct(LoaderConfiguration $loader)
	{
		$loader->addNamespace('test', '/config');
	}
}

或测试文件。

    php test

API

  • addNamespace(?string $namespace = null, string $directory) : void
  • addNamespaceOverrides($namespace, $directory) : void