コード例 #1
0
 protected function compileEnum(proto\EnumDescriptorProto $enum, $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 . '.' . $enum->getName(), '   * ');
     if ($cmt) {
         $s[] = "  /**";
         $s[] = $cmt;
         $s[] = "   */";
     }
     $s[] = "  class " . $enum->getName() . ' extends \\DrSlump\\Protobuf\\Enum {';
     foreach ($enum->getValueList() as $value) {
         $s[] = "    const " . $value->getName() . " = " . $value->getNumber() . ";";
     }
     $s[] = "";
     $s[] = "    // @@protoc_insertion_point(scope_class)";
     $s[] = '    // @@protoc_insertion_point(class_' . $ns . '.' . $enum->getName() . ')';
     $s[] = "  }";
     $s[] = "}";
     $s[] = "";
     return implode(PHP_EOL, $s);
 }
コード例 #2
0
ファイル: ProtocGenPhp.php プロジェクト: jakubkulhan/meta
 private function generateEnum($className, EnumDescriptorProto $enum)
 {
     $file = new PhpFile();
     $class = $file->addClass($className);
     $class->setFinal(true);
     if (($info = $this->getSourceCodeInfo($className)) && $info->getLeadingComments()) {
         $class->addComment(trim($info->getLeadingComments()));
     }
     $this->addAutoGeneratedWarning($class);
     foreach ($enum->getValue() as $value) {
         $class->addConst($value->getName(), $value->getNumber());
     }
     return new Result($file, $class);
 }
コード例 #3
0
 /**
  * @param string $name
  * @param array  $values
  *
  * @return \google\protobuf\EnumDescriptorProto
  */
 protected function createEnumDescriptorProto($name, array $values)
 {
     $descriptor = new EnumDescriptorProto();
     $descriptor->setName($name);
     foreach ($values as $number => $value) {
         if (is_string($value)) {
             $value = $this->createEnumValueDescriptorProto($number, $value);
         }
         $descriptor->addValue($value);
     }
     return $descriptor;
 }
コード例 #4
0
ファイル: Compiler.php プロジェクト: melanc/devel
 protected function setupFullNameForEnum(EnumDescriptorProto $enum, $package_name = "")
 {
     $enum->full_name = $package_name . "." . $enum->getName();
     $enum->package_name = $package_name;
     MessagePool::register($enum->full_name, $enum);
 }
コード例 #5
0
ファイル: EnumGenerator.php プロジェクト: melanc/devel
 public function generate(Printer $printer)
 {
     if ($this->descriptor->file()->getOptions()->getExtension("php")->getMultipleFiles()) {
         $printer->put("<?php\n");
     }
     $this->printUseNameSpaceIfNeeded($printer);
     $printer->put("/**\n" . " * Generated by the protocol buffer compiler.  DO NOT EDIT!\n" . " * source: `filename`\n" . " *\n", "filename", $this->descriptor->file()->getName());
     $printer->put(" */\n");
     $printer->put("class `name` extends `base`\n{\n", "name", Helper::getClassName($this->descriptor), "base", Helper::getEnumClassName($this->descriptor));
     $printer->indent();
     $printer->put("// @@protoc_insertion_point(traits:`name`)\n", "name", $this->descriptor->full_name);
     $printer->put("\n");
     foreach ($this->descriptor->getValue() as $value) {
         if ($dict = SourceInfoDictionary::get($this->descriptor->file()->getName(), $this->descriptor->getName(), $value->getName())) {
             if ($dict->getLeadingComments()) {
                 $printer->put("/**\n");
                 $lines = preg_split("/\r?\n/", trim($dict->getLeadingComments()));
                 foreach ($lines as $line) {
                     $line = Helper::cleanupComment($line);
                     $printer->put(" * `comment`\n", "comment", $line);
                 }
                 $printer->put(" */\n");
             }
         }
         $printer->put("const `name` = `number`;\n", "name", $this->getEnumValueAsString($value), "number", $value->getNumber());
     }
     $printer->put("\n");
     $printer->put("// @@protoc_insertion_point(const_scope:`name`)\n", "name", $this->descriptor->full_name);
     $printer->put("\n");
     $printer->put("// @@protoc_insertion_point(class_scope:`name`)\n", "name", $this->descriptor->full_name);
     $printer->put("\n");
     $printer->put("/**\n");
     $printer->put(" * @return \\ProtocolBuffers\\EnumDescriptor\n");
     $printer->put(" */\n");
     $printer->put("public static function getEnumDescriptor()\n");
     $printer->put("{\n");
     $printer->indent();
     $printer->put("static \$descriptor;\n");
     $printer->put("if (!\$descriptor) {\n");
     $printer->indent();
     $printer->put("\$builder = new `builder`();\n", "builder", Helper::getEnumDescriptorBuilderClassName($this->descriptor));
     foreach ($this->descriptor->getValue() as $value) {
         $printer->put("\$builder->addValue(new \\ProtocolBuffers\\EnumValueDescriptor(array(\n");
         $printer->indent();
         $printer->put("\"value\" => `value`,\n", "value", "self::" . $value->getName());
         $printer->put("\"name\"  => '`name`',\n", "name", $value->getName());
         $printer->outdent();
         $printer->put(")));\n");
     }
     $printer->put("// @@protoc_insertion_point(builder_scope:`name`)\n", "name", $this->descriptor->full_name);
     $printer->put("\$descriptor = \$builder->build();\n");
     $printer->outdent();
     $printer->put("}\n");
     $printer->put("return \$descriptor;\n");
     $printer->outdent();
     $printer->put("}\n");
     $printer->outdent();
     $printer->put("}\n");
     if ($this->enclose_namespace_) {
         $printer->outdent();
         $printer->put("}\n\n");
     }
     PragmaticInserter::execute($this->descriptor, $this->context);
 }
コード例 #6
0
 public function compileEnum(proto\EnumDescriptorProto $enum, $namespace)
 {
     $s[] = "{$namespace}.{$enum->name} = {";
     $lines = array();
     foreach ($enum->getValueList() as $value) {
         $lines[] = "  /** @const */ {$value->name}: {$value->number}";
     }
     $s[] = implode(",\n", $lines);
     $s[] = '};';
     $s[] = '';
     return implode("\n", $s);
 }
コード例 #7
0
 /**
  * @param \google\protobuf\FileDescriptorProto $fileDescriptor
  * @param \google\protobuf\EnumDescriptorProto $enumDescriptor
  * @param string                               $parent
  *
  * @return \Protobuf\Compiler\Entity
  */
 protected function generateEnum(FileDescriptorProto $fileDescriptor, EnumDescriptorProto $enumDescriptor, $parent = null)
 {
     $type = Entity::TYPE_ENUM;
     $name = $enumDescriptor->getName();
     $entity = new Entity($type, $name, $enumDescriptor, $fileDescriptor, $parent);
     return $entity;
 }