Last active
July 14, 2019 07:33
-
-
Save CMCDragonkai/7579059 to your computer and use it in GitHub Desktop.
PHP: Secrets Loader - Place Secrets.php in the root of the project. Loads files within ./secrets/*.php adapting $secrets[] to $_ENV['secrets'][].
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 | |
/** | |
* Loads secrets into the application. Setup secrets via $secrets[], get secrets via $_ENV['secrets'][] | |
*/ | |
class Secrets{ | |
public static function load(){ | |
$secrets_path = __DIR__ . '/secrets'; | |
$secrets_loaded = false; | |
//see if "secrets folder" exists | |
if(file_exists($secrets_path) AND is_dir($secrets_path)){ | |
foreach(new DirectoryIterator($secrets_path) as $file){ | |
//ignore dots and non-php extensions and this file itself | |
if($file->isDot() OR $file->getExtension() != 'php') continue; | |
$secrets_loaded = true; | |
include_once($file->getPathname()); | |
} | |
} | |
if($secrets_loaded){ | |
foreach($secrets as $key => $value){ | |
$_ENV['secrets'][$key] = $value; | |
} | |
unset($secrets); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I ended up using PHP's parse_ini_file() to store secrets.
But this Secrets code is useful.