public function testService() { $serviceDesc = new ServiceDescriptorProto(); $methodDesc = new MethodDescriptorProto(); // rpc search (SearchRequest) returns (SearchResponse); $methodDesc->setName('search'); $methodDesc->setInputType('.ProtobufCompilerTest.Protos.Service.SearchRequest'); $methodDesc->setOutputType('.ProtobufCompilerTest.Protos.Service.SearchResponse'); $serviceDesc->setName('SearchService'); $serviceDesc->addMethod($methodDesc); $context = $this->createContext([['name' => 'service.proto', 'package' => 'ProtobufCompilerTest.Protos.Service', 'values' => ['services' => [$serviceDesc], 'messages' => [['name' => 'SearchRequest', 'fields' => []], ['name' => 'SearchResponse', 'fields' => []]]]]]); $expected = $this->getFixtureFileContent('Service/SearchService.tpl'); $className = 'ProtobufCompilerTest.Protos.Service.SearchService'; $entity = $context->getEntity($className); $generator = new Generator($context); $generator->visit($entity); // file_put_contents(__DIR__ . '/Fixtures/Service/SearchService.tpl', $entity->getContent()); $this->assertEquals($expected, $entity->getContent()); }
protected function compileStub(proto\ServiceDescriptorProto $service, $ns) { $s = array(); $s[] = 'namespace ' . $this->normalizeNS($ns) . ' {'; $s[] = ''; $s[] = " // @@protoc_insertion_point(scope_namespace)"; $s[] = " // @@protoc_insertion_point(namespace_{$ns})"; $s[] = ''; $cmt = $this->compiler->getComment($ns . '.' . $service->getName(), ' * '); if ($cmt) { $s[] = " /**"; $s[] = $cmt; $s[] = " */"; } $s[] = ' class ' . $service->getName() . 'Client extends \\Grpc\\BaseStub {'; $s[] = ''; $s[] = ' public function __construct($hostname, $opts, $channel = null) {'; $s[] = ' parent::__construct($hostname, $opts, $channel);'; $s[] = ' }'; foreach ($service->getMethodList() as $method) { $ns_input = $this->normalizeNS($method->getInputType()); $ns_output = $this->normalizeNS($method->getOutputType()); $s[] = ' /**'; $cmt = $this->compiler->getComment($ns . '.' . $service->getName() . '.' . $method->getName(), ' * '); if ($cmt) { $s[] = $cmt; $s[] = ' * '; } $s[] = ' * @param ' . $ns_input . ' $input'; $s[] = ' */'; $server_stream = $method->getServerStreaming(); $client_stream = $method->getClientStreaming(); $service_fqn = $ns . '.' . $service->getName(); if ($client_stream) { if ($server_stream) { $s[] = ' public function ' . $method->getName() . '($metadata = array(), $options = array()) {'; $s[] = ' return $this->_bidiRequest(\'/' . $service_fqn . '/' . $method->getName() . '\', \'\\' . $ns_output . '::deserialize\', $metadata, $options);'; } else { $s[] = ' public function ' . $method->getName() . '($metadata = array(), $options = array()) {'; $s[] = ' return $this->_clientStreamRequest(\'/' . $service_fqn . '/' . $method->getName() . '\', \'\\' . $ns_output . '::deserialize\', $metadata, $options);'; } } else { if ($server_stream) { $s[] = ' public function ' . $method->getName() . '($argument, $metadata = array(), $options = array()) {'; $s[] = ' return $this->_serverStreamRequest(\'/' . $service_fqn . '/' . $method->getName() . '\', $argument, \'\\' . $ns_output . '::deserialize\', $metadata, $options);'; } else { $s[] = ' public function ' . $method->getName() . '(\\' . $ns_input . ' $argument, $metadata = array(), $options = array()) {'; $s[] = ' return $this->_simpleRequest(\'/' . $service_fqn . '/' . $method->getName() . '\', $argument, \'\\' . $ns_output . '::deserialize\', $metadata, $options);'; } } $s[] = ' }'; } $s[] = ' }'; $s[] = '}'; return implode(PHP_EOL, $s) . PHP_EOL; }
/** * @param string $name * @param array $values * * @return \google\protobuf\ServiceDescriptorProto */ protected function createServiceDescriptorProto($name, array $values = []) { $descriptor = new ServiceDescriptorProto(); $descriptor->setName($name); return $descriptor; }
protected function compileService(proto\ServiceDescriptorProto $service, $ns) { $s = array(); $s[] = 'namespace ' . $this->normalizeNS($ns) . ' {'; $s[] = ''; $s[] = " // @@protoc_insertion_point(scope_namespace)"; $s[] = " // @@protoc_insertion_point(namespace_{$ns})"; $s[] = ''; $cmt = $this->compiler->getComment($ns . '.' . $service->getName(), ' * '); if ($cmt) { $s[] = " /**"; $s[] = $cmt; $s[] = " */"; } $s[] = ' interface ' . $service->getName(); $s[] = ' {'; $s[] = ' // @@protoc_insertion_point(scope_interface)'; $s[] = ' // @@protoc_insertion_point(interface_' . $ns . '.' . $service->getName() . ')'; $s[] = ''; foreach ($service->getMethodList() as $method) { $s[] = ' /**'; $cmt = $this->compiler->getComment($ns . '.' . $service->getName() . '.' . $method->getName(), ' * '); if ($cmt) { $s[] = $cmt; $s[] = ' * '; } $s[] = ' * @param ' . $this->normalizeNS($method->getInputType()) . ' $input'; $s[] = ' * @return ' . $this->normalizeNS($method->getOutputType()); $s[] = ' */'; $s[] = ' public function ' . $method->getName() . '(\\' . $this->normalizeNS($method->getInputType()) . ' $input);'; $s[] = ''; } $s[] = ' }'; $s[] = '}'; $s[] = ''; return implode(PHP_EOL, $s) . PHP_EOL; }
/** * @param \google\protobuf\FileDescriptorProto $fileDescriptor * @param \google\protobuf\ServiceDescriptorProto $serviceDescriptor * @param string $parent * * @return \Protobuf\Compiler\Entity */ protected function generateService(FileDescriptorProto $fileDescriptor, ServiceDescriptorProto $serviceDescriptor, $parent = null) { $type = Entity::TYPE_SERVICE; $name = $serviceDescriptor->getName(); $entity = new Entity($type, $name, $serviceDescriptor, $fileDescriptor, $parent); return $entity; }