hladpe/nette-config-finder

在应用程序中自动搜索配置文件。

此软件包的官方仓库似乎已不存在,因此软件包已被冻结。

1.1.0 2019-10-17 07:53 UTC

This package is auto-updated.

Last update: 2024-05-17 17:37:22 UTC


README

在应用程序中自动搜索配置(neon)文件。

用法

配置文件必须在应用程序初始化之前设置到配置容器中。这意味着在 bootstrap.phpapp/bootstrap.php)中将配置文件连接到容器。

而不是使用静态链接配置文件

// ~~~
$configurator->addConfig(__DIR__ . '/config/config.neon');
$configurator->addConfig(__DIR__ . '/config/config.local.neon');
// ~~~

我们可以使用动态链接搜索到的配置文件

// ~~~
foreach (getNetteConfigs() as $path) {
	$configurator->addConfig($path);
}
// ~~~
		
function getNetteConfigs(): array
{
	$cachePath = '..' . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'cache';
	$storage = new \Nette\Caching\Storages\FileStorage($cachePath);
	$config = (new \Hladpe\NetteConfigFinder\Configuration())
		->addPath(__DIR__);
	
	$finder = new \Hladpe\NetteConfigFinder\Finder($config, $storage);
	return $finder->find();
}

\Hladpe\NetteConfigFinder\Finder 的第二个参数是一个可选的 IStorage 实例,用于将搜索到的文件存储到缓存中。当然,我们也可以不使用第二个参数使用 Finder。

// ~~~
foreach (getNetteConfigs() as $path) {
	$configurator->addConfig($path);
}
// ~~~
		
function getNetteConfigs(): array
{
	$config = (new \Hladpe\NetteConfigFinder\Configuration())
		->addPath(__DIR__);
	
	$finder = new \Hladpe\NetteConfigFinder\Finder($config);
	return $finder->find();
}

我们可以指定多个搜索目录,例如在 vendor 软件包中搜索配置文件。

// ~~~
foreach (getNetteConfigs() as $path) {
	$configurator->addConfig($path);
}
// ~~~
		
function getNetteConfigs(): array
{
	$config = (new \Hladpe\NetteConfigFinder\Configuration())
		->addPath(__DIR__);
		->addPath('..' . DIRECTORY_SEPARATOR . 'vendor');
	
	$finder = new \Hladpe\NetteConfigFinder\Finder($config);
	return $finder->find();
}