diff --git a/src/Plugin/Compiler.php b/src/Plugin/Compiler.php index e8bcf2f..589b8c3 100644 --- a/src/Plugin/Compiler.php +++ b/src/Plugin/Compiler.php @@ -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( @@ -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), diff --git a/src/Plugin/CompilerOptions.php b/src/Plugin/CompilerOptions.php index b4abff0..aaa00bb 100644 --- a/src/Plugin/CompilerOptions.php +++ b/src/Plugin/CompilerOptions.php @@ -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 { @@ -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), ); @@ -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, ) {} diff --git a/tests/Plugin/CompilerOptionsTest.php b/tests/Plugin/CompilerOptionsTest.php index 2e7f7f1..48d8cde 100644 --- a/tests/Plugin/CompilerOptionsTest.php +++ b/tests/Plugin/CompilerOptionsTest.php @@ -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); + } }