hladpe/nette-config-finder

Automatically search for configuration files across application.

1.1.0 2019-10-17 07:53 UTC

This package is auto-updated.

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


README

Automatically search for configuration (neon) files across application.

Usage

The configuration files must be set up in the configuration container before the application is initialized. This means connecting the configuration files to the container in bootstrap.php (app/bootstrap.php).

Instead of using static linking of configuration files:

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

We can use dynamic linking of the searched configuration files:

// ~~~
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();
}

The second parameter of the \Hladpe\NetteConfigFinder\Finder is an optional instance of IStorage used to store searched files into cache. We can use Finder without the second parameter of course:

// ~~~
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();
}

We can specify multiple search directories, such as search for configuration files in vendor packages:

// ~~~
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();
}