Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
Last active December 23, 2021 21:28
Show Gist options
  • Save kmuenkel/142f87f1cc3b45b9d4e92d9771fe9cea to your computer and use it in GitHub Desktop.
Save kmuenkel/142f87f1cc3b45b9d4e92d9771fe9cea to your computer and use it in GitHub Desktop.
Dot notation config traversing
<?php
if (!function_exists('conf')) {
/**
* Look for a dot-delimited name/key reference
* @param string $name
* @return mixed|null
*/
function conf(string $name)
{
$keys = explode('.', $name);
$root = array_shift($keys);
foreach (scandir('../config') as $file) {
if (in_array($file, [$root.'.php', $root.'.inc']) {
$keyVal = fn($configs, string $key) => is_array($configs) ? ($configs[$key] ?? null) : null;
return isset($file) ? array_reduce($keys, $keyVal, require $file) : null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment