config merge code from laravel 5

This commit is contained in:
Bertus Steenberg 2019-04-28 12:23:20 +02:00
parent f0f272b0f9
commit d15cf58833

View file

@ -3,6 +3,7 @@
namespace Superbalist\LaravelPrometheusExporter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Arr;
use Prometheus\CollectorRegistry;
use Prometheus\Storage\Adapter;
@ -69,4 +70,44 @@ class PrometheusServiceProvider extends ServiceProvider
'prometheus.storage_adapter',
];
}
/**
* https://medium.com/@koenhoeijmakers/properly-merging-configs-in-laravel-packages-a4209701746d
* :heart:
*
* Merge the given configuration with the existing configuration.
*
* @param string $path
* @param string $key
* @return void
*/
protected function mergeConfigFrom($path, $key)
{
$config = $this->app['config']->get($key, []);
$this->app['config']->set($key, $this->mergeConfig(require $path, $config));
}
/**
* Merges the configs together and takes multi-dimensional arrays into account.
*
* @param array $original
* @param array $merging
* @return array
*/
protected function mergeConfig(array $original, array $merging)
{
$array = array_merge($original, $merging);
foreach ($original as $key => $value) {
if (! is_array($value)) {
continue;
}
if (! Arr::exists($merging, $key)) {
continue;
}
if (is_numeric($key)) {
continue;
}
$array[$key] = $this->mergeConfig($value, $merging[$key]);
}
return $array;
}
}