/** * @param \google\protobuf\FileDescriptorProto $fileDescriptor * * @return array */ protected function buildFileEntities(FileDescriptorProto $fileDescriptor) { $messages = $fileDescriptor->getMessageTypeList(); $enums = $fileDescriptor->getEnumTypeList(); $services = $fileDescriptor->getServiceList(); $result = []; if ($messages !== null) { $result = array_merge($result, $this->generateMessages($fileDescriptor, $messages)); } if ($services !== null) { $result = array_merge($result, $this->generateServices($fileDescriptor, $services)); } if ($enums !== null) { $result = array_merge($result, $this->generateEnums($fileDescriptor, $enums)); } if ($this->hasExtension($fileDescriptor)) { $result[] = $this->generateExtension($fileDescriptor); } return $result; }
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; }