PHP code example of battis / lazy-secrets

1. Go to this page and download the library: Download battis/lazy-secrets 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/ */

    

battis / lazy-secrets example snippets


use Battis\LazySecrets\Secrets;

$data = Secrets::get("MY_APP_SECRET");

use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;

$client = new SecretManagerServiceClient();
$project = $_ENV["GOOGLE_CLOUD_PROJECT"];
$key = "MY_APP_SECRET";
$version = "latest";
$secret = $client->accessSecretVersion(
  "projects/$project/secrets/$key/versions/$version"
);
$data = $secret->getPayload()->getData();

// and even (if you're packing a lot into one secret)
$obj = json_decode($data);

// ...and then using the $data or $obj

use Battis\LazySecrets\Secrets;

$data = Secrets::get("MY_APP_SECRET");

// or
Secrets::init($project, true);
$obj = Secrets::get("MY_APP_SECRET");

use Battis\LazySecrets\Cache;

// assume that the `GOOGLE_CLOUD_PROJECT` environment variable is set
$secrets = new Cache();

$obj = $secrets->get("MY_APP_SECRET");

/** src/Example/DependencyConsumer */

namespace Example;

use Psr\SimpleCache\CacheInterface;

class DependencyConsumer
{
  public function __constructor(CacheInterface $cache)
  {
    // ...
  }
}

/** src/app.php */

$container = new DI\Container([
  Psr\SimpleCache\CacheInterface::class => DI\create(
    \Battis\LazySecrets\Cache::class
  ),
]);
$consumer = $container->get(DependencyConsumer::class);