Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Plugin/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static function (ServiceMethodDescriptor $method): File\MethodDescriptor {
yield from $this->doGenerateMessages($generator, $descriptor);
}

if (!$index->empty()) {
if ($options->emitMetadata && !$index->empty()) {
$descriptorName = Naming::descriptorName($source);

yield $generator->generateDescriptorMetadataRegistry(
Expand All @@ -128,7 +128,7 @@ static function (ServiceMethodDescriptor $method): File\MethodDescriptor {
}
}

foreach ($descriptorPaths->groupByNamespace() as $ns => $descriptors) {
foreach ($options->emitMetadata ? $descriptorPaths->groupByNamespace() : [] as $ns => $descriptors) {
$generator = new AutoloadFunctionGenerator(
new FileFactory(
self::createAutoloadFileGeneratedDoc($request),
Expand Down
11 changes: 11 additions & 0 deletions src/Plugin/CompilerOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
private const string OPTION_GRPC = 'grpc';
private const string GRPC_OPTION_CLIENT = 'client';
private const string GRPC_OPTION_SERVER = 'server';
private const string OPTION_METADATA = 'metadata';
private const string METADATA_EMIT = 'emit';
private const string METADATA_NONE = 'none';

public static function fromRequest(CodeGeneratorRequest $request): self
{
Expand All @@ -33,12 +36,19 @@ public static function fromRequest(CodeGeneratorRequest $request): self
self::GRPC_OPTION_SERVER,
];

$metadata = self::doGetString($parameters, self::OPTION_METADATA);

if ($metadata !== self::METADATA_EMIT && $metadata !== self::METADATA_NONE) {
$metadata = self::METADATA_EMIT;
}

$requireGrpcClient = array_any($grpc, static fn(string $target) => $target === self::GRPC_OPTION_CLIENT);
$requireGrpcServer = array_any($grpc, static fn(string $target) => $target === self::GRPC_OPTION_SERVER);

return new self(
requireGrpcClient: $requireGrpcClient,
requireGrpcServer: $requireGrpcServer,
emitMetadata: $metadata === self::METADATA_EMIT,
phpNamespace: self::doGetString($parameters, self::OPTION_PHP_NAMESPACE),
srcPath: self::doGetString($parameters, self::OPTION_SRC_PATH),
);
Expand All @@ -51,6 +61,7 @@ public static function fromRequest(CodeGeneratorRequest $request): self
private function __construct(
public bool $requireGrpcClient,
public bool $requireGrpcServer,
public bool $emitMetadata,
public ?string $phpNamespace = null,
public ?string $srcPath = null,
) {}
Expand Down
11 changes: 11 additions & 0 deletions tests/Plugin/CompilerOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ public function testPhpNamespaceOption(string $parameter, ?string $namespace = n
$options = CompilerOptions::fromRequest(new CodeGeneratorRequest(parameter: $parameter));
self::assertSame($namespace, $options->phpNamespace);
}

#[TestWith(['metadata=none', false])]
#[TestWith(['metadata=emit', true])]
#[TestWith(['metadata=none,metadata=emit', false])]
#[TestWith(['grpc=client,metadata=emit,grpc=server', true])]
#[TestWith(['grpc=client,metadata=none,grpc=server', false])]
public function testMetadata(string $parameter, bool $emitMetadata): void
{
$options = CompilerOptions::fromRequest(new CodeGeneratorRequest(parameter: $parameter));
self::assertSame($emitMetadata, $options->emitMetadata);
}
}