示例#1
0
 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;
 }
 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 \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;
 }