示例#1
0
 public function getNameSpace()
 {
     $package = $this->file->getPackage();
     if (!$package) {
         $args = explode(".", $package);
         array_pop($args);
         $output = join("\\", $args);
     } else {
         $output = "";
     }
     return $output;
 }
示例#2
0
 private function collectFile(FileDescriptorProto $file, $path)
 {
     $this->file = $file;
     $this->messages = array();
     $this->enums = array();
     $this->paths = array();
     $this->sourceCodeInfo = array();
     foreach ((array) $file->getMessageType() as $i => $nestedMessage) {
         $this->collectMessage($file->getPackage(), $nestedMessage, array_merge($path, array(FileDescriptorProtoMeta::MESSAGE_TYPE_PROTOBUF_FIELD, $i)));
     }
     foreach ((array) $file->getEnumType() as $i => $nestedEnum) {
         $this->collectEnum($file->getPackage(), $nestedEnum, array_merge($path, array(FileDescriptorProtoMeta::ENUM_TYPE_PROTOBUF_FIELD, $i)));
     }
 }
 public function generate(proto\FileDescriptorProto $proto)
 {
     // Keep a reference to the current proto
     $this->proto = $proto;
     // Obtain the root namespace
     $ns = $proto->getPackage();
     // Reset the extensions dictionary
     $this->extensions = array();
     $result = array();
     // Generate Enums
     if (!empty($proto->enum_type)) {
         $result += $this->generateEnums($proto->enum_type, $ns);
     }
     // Generate Messages
     if (!empty($proto->message_type)) {
         $result += $this->generateMessages($proto->message_type, $ns);
     }
     // Collect extensions
     if (!empty($proto->extension_)) {
         foreach ($proto->extension_ as $field) {
             $this->extensions[$field->getExtendee()][] = $field;
         }
     }
     // Generate all extensions found in this proto file
     if (count($this->extensions)) {
         // In multifile mode we output all the extensions in a file named after
         // the proto file, since it's not trivial or even possible in all cases
         // to include the extensions with the extended message file.
         $fname = pathinfo($proto->name, PATHINFO_FILENAME) . '-extensions';
         $src = array();
         foreach ($this->extensions as $extendee => $fields) {
             $src[] = $this->template('extension', $fields, $extendee);
         }
         $result[$fname] = implode("\n", $src);
     }
     // Generate services
     if ($this->option('generic_services') && $proto->hasService()) {
         foreach ($proto->getServiceList() as $service) {
             $src = $this->template('service', $service, $ns);
             $result[$namespace . '.' . $service->getName()] = $src;
         }
     }
     $suffix = $this->option('suffix', '.php');
     $files = array();
     if ($this->option('multifile', false)) {
         foreach ($result as $ns => $content) {
             if (empty($content)) {
                 continue;
             }
             // Generate a filename from the mapped namespace
             $fname = str_replace($this->nsSep, DIRECTORY_SEPARATOR, $this->ns($ns));
             $fname .= $suffix;
             $file = new proto\compiler\CodeGeneratorResponse\File();
             $file->setName($fname);
             $src = $this->template('file', $content, $ns);
             $file->setContent($src);
             $files[] = $file;
         }
     } else {
         $fname = pathinfo($proto->name, PATHINFO_FILENAME) . $suffix;
         $file = new \google\protobuf\compiler\CodeGeneratorResponse\File();
         $file->setName($fname);
         $src = $this->template('file', implode("\n", $result), $ns);
         $file->setContent($src);
         $files[] = $file;
     }
     return $files;
 }
 public function getNamespace(proto\FileDescriptorProto $proto = NULL)
 {
     return NULL === $proto ? $this->proto->getPackage() : $proto->getPackage();
 }
示例#5
0
文件: Helper.php 项目: melanc/devel
 public static function getPackageName(FileDescriptorProto $file)
 {
     $package = getEnv("PACKAGE");
     if ($package) {
         $result = $package;
         //        } else if ($file->getOptions()->getJavaPackage()) {
         //            $result = $file->getOptions()->getJavaPackage();
     } else {
         $result = "";
         if ($file->getPackage()) {
             if (!$result) {
                 $result .= ".";
             }
             $result .= $file->getPackage();
         }
     }
     $result = preg_replace("/^\\.+/", ".", $result);
     return $result;
 }
示例#6
0
 /**
  * @return string
  */
 public function getPackage()
 {
     $parent = $this->parent;
     $package = $this->fileDescriptor->getPackage();
     return $this->fullyQualifiedName($package, $parent);
 }