public function printMagicMethod(Printer $printer)
 {
     $printer->put(" * -*- magic methods -*-\n");
     $printer->put(" *\n");
     foreach ($this->descriptor->getField() as $field) {
         $printer->put(" * @method `type` get`variable`()\n", "type", $this->getTypeName($field), "variable", Helper::cameraize($field->getName()));
         if (FieldDescriptorProto\Label::isRepeated($field)) {
             $printer->put(" * @method void append`variable`(`type2` \$value)\n", "type", $this->getTypeName($field), "variable", Helper::cameraize($field->getName()), "type2", $this->getTypeName2($field, true));
         } else {
             $printer->put(" * @method void set`variable`(`type2` \$value)\n", "type", $this->getTypeName($field), "variable", Helper::cameraize($field->getName()), "type2", $this->getTypeName2($field));
         }
     }
 }
Beispiel #2
0
 private function generateMessage($className, DescriptorProto $message)
 {
     $file = new PhpFile();
     $class = $file->addClass($className);
     $ns = $class->getNamespace();
     $ns->addUse("Skrz\\Meta\\Protobuf\\ProtobufField", null, $protobufFieldAlias);
     if (($info = $this->getSourceCodeInfo($className)) && $info->getLeadingComments()) {
         $class->addComment(trim($info->getLeadingComments()));
     }
     $this->addAutoGeneratedWarning($class);
     foreach ((array) $message->getField() as $i => $field) {
         /** @var FieldDescriptorProto $field */
         $propertyName = lcfirst(implode("", array_map(function ($s) {
             return ucfirst($s);
         }, explode("_", $field->getName()))));
         $property = $class->addProperty($propertyName);
         $property->setVisibility("protected");
         if (($info = $this->getSourceCodeInfo($className, array_merge($this->paths[$className], [DescriptorProtoMeta::FIELD_PROTOBUF_FIELD, $i]))) && $info->getLeadingComments()) {
             $property->addComment(str_replace("*/", "* /", trim($info->getLeadingComments())));
             $property->addComment("");
         }
         switch ($field->getType()) {
             case FieldDescriptorProto\TypeEnum::TYPE_DOUBLE:
                 $wireType = WireTypeEnum::FIXED64;
                 $phpType = "float";
                 break;
             case FieldDescriptorProto\TypeEnum::TYPE_FLOAT:
                 $wireType = WireTypeEnum::FIXED32;
                 $phpType = "float";
                 break;
             case FieldDescriptorProto\TypeEnum::TYPE_UINT64:
             case FieldDescriptorProto\TypeEnum::TYPE_UINT32:
                 $unsigned = true;
             case FieldDescriptorProto\TypeEnum::TYPE_INT64:
             case FieldDescriptorProto\TypeEnum::TYPE_INT32:
                 $wireType = WireTypeEnum::VARINT;
                 $phpType = "int";
                 break;
             case FieldDescriptorProto\TypeEnum::TYPE_FIXED64:
                 $unsigned = true;
             case FieldDescriptorProto\TypeEnum::TYPE_SFIXED64:
                 $wireType = WireTypeEnum::FIXED64;
                 $phpType = "int";
                 break;
             case FieldDescriptorProto\TypeEnum::TYPE_FIXED32:
                 $unsigned = true;
             case FieldDescriptorProto\TypeEnum::TYPE_SFIXED32:
                 $wireType = WireTypeEnum::FIXED32;
                 $phpType = "int";
                 break;
             case FieldDescriptorProto\TypeEnum::TYPE_BOOL:
                 $wireType = WireTypeEnum::VARINT;
                 $phpType = "bool";
                 break;
             case FieldDescriptorProto\TypeEnum::TYPE_STRING:
             case FieldDescriptorProto\TypeEnum::TYPE_BYTES:
                 $wireType = WireTypeEnum::STRING;
                 $phpType = "string";
                 break;
             case FieldDescriptorProto\TypeEnum::TYPE_MESSAGE:
                 $wireType = WireTypeEnum::STRING;
                 $fieldClassName = $this->convertPackageToNamespace($field->getTypeName());
                 $ns->addUse($fieldClassName, null, $phpType);
                 break;
             case FieldDescriptorProto\TypeEnum::TYPE_ENUM:
                 $wireType = WireTypeEnum::VARINT;
                 $fieldClassName = $this->convertPackageToNamespace($field->getTypeName() . "Enum");
                 $ns->addUse($fieldClassName, null, $see);
                 $phpType = "int";
                 break;
             case FieldDescriptorProto\TypeEnum::TYPE_SINT32:
             case FieldDescriptorProto\TypeEnum::TYPE_SINT64:
                 $wireType = WireTypeEnum::ZIGZAG;
                 $phpType = "int";
                 break;
             default:
                 throw new \LogicException("Unhandled type '{$field->getType()}'.");
         }
         if ($field->getLabel() === FieldDescriptorProto\LabelEnum::LABEL_REPEATED) {
             $phpType .= "[]";
         }
         $property->addComment("@var {$phpType}");
         if (isset($see)) {
             $property->addComment("@see {$see}");
             unset($see);
         }
         $property->addComment("")->addComment("@Skrz\\Meta\\Protobuf\\ProtobufField(" . "number={$field->getNumber()}" . ", wireType=\"{$wireType}\"" . ", unsigned=" . var_export(isset($unsigned) ? $unsigned : false, true) . ", packed=" . var_export($field->getOptions() ? $field->getOptions()->getPacked() : false, true) . ")");
         $getter = $class->addMethod("get" . ucfirst($propertyName));
         $getter->addComment("@return {$phpType}");
         $getter->addBody("return \$this->{$propertyName};");
         $setter = $class->addMethod("set" . ucfirst($propertyName));
         $setter->addParameter($propertyName);
         $setter->addComment("@param {$phpType} \${$propertyName}")->addComment("")->addComment("@return self");
         $setter->addBody("\$this->{$propertyName} = \${$propertyName};")->addBody("return \$this;");
     }
     return new Result($file, $class);
 }
 protected function compileMessage(proto\DescriptorProto $msg, $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 . '.' . $msg->getName(), '   * ');
     if ($cmt) {
         $s[] = "  /**";
         $s[] = $cmt;
         $s[] = "   */";
     }
     // Compute a new namespace with the message name as suffix
     $ns .= '.' . $msg->getName();
     $s[] = '  class ' . $msg->getName() . ' extends \\DrSlump\\Protobuf\\Message {';
     $s[] = '';
     foreach ($msg->getField() as $field) {
         $s[] = $this->generatePublicField($field, $ns, "    ");
     }
     $s[] = '';
     $s[] = '    /** @var \\Closure[] */';
     $s[] = '    protected static $__extensions = array();';
     $s[] = '';
     $s[] = '    public static function descriptor()';
     $s[] = '    {';
     $s[] = '      $descriptor = new \\DrSlump\\Protobuf\\Descriptor(__CLASS__, \'' . $ns . '\');';
     $s[] = '';
     foreach ($msg->getField() as $field) {
         $s[] = $this->compileField($field, $ns, "      ");
         $s[] = '      $descriptor->addField($f);';
         $s[] = '';
     }
     $s[] = '      foreach (self::$__extensions as $cb) {';
     $s[] = '        $descriptor->addField($cb(), true);';
     $s[] = '      }';
     $s[] = '';
     $s[] = '      // @@protoc_insertion_point(scope_descriptor)';
     $s[] = '      // @@protoc_insertion_point(descriptor_' . $ns . ')';
     $s[] = '';
     $s[] = '      return $descriptor;';
     $s[] = '    }';
     $s[] = '';
     //$s[]= "    protected static \$__exts = array(";
     //foreach ($msg->getExtensionRange() as $range):
     //$s[]= '      array(' . $range->getStart() . ', ' . ($range->getEnd()-1) . '),';
     //endforeach;
     //$s[]= "    );";
     //$s[]= "";
     foreach ($msg->getField() as $field) {
         $s[] = $this->generateAccessors($field, $ns, "    ");
     }
     $s[] = "";
     $s[] = "    // @@protoc_insertion_point(scope_class)";
     $s[] = '    // @@protoc_insertion_point(class_' . $ns . ')';
     $s[] = "  }";
     $s[] = "}";
     $s[] = "";
     // Generate Enums
     if ($msg->hasEnumType()) {
         foreach ($msg->getEnumType() as $enum) {
             $src = $this->compileEnum($enum, $ns);
             $this->addComponent($ns, $enum->getName(), $src);
         }
     }
     // Generate nested messages
     if ($msg->hasNestedType()) {
         foreach ($msg->getNestedType() as $msg) {
             $src = $this->compileMessage($msg, $ns);
             $this->addComponent($ns, $msg->getName(), $src);
         }
     }
     // Collect extensions
     if ($msg->hasExtension()) {
         foreach ($msg->getExtensionList() as $field) {
             $this->extensions[$field->getExtendee()][] = array($ns, $field);
         }
     }
     return implode(PHP_EOL, $s) . PHP_EOL;
 }