Last active
December 23, 2021 21:28
-
-
Save kmuenkel/142f87f1cc3b45b9d4e92d9771fe9cea to your computer and use it in GitHub Desktop.
Dot notation config traversing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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