config merge code from laravel 5
This commit is contained in:
parent
f0f272b0f9
commit
d15cf58833
1 changed files with 41 additions and 0 deletions
|
@ -3,6 +3,7 @@
|
||||||
namespace Superbalist\LaravelPrometheusExporter;
|
namespace Superbalist\LaravelPrometheusExporter;
|
||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
use Prometheus\CollectorRegistry;
|
use Prometheus\CollectorRegistry;
|
||||||
use Prometheus\Storage\Adapter;
|
use Prometheus\Storage\Adapter;
|
||||||
|
|
||||||
|
@ -69,4 +70,44 @@ class PrometheusServiceProvider extends ServiceProvider
|
||||||
'prometheus.storage_adapter',
|
'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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue