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());
 }
예제 #2
0
 /**
  * @param \Protobuf\Stream $stream
  *
  * @return \Protobuf\Stream
  */
 public function compile(Stream $stream)
 {
     // Parse the request
     $request = CodeGeneratorRequest::fromStream($stream, $this->config);
     $response = new CodeGeneratorResponse();
     $context = $this->createContext($request);
     $entities = $context->getEntities();
     $options = $context->getOptions();
     $generator = new Generator($context);
     // whether or not it will renegerate classes with new extensions
     $regenerate = false;
     $hasExtension = $context->hasProtobufExtension();
     // Run each entity
     foreach ($entities as $key => $entity) {
         $generateImported = $options->getGenerateImported();
         $isFileToGenerate = $entity->isFileToGenerate();
         // Only compile those given to generate, not the imported ones
         if (!$generateImported && !$isFileToGenerate) {
             $this->logger->debug(sprintf('Skipping generation of imported class "%s"', $entity->getClass()));
             continue;
         }
         $this->logger->info(sprintf('Generating class "%s"', $entity->getClass()));
         $generator->visit($entity);
         $file = new File();
         $path = $entity->getPath();
         $content = $entity->getContent();
         $file->setName($path);
         $file->setContent($content);
         $response->addFile($file);
         if ($hasExtension && $this->loadEntityClass($entity)) {
             $regenerate = true;
         }
     }
     if ($regenerate) {
         $this->logger->info('Regenerating classes with new extensions');
         $stream->seek(0);
         // Renegerate classes with new extensions
         return $this->compile($stream);
     }
     $this->logger->info('Generation completed.');
     // Finally serialize the response object
     return $response->toStream($this->config);
 }