Skip to content

Commit

Permalink
Merge pull request #103 from spiral/feature/client-interceptors
Browse files Browse the repository at this point in the history
Inject interceptors to a GRPC clients
  • Loading branch information
butschster committed May 20, 2024
2 parents 191042e + 6ae8b59 commit e7709ed
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
17 changes: 10 additions & 7 deletions src/GRPC/Generator/BootloaderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,19 @@ private function addSingleton(Method $servicesMethod, FileDeclaration $bootloade
<<<'EOL'
$container->bindSingleton(
%s::class,
static function(GRPCServicesConfig $config): %s
static function(GRPCServicesConfig $config) use($container): %s
{
$service = $config->getService(%s::class);
$core = new InterceptableCore(new ServiceClientCore(
$service['host'],
['credentials' => $service['credentials'] ?? $config->getDefaultCredentials()]
));
return new %s(
new InterceptableCore(new ServiceClientCore(
$service['host'],
['credentials' => $service['credentials'] ?? $config->getDefaultCredentials()]
))
);
foreach ($config->getInterceptors() as $interceptor) {
$core->addInterceptor($container->get($interceptor));
}
return $container->make(%s::class, ['core' => $core]);
}
);
EOL,
Expand Down
7 changes: 6 additions & 1 deletion src/GRPC/Generator/ConfigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function run(array $files, string $targetPath, string $namespace): void

$config->addConstant('CONFIG', 'grpcServices')->setPublic();
$config
->addProperty('config', ['services' => []])
->addProperty('config', ['services' => [], 'interceptors' => []])
->setProtected()
->setType('array')
->setComment('@var array<class-string, array{host: string, credentials?: mixed}>');
Expand All @@ -47,6 +47,11 @@ public function run(array $files, string $targetPath, string $namespace): void
->setPublic()
->addBody('return ChannelCredentials::createInsecure();')
->setReturnType('mixed');
$config
->addMethod('getInterceptors')
->setPublic()
->setBody('return $this->config[\'interceptors\'];')
->setReturnType('array');
$config
->addMethod('getService')
->addComment('Get service definition.')
Expand Down
17 changes: 10 additions & 7 deletions tests/app/GRPC/Generator/Bootloader/ExpectedBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,19 @@ private function initServices(Container $container): void
{
$container->bindSingleton(
UsersServiceInterface::class,
static function(GRPCServicesConfig $config): UsersServiceInterface
static function(GRPCServicesConfig $config) use($container): UsersServiceInterface
{
$service = $config->getService(UsersServiceClient::class);
$core = new InterceptableCore(new ServiceClientCore(
$service['host'],
['credentials' => $service['credentials'] ?? $config->getDefaultCredentials()]
));

return new UsersServiceClient(
new InterceptableCore(new ServiceClientCore(
$service['host'],
['credentials' => $service['credentials'] ?? $config->getDefaultCredentials()]
))
);
foreach ($config->getInterceptors() as $interceptor) {
$core->addInterceptor($container->get($interceptor));
}

return $container->make(UsersServiceClient::class, ['core' => $core]);
}
);
}
Expand Down
7 changes: 6 additions & 1 deletion tests/app/GRPC/Generator/Config/ExpectedConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ class GRPCServicesConfig extends InjectableConfig
public const CONFIG = 'grpcServices';

/** @var array<class-string, array{host: string, credentials?: mixed}> */
protected array $config = ['services' => []];
protected array $config = ['services' => [], 'interceptors' => []];

public function getDefaultCredentials(): mixed
{
return ChannelCredentials::createInsecure();
}

public function getInterceptors(): array
{
return $this->config['interceptors'];
}

/**
* Get service definition.
* @return array{host: string, credentials?: mixed}
Expand Down

0 comments on commit e7709ed

Please sign in to comment.