Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active July 14, 2019 07:33
Show Gist options
  • Save CMCDragonkai/7579059 to your computer and use it in GitHub Desktop.
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'][].
<?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);
}
}
}
@hexicle
Copy link

hexicle commented Mar 10, 2016

I ended up using PHP's parse_ini_file() to store secrets.
But this Secrets code is useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment