1. Go to this page and download the library: Download vartroth/secrets-manager library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vartroth / secrets-manager example snippets
use Vartroth\SecretsManager\SecretsManager;
$secrets = new SecretsManager();
// Get a secret with optional default
$dbPassword = $secrets->get('db_password', 'fallback_password');
// Require a secret (throws exception if not found)
$apiKey = $secrets->
use Vartroth\SecretsManager\Secrets;
// Configure once
Secrets::configure(function ($config) {
$config->setEnvPrefix('APP_')
->enableCache()
->prioritizeEnvironmentVars();
});
// Use anywhere
$jwtSecret = Secrets::
use Vartroth\SecretsManager\SecretsConfig;
$manager = (new SecretsConfig())
->setSecretsPath('/custom/secrets/path')
->setEnvPrefix('MYAPP_')
->enableCache(true)
->prioritizeDockerSecrets() // Docker secrets over env vars
->build();
// Get a secret with optional default
get(string $name, ?string $default = null): ?string
// Get a ecrets
getMultiple(array $names, bool $
// Clear all cached secrets
clearCache(): void
// Set secret manually (useful for testing)
set(string $name, string $value): void
// Remove secret from cache
forget(string $name): void
use Vartroth\SecretsManager\SecretsManager;
use Vartroth\SecretsManager\Secrets;
class MyServiceTest extends TestCase
{
private SecretsManager $secrets;
protected function setUp(): void
{
$this->secrets = new SecretsManager();
// Set test secrets
$this->secrets->set('test_api_key', 'test_key_123');
$this->secrets->set('test_db_url', 'sqlite::memory:');
// Configure static facade for testing
Secrets::setInstance($this->secrets);
}
public function testServiceUsesSecrets(): void
{
$service = new MyService();
// Your service will now use the test secrets
$this->assertEquals('test_key_123', $service->getApiKey());
}
}
// Create a secrets manager with custom path for testing
$testSecrets = new SecretsManager('/tmp/test-secrets', false);
// Or use the facade
Secrets::configure(function ($config) {
$config->setSecretsPath('/tmp/test-secrets')
->disableCache();
});