Skip to content

Instantly share code, notes, and snippets.

@cspray
Created May 1, 2022 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cspray/d6c87dd14bec6a0923da84a3e4ddde1b to your computer and use it in GitHub Desktop.
Save cspray/d6c87dd14bec6a0923da84a3e4ddde1b to your computer and use it in GitHub Desktop.
<?php
use Cspray\AnnotatedContainer\Attribute\Service;
use Cspray\AnnotatedContainer\Attribute\Inject;
use Cspray\AnnotatedContainer\ParameterStore;
use Cspray\Typiphy\Type;
class MySecretParameterStore implements ParameterStore {
public function getName() : string {
return 'secrets';
}
public function fetch(Type $type, string $key) : mixed {
// Something you'd implement to retrieve the value from your secrets store
return fetchFromVaultOrAwsSecretsOrSomewhereElseSafe($key);
}
}
#[Service]
class SomeApiClient {
public function __construct(
#[Inject('my-api-key', from: 'secrets')]
private readonly string $apiKey
) {}
}
$compiler = ContainerDefinitionCompilerFactory::withoutCache()->getCompiler();
$containerDefinition = $compiler->compile(
ContainerDefinitionCompileOptionsBuilder::scanDirectories(__DIR__ . '/src')->build()
);
$containerFactory = new AurynContainerFactory();
$containerFactory->addParameterStore(new MySecretParameterStore());
$container = $containerFactory->createContainer($containerDefinition);
// The value returned from they 'my-api-key' passed to MyParameterStore::fetch will be injected
$myClient = $container->get(SomeApiClient::class);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment