コード例 #1
0
 public function testGetNamespaceUsingPhpOptions()
 {
     $name = 'SimpleMessage';
     $type = Entity::TYPE_MESSAGE;
     $descriptor = new DescriptorProto();
     $fileDescriptor1 = new FileDescriptorProto();
     $fileDescriptor2 = new FileDescriptorProto();
     $fileDescriptor3 = new FileDescriptorProto();
     $fileOptions2 = new FileOptions();
     $fileOptions3 = new FileOptions();
     $fileOptions2->extensions()->put(Extension::package(), 'Package');
     $fileOptions3->extensions()->put(Extension::package(), 'Package');
     $fileDescriptor1->setPackage('package');
     $fileDescriptor2->setPackage('package');
     $fileDescriptor3->setPackage('package');
     $fileDescriptor2->setOptions($fileOptions2);
     $fileDescriptor3->setOptions($fileOptions3);
     $entity1 = new Entity($type, $name, $descriptor, $fileDescriptor1);
     $entity2 = new Entity($type, $name, $descriptor, $fileDescriptor2);
     $entity3 = new Entity($type, $name, $descriptor, $fileDescriptor3, 'Parent');
     $this->assertEquals('package', $entity1->getNamespace());
     $this->assertEquals('Package', $entity2->getNamespace());
     $this->assertEquals('Package\\Parent', $entity3->getNamespace());
 }
コード例 #2
0
 /**
  * @param string $name
  * @param string $package
  * @param array  $values
  *
  * @return \google\protobuf\FileDescriptorProto
  */
 protected function createFileDescriptorProto($name, $package, array $values = [])
 {
     $descriptor = new FileDescriptorProto();
     $extensions = isset($values['extensions']) ? $values['extensions'] : [];
     $options = isset($values['options']) ? $values['options'] : null;
     $messages = isset($values['messages']) ? $values['messages'] : [];
     $services = isset($values['services']) ? $values['services'] : [];
     $enums = isset($values['enums']) ? $values['enums'] : [];
     $descriptor->setName($name);
     $descriptor->setPackage($package);
     foreach ($extensions as $number => $field) {
         if (is_array($field)) {
             $name = $field[0];
             $type = $field[1];
             $label = $field[2];
             $extendee = $field[3];
             $typeName = isset($field[4]) ? $field[4] : null;
             $field = $this->createFieldDescriptorProto($number, $name, $type, $label, $typeName);
             $field->setExtendee($extendee);
         }
         $descriptor->addExtension($field);
     }
     foreach ($messages as $item) {
         if (is_array($item)) {
             $name = $item['name'];
             $fields = isset($item['fields']) ? $item['fields'] : [];
             $values = isset($item['values']) ? $item['values'] : [];
             $item = $this->createDescriptorProto($name, $fields, $values);
         }
         $descriptor->addMessageType($item);
     }
     foreach ($enums as $item) {
         if (is_array($item)) {
             $name = $item['name'];
             $values = isset($item['values']) ? $item['values'] : [];
             $item = $this->createEnumDescriptorProto($name, $values);
         }
         $descriptor->addEnumType($item);
     }
     foreach ($services as $item) {
         if (is_array($item)) {
             $name = $item['name'];
             $values = isset($item['values']) ? $item['values'] : [];
             $item = $this->createServiceDescriptorProto($name, $values);
         }
         $descriptor->addService($item);
     }
     if ($options !== null) {
         $fileOptions = new FileOptions();
         $optionsExt = isset($options['extensions']) ? $options['extensions'] : [];
         if (isset($options['packed'])) {
             $fileOptions->setPacked($options['packed']);
         }
         foreach ($optionsExt as $ext) {
             $fileOptions->extensions()->put($ext[0], $ext[1]);
         }
         $descriptor->setOptions($fileOptions);
     }
     return $descriptor;
 }