diff options
-rw-r--r-- | Makefile.am | 9 | ||||
-rw-r--r-- | composer.json | 5 | ||||
-rw-r--r-- | php/composer.json | 5 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/Descriptor.php | 167 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/EnumDescriptor.php | 56 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/EnumValueDescriptor.php | 37 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/FieldDescriptor.php | 236 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/FileDescriptor.php | 90 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/GPBUtil.php | 100 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/InputStream.php | 25 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/MapField.php | 181 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/MapFieldIter.php | 113 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/OneofDescriptor.php | 67 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/RepeatedField.php | 80 | ||||
-rw-r--r-- | php/src/Google/Protobuf/Internal/RepeatedFieldIter.php | 118 | ||||
-rw-r--r-- | php/src/Google/Protobuf/descriptor.php | 600 |
16 files changed, 1048 insertions, 841 deletions
diff --git a/Makefile.am b/Makefile.am index 385531ed..47fe4bb9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -597,36 +597,44 @@ php_EXTRA_DIST= \ php/ext/google/protobuf/upb.c \ php/ext/google/protobuf/protobuf.c \ php/src/phpdoc.dist.xml \ + php/src/Google/Protobuf/Internal/Descriptor.php \ php/src/Google/Protobuf/Internal/DescriptorPool.php \ php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php \ php/src/Google/Protobuf/Internal/OneofField.php \ php/src/Google/Protobuf/Internal/MessageOptions.php \ + php/src/Google/Protobuf/Internal/FileDescriptor.php \ php/src/Google/Protobuf/Internal/FileDescriptorProto.php \ php/src/Google/Protobuf/Internal/MapEntry.php \ + php/src/Google/Protobuf/Internal/FieldDescriptor.php \ php/src/Google/Protobuf/Internal/FieldDescriptorProto.php \ php/src/Google/Protobuf/Internal/InputStream.php \ php/src/Google/Protobuf/Internal/UninterpretedOption.php \ php/src/Google/Protobuf/Internal/ServiceOptions.php \ php/src/Google/Protobuf/Internal/MethodOptions_IdempotencyLevel.php \ php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php \ + php/src/Google/Protobuf/Internal/OneofDescriptor.php \ php/src/Google/Protobuf/Internal/OneofDescriptorProto.php \ php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php \ php/src/Google/Protobuf/Internal/OutputStream.php \ php/src/Google/Protobuf/Internal/MessageBuilderContext.php \ + php/src/Google/Protobuf/Internal/EnumValueDescriptor.php \ php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php \ php/src/Google/Protobuf/Internal/FileOptions_OptimizeMode.php \ php/src/Google/Protobuf/Internal/DescriptorProto.php \ php/src/Google/Protobuf/Internal/MapField.php \ + php/src/Google/Protobuf/Internal/MapFieldIter.php \ php/src/Google/Protobuf/Internal/MethodDescriptorProto.php \ php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php \ php/src/Google/Protobuf/Internal/DescriptorProto_ReservedRange.php \ php/src/Google/Protobuf/Internal/RepeatedField.php \ + php/src/Google/Protobuf/Internal/RepeatedFieldIter.php \ php/src/Google/Protobuf/Internal/EnumValueOptions.php \ php/src/Google/Protobuf/Internal/MethodOptions.php \ php/src/Google/Protobuf/Internal/OneofOptions.php \ php/src/Google/Protobuf/Internal/Message.php \ php/src/Google/Protobuf/Internal/FileOptions.php \ php/src/Google/Protobuf/Internal/FileDescriptorSet.php \ + php/src/Google/Protobuf/Internal/EnumDescriptor.php \ php/src/Google/Protobuf/Internal/EnumDescriptorProto.php \ php/src/Google/Protobuf/Internal/GPBWire.php \ php/src/Google/Protobuf/Internal/FieldDescriptorProto_Label.php \ @@ -643,7 +651,6 @@ php_EXTRA_DIST= \ php/src/Google/Protobuf/Internal/GPBUtil.php \ php/src/Google/Protobuf/Internal/FieldOptions_CType.php \ php/src/Google/Protobuf/Internal/GPBDecodeException.php \ - php/src/Google/Protobuf/descriptor.php \ php/src/GPBMetadata/Google/Protobuf/Internal/Descriptor.php \ php/tests/array_test.php \ php/tests/autoload.php \ diff --git a/composer.json b/composer.json index 2b04e079..5b6c7ee2 100644 --- a/composer.json +++ b/composer.json @@ -15,9 +15,6 @@ "psr-4": { "Google\\Protobuf\\Internal\\": "php/src/Google/Protobuf/Internal", "GPBMetadata\\Google\\Protobuf\\Internal\\": "php/src/GPBMetadata/Google/Protobuf/Internal" - }, - "files": [ - "php/src/Google/Protobuf/descriptor.php" - ] + } } } diff --git a/php/composer.json b/php/composer.json index 32b0f44c..724a45dd 100644 --- a/php/composer.json +++ b/php/composer.json @@ -20,9 +20,6 @@ "GPBMetadata\\": "tests/generated/GPBMetadata", "GPBMetadata\\Google\\Protobuf\\Internal\\": "src/GPBMetadata/Google/Protobuf/Internal", "": "tests/generated" - }, - "files": [ - "src/Google/Protobuf/descriptor.php" - ] + } } } diff --git a/php/src/Google/Protobuf/Internal/Descriptor.php b/php/src/Google/Protobuf/Internal/Descriptor.php new file mode 100644 index 00000000..d0d1e259 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/Descriptor.php @@ -0,0 +1,167 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +class Descriptor +{ + + private $full_name; + private $field = []; + private $nested_type = []; + private $enum_type = []; + private $klass; + private $options; + private $oneof_decl = []; + + public function addOneofDecl($oneof) + { + $this->oneof_decl[] = $oneof; + } + + public function getOneofDecl() + { + return $this->oneof_decl; + } + + public function setFullName($full_name) + { + $this->full_name = $full_name; + } + + public function getFullName() + { + return $this->full_name; + } + + public function addField($field) + { + $this->field[$field->getNumber()] = $field; + } + + public function getField() + { + return $this->field; + } + + public function addNestedType($desc) + { + $this->nested_type[] = $desc; + } + + public function getNestedType() + { + return $this->nested_type; + } + + public function addEnumType($desc) + { + $this->enum_type[] = $desc; + } + + public function getEnumType() + { + return $this->enum_type; + } + + public function getFieldByNumber($number) + { + if (!isset($this->field[$number])) { + return NULL; + } else { + return $this->field[$number]; + } + } + + public function setClass($klass) + { + $this->klass = $klass; + } + + public function getClass() + { + return $this->klass; + } + + public function setOptions($options) + { + $this->options = $options; + } + + public function getOptions() + { + return $this->options; + } + + public static function buildFromProto($proto, $file_proto, $containing) + { + $desc = new Descriptor(); + + $message_name_without_package = ""; + $classname = ""; + $fullname = ""; + getFullClassName( + $proto, + $containing, + $file_proto, + $message_name_without_package, + $classname, + $fullname); + $desc->setFullName($fullname); + $desc->setClass($classname); + $desc->setOptions($proto->getOptions()); + + foreach ($proto->getField() as $field_proto) { + $desc->addField(FieldDescriptor::buildFromProto($field_proto)); + } + + // Handle nested types. + foreach ($proto->getNestedType() as $nested_proto) { + $desc->addNestedType(Descriptor::buildFromProto( + $nested_proto, $file_proto, $message_name_without_package)); + } + + // Handle nested enum. + foreach ($proto->getEnumType() as $enum_proto) { + $desc->addEnumType(EnumDescriptor::buildFromProto( + $enum_proto, $file_proto, $message_name_without_package)); + } + + // Handle oneof fields. + foreach ($proto->getOneofDecl() as $oneof_proto) { + $desc->addOneofDecl( + OneofDescriptor::buildFromProto($oneof_proto, $desc)); + } + + return $desc; + } +} diff --git a/php/src/Google/Protobuf/Internal/EnumDescriptor.php b/php/src/Google/Protobuf/Internal/EnumDescriptor.php new file mode 100644 index 00000000..7360a477 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/EnumDescriptor.php @@ -0,0 +1,56 @@ +<?php + +namespace Google\Protobuf\Internal; + +class EnumDescriptor +{ + + private $klass; + private $full_name; + private $value; + + public function setFullName($full_name) + { + $this->full_name = $full_name; + } + + public function getFullName() + { + return $this->full_name; + } + + public function addValue($number, $value) + { + $this->value[$number] = $value; + } + + public function setClass($klass) + { + $this->klass = $klass; + } + + public function getClass() + { + return $this->klass; + } + + public static function buildFromProto($proto, $file_proto, $containing) + { + $desc = new EnumDescriptor(); + + $enum_name_without_package = ""; + $classname = ""; + $fullname = ""; + GPBUtil::getFullClassName( + $proto, + $containing, + $file_proto, + $enum_name_without_package, + $classname, + $fullname); + $desc->setFullName($fullname); + $desc->setClass($classname); + + return $desc; + } +} diff --git a/php/src/Google/Protobuf/Internal/EnumValueDescriptor.php b/php/src/Google/Protobuf/Internal/EnumValueDescriptor.php new file mode 100644 index 00000000..e65a4e8d --- /dev/null +++ b/php/src/Google/Protobuf/Internal/EnumValueDescriptor.php @@ -0,0 +1,37 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +class EnumValueDescriptor +{ +} diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptor.php b/php/src/Google/Protobuf/Internal/FieldDescriptor.php new file mode 100644 index 00000000..6c91950f --- /dev/null +++ b/php/src/Google/Protobuf/Internal/FieldDescriptor.php @@ -0,0 +1,236 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +class FieldDescriptor +{ + + private $name; + private $setter; + private $getter; + private $number; + private $label; + private $type; + private $message_type; + private $enum_type; + private $packed; + private $is_map; + private $oneof_index = -1; + + public function setOneofIndex($index) + { + $this->oneof_index = $index; + } + + public function getOneofIndex() + { + return $this->oneof_index; + } + + public function setName($name) + { + $this->name = $name; + } + + public function getName() + { + return $this->name; + } + + public function setSetter($setter) + { + $this->setter = $setter; + } + + public function getSetter() + { + return $this->setter; + } + + public function setGetter($getter) + { + $this->getter = $getter; + } + + public function getGetter() + { + return $this->getter; + } + + public function setNumber($number) + { + $this->number = $number; + } + + public function getNumber() + { + return $this->number; + } + + public function setLabel($label) + { + $this->label = $label; + } + + public function getLabel() + { + return $this->label; + } + + public function isRepeated() + { + return $this->label === GPBLabel::REPEATED; + } + + public function setType($type) + { + $this->type = $type; + } + + public function getType() + { + return $this->type; + } + + public function setMessageType($message_type) + { + $this->message_type = $message_type; + } + + public function getMessageType() + { + return $this->message_type; + } + + public function setEnumType($enum_type) + { + $this->enum_type = $enum_type; + } + + public function getEnumType() + { + return $this->enum_type; + } + + public function setPacked($packed) + { + $this->packed = $packed; + } + + public function getPacked() + { + return $this->packed; + } + + public function isPackable() + { + return $this->isRepeated() && self::isTypePackable($this->type); + } + + public function isMap() + { + return $this->getType() == GPBType::MESSAGE && + !is_null($this->getMessageType()->getOptions()) && + $this->getMessageType()->getOptions()->getMapEntry(); + } + + private static function isTypePackable($field_type) + { + return ($field_type !== GPBType::STRING && + $field_type !== GPBType::GROUP && + $field_type !== GPBType::MESSAGE && + $field_type !== GPBType::BYTES); + } + + public static function getFieldDescriptor( + $name, + $label, + $type, + $number, + $oneof_index, + $packed, + $type_name = null) + { + $field = new FieldDescriptor(); + $field->setName($name); + $camel_name = implode('', array_map('ucwords', explode('_', $name))); + $field->setGetter('get' . $camel_name); + $field->setSetter('set' . $camel_name); + $field->setType($type); + $field->setNumber($number); + $field->setLabel($label); + $field->setPacked($packed); + $field->setOneofIndex($oneof_index); + + // At this time, the message/enum type may have not been added to pool. + // So we use the type name as place holder and will replace it with the + // actual descriptor in cross building. + switch ($type) { + case GPBType::MESSAGE: + $field->setMessageType($type_name); + break; + case GPBType::ENUM: + $field->setEnumType($type_name); + break; + default: + break; + } + + return $field; + } + + public static function buildFromProto($proto) + { + $type_name = null; + switch ($proto->getType()) { + case GPBType::MESSAGE: + case GPBType::GROUP: + case GPBType::ENUM: + $type_name = $proto->getTypeName(); + break; + default: + break; + } + + $oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1; + $packed = false; + $options = $proto->getOptions(); + if ($options !== null) { + $packed = $options->getPacked(); + } + + return FieldDescriptor::getFieldDescriptor( + $proto->getName(), $proto->getLabel(), $proto->getType(), + $proto->getNumber(), $oneof_index, $packed, $type_name); + } +} diff --git a/php/src/Google/Protobuf/Internal/FileDescriptor.php b/php/src/Google/Protobuf/Internal/FileDescriptor.php new file mode 100644 index 00000000..14716390 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/FileDescriptor.php @@ -0,0 +1,90 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +class FileDescriptor +{ + + private $package; + private $message_type = []; + private $enum_type = []; + + public function setPackage($package) + { + $this->package = $package; + } + + public function getPackage() + { + return $this->package; + } + + public function getMessageType() + { + return $this->message_type; + } + + public function addMessageType($desc) + { + $this->message_type[] = $desc; + } + + public function getEnumType() + { + return $this->enum_type; + } + + public function addEnumType($desc) + { + $this->enum_type[]= $desc; + } + + public static function buildFromProto($proto) + { + $file = new FileDescriptor(); + $file->setPackage($proto->getPackage()); + foreach ($proto->getMessageType() as $message_proto) { + $file->addMessageType(Descriptor::buildFromProto( + $message_proto, $proto, "")); + } + foreach ($proto->getEnumType() as $enum_proto) { + $file->getEnumType()[] = + $file->addEnumType( + EnumDescriptor::buildFromProto( + $enum_proto, + $proto, + "")); + } + return $file; + } +} diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php index 0e66ae6f..1b71f7b7 100644 --- a/php/src/Google/Protobuf/Internal/GPBUtil.php +++ b/php/src/Google/Protobuf/Internal/GPBUtil.php @@ -67,7 +67,6 @@ class GPBUtil } } - public static function checkString(&$var, $check_utf8) { if (is_array($var) || is_object($var)) { @@ -242,4 +241,103 @@ class GPBUtil { return new Uint64($value); } + + public static function getClassNamePrefix( + $classname, + $file_proto) + { + $option = $file_proto->getOptions(); + $prefix = is_null($option) ? "" : $option->getPhpClassPrefix(); + if ($prefix !== "") { + return $prefix; + } + + $reserved_words = array("Empty"); + foreach ($reserved_words as $reserved_word) { + if ($classname === $reserved_word) { + if ($file_proto->getPackage() === "google.protobuf") { + return "GPB"; + } else { + return "PB"; + } + } + } + + return ""; + } + + public static function getClassNameWithoutPackage( + $name, + $file_proto) + { + $classname = implode('_', array_map('ucwords', explode('.', $name))); + return static::getClassNamePrefix($classname, $file_proto) . $classname; + } + + public static function getFullClassName( + $proto, + $containing, + $file_proto, + &$message_name_without_package, + &$classname, + &$fullname) + { + // Full name needs to start with '.'. + $message_name_without_package = $proto->getName(); + if ($containing !== "") { + $message_name_without_package = + $containing . "." . $message_name_without_package; + } + + $package = $file_proto->getPackage(); + if ($package === "") { + $fullname = "." . $message_name_without_package; + } else { + $fullname = "." . $package . "." . $message_name_without_package; + } + + $class_name_without_package = + static::getClassNameWithoutPackage($message_name_without_package, $file_proto); + + $option = $file_proto->getOptions(); + if (!is_null($option) && $option->hasPhpNamespace()) { + $namespace = $option->getPhpNamespace(); + if ($namespace !== "") { + $classname = $namespace . "\\" . $class_name_without_package; + return; + } else { + $classname = $class_name_without_package; + return; + } + } + + if ($package === "") { + $classname = $class_name_without_package; + } else { + $classname = + implode('\\', array_map('ucwords', explode('.', $package))). + "\\".$class_name_without_package; + } + } + + public static function combineInt32ToInt64($high, $low) + { + $isNeg = $high < 0; + if ($isNeg) { + $high = ~$high; + $low = ~$low; + $low++; + if (!$low) { + $high++; + } + } + $result = bcadd(bcmul($high, 4294967296), $low); + if ($low < 0) { + $result = bcadd($result, 4294967296); + } + if ($isNeg) { + $result = bcsub(0, $result); + } + return $result; + } } diff --git a/php/src/Google/Protobuf/Internal/InputStream.php b/php/src/Google/Protobuf/Internal/InputStream.php index 8012a225..f84e1aee 100644 --- a/php/src/Google/Protobuf/Internal/InputStream.php +++ b/php/src/Google/Protobuf/Internal/InputStream.php @@ -34,27 +34,6 @@ namespace Google\Protobuf\Internal; use Google\Protobuf\Internal\Uint64; -function combineInt32ToInt64($high, $low) -{ - $isNeg = $high < 0; - if ($isNeg) { - $high = ~$high; - $low = ~$low; - $low++; - if (!$low) { - $high++; - } - } - $result = bcadd(bcmul($high, 4294967296), $low); - if ($low < 0) { - $result = bcadd($result, 4294967296); - } - if ($isNeg) { - $result = bcsub(0, $result); - } - return $result; -} - class InputStream { @@ -192,7 +171,7 @@ class InputStream $count += 1; } while ($b & 0x80); - $var = combineInt32ToInt64($high, $low); + $var = GPBUtil::combineInt32ToInt64($high, $low); } else { $result = 0; $shift = 0; @@ -265,7 +244,7 @@ class InputStream } $high = unpack('V', $data)[1]; if (PHP_INT_SIZE == 4) { - $var = combineInt32ToInt64($high, $low); + $var = GPBUtil::combineInt32ToInt64($high, $low); } else { $var = ($high << 32) | $low; } diff --git a/php/src/Google/Protobuf/Internal/MapField.php b/php/src/Google/Protobuf/Internal/MapField.php index 55cc12ce..12f09d61 100644 --- a/php/src/Google/Protobuf/Internal/MapField.php +++ b/php/src/Google/Protobuf/Internal/MapField.php @@ -38,131 +38,6 @@ namespace Google\Protobuf\Internal; /** - * MapFieldIter is used to iterate MapField. It is also need for the foreach - * syntax. - */ -class MapFieldIter implements \Iterator -{ - - /** - * @ignore - */ - private $container; - - /** - * Create iterator instance for MapField. - * - * @param MapField The MapField instance for which this iterator is - * created. - * @ignore - */ - public function __construct($container) - { - $this->container = $container; - } - - /** - * Reset the status of the iterator - * - * @return void - */ - public function rewind() - { - return reset($this->container); - } - - /** - * Return the element at the current position. - * - * @return object The element at the current position. - */ - public function current() - { - return current($this->container); - } - - /** - * Return the current key. - * - * @return object The current key. - */ - public function key() - { - return key($this->container); - } - - /** - * Move to the next position. - * - * @return void - */ - public function next() - { - return next($this->container); - } - - /** - * Check whether there are more elements to iterate. - * - * @return bool True if there are more elements to iterate. - */ - public function valid() - { - return key($this->container) !== null; - } -} - -/** - * @ignore - */ -function checkKey($key_type, &$key) -{ - switch ($key_type) { - case GPBType::INT32: - GPBUtil::checkInt32($key); - break; - case GPBType::UINT32: - GPBUtil::checkUint32($key); - break; - case GPBType::INT64: - GPBUtil::checkInt64($key); - break; - case GPBType::UINT64: - GPBUtil::checkUint64($key); - break; - case GPBType::FIXED64: - GPBUtil::checkUint64($key); - break; - case GPBType::FIXED32: - GPBUtil::checkUint32($key); - break; - case GPBType::SFIXED64: - GPBUtil::checkInt64($key); - break; - case GPBType::SFIXED32: - GPBUtil::checkInt32($key); - break; - case GPBType::SINT64: - GPBUtil::checkInt64($key); - break; - case GPBType::SINT32: - GPBUtil::checkInt32($key); - break; - case GPBType::BOOL: - GPBUtil::checkBool($key); - break; - case GPBType::STRING: - GPBUtil::checkString($key, true); - break; - default: - trigger_error( - "Given type cannot be map key.", - E_USER_ERROR); - break; - } -} - -/** * MapField is used by generated protocol message classes to manipulate map * fields. It can be used like native PHP array. */ @@ -255,7 +130,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable */ public function offsetSet($key, $value) { - checkKey($this->key_type, $key); + $this->checkKey($this->key_type, $key); switch ($this->value_type) { case GPBType::INT32: @@ -306,7 +181,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable */ public function offsetUnset($key) { - checkKey($this->key_type, $key); + $this->checkKey($this->key_type, $key); unset($this->container[$key]); } @@ -321,7 +196,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable */ public function offsetExists($key) { - checkKey($this->key_type, $key); + $this->checkKey($this->key_type, $key); return isset($this->container[$key]); } @@ -344,4 +219,54 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable { return count($this->container); } + + /** + * @ignore + */ + private function checkKey($key_type, &$key) + { + switch ($key_type) { + case GPBType::INT32: + GPBUtil::checkInt32($key); + break; + case GPBType::UINT32: + GPBUtil::checkUint32($key); + break; + case GPBType::INT64: + GPBUtil::checkInt64($key); + break; + case GPBType::UINT64: + GPBUtil::checkUint64($key); + break; + case GPBType::FIXED64: + GPBUtil::checkUint64($key); + break; + case GPBType::FIXED32: + GPBUtil::checkUint32($key); + break; + case GPBType::SFIXED64: + GPBUtil::checkInt64($key); + break; + case GPBType::SFIXED32: + GPBUtil::checkInt32($key); + break; + case GPBType::SINT64: + GPBUtil::checkInt64($key); + break; + case GPBType::SINT32: + GPBUtil::checkInt32($key); + break; + case GPBType::BOOL: + GPBUtil::checkBool($key); + break; + case GPBType::STRING: + GPBUtil::checkString($key, true); + break; + default: + trigger_error( + "Given type cannot be map key.", + E_USER_ERROR); + break; + } + } } diff --git a/php/src/Google/Protobuf/Internal/MapFieldIter.php b/php/src/Google/Protobuf/Internal/MapFieldIter.php new file mode 100644 index 00000000..a0388d92 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/MapFieldIter.php @@ -0,0 +1,113 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +/** + * MapField and MapFieldIter are used by generated protocol message classes to + * manipulate map fields. + */ + +namespace Google\Protobuf\Internal; + +/** + * MapFieldIter is used to iterate MapField. It is also need for the foreach + * syntax. + */ +class MapFieldIter implements \Iterator +{ + + /** + * @ignore + */ + private $container; + + /** + * Create iterator instance for MapField. + * + * @param MapField The MapField instance for which this iterator is + * created. + * @ignore + */ + public function __construct($container) + { + $this->container = $container; + } + + /** + * Reset the status of the iterator + * + * @return void + */ + public function rewind() + { + return reset($this->container); + } + + /** + * Return the element at the current position. + * + * @return object The element at the current position. + */ + public function current() + { + return current($this->container); + } + + /** + * Return the current key. + * + * @return object The current key. + */ + public function key() + { + return key($this->container); + } + + /** + * Move to the next position. + * + * @return void + */ + public function next() + { + return next($this->container); + } + + /** + * Check whether there are more elements to iterate. + * + * @return bool True if there are more elements to iterate. + */ + public function valid() + { + return key($this->container) !== null; + } +}
\ No newline at end of file diff --git a/php/src/Google/Protobuf/Internal/OneofDescriptor.php b/php/src/Google/Protobuf/Internal/OneofDescriptor.php new file mode 100644 index 00000000..04988737 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/OneofDescriptor.php @@ -0,0 +1,67 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +class OneofDescriptor +{ + + private $name; + private $fields; + + public function setName($name) + { + $this->name = $name; + } + + public function getName() + { + return $this->name; + } + + public function addField(&$field) + { + $this->fields[] = $field; + } + + public function getFields() + { + return $this->fields; + } + + public static function buildFromProto($oneof_proto) + { + $oneof = new OneofDescriptor(); + $oneof->setName($oneof_proto->getName()); + return $oneof; + } +} diff --git a/php/src/Google/Protobuf/Internal/RepeatedField.php b/php/src/Google/Protobuf/Internal/RepeatedField.php index 2ad4709a..797b3b3a 100644 --- a/php/src/Google/Protobuf/Internal/RepeatedField.php +++ b/php/src/Google/Protobuf/Internal/RepeatedField.php @@ -41,86 +41,6 @@ use Google\Protobuf\Internal\GPBType; use Google\Protobuf\Internal\GPBUtil; /** - * RepeatedFieldIter is used to iterate RepeatedField. It is also need for the - * foreach syntax. - */ -class RepeatedFieldIter implements \Iterator -{ - - /** - * @ignore - */ - private $position; - /** - * @ignore - */ - private $container; - - /** - * Create iterator instance for RepeatedField. - * - * @param RepeatedField The RepeatedField instance for which this iterator - * is created. - * @ignore - */ - public function __construct($container) - { - $this->position = 0; - $this->container = $container; - } - - /** - * Reset the status of the iterator - * - * @return void - */ - public function rewind() - { - $this->position = 0; - } - - /** - * Return the element at the current position. - * - * @return object The element at the current position. - */ - public function current() - { - return $this->container[$this->position]; - } - - /** - * Return the current position. - * - * @return integer The current position. - */ - public function key() - { - return $this->position; - } - - /** - * Move to the next position. - * - * @return void - */ - public function next() - { - ++$this->position; - } - - /** - * Check whether there are more elements to iterate. - * - * @return bool True if there are more elements to iterate. - */ - public function valid() - { - return isset($this->container[$this->position]); - } -} - -/** * RepeatedField is used by generated protocol message classes to manipulate * repeated fields. It can be used like native PHP array. */ diff --git a/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php b/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php new file mode 100644 index 00000000..2b6f8230 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php @@ -0,0 +1,118 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +/** + * RepeatedField and RepeatedFieldIter are used by generated protocol message + * classes to manipulate repeated fields. + */ + +namespace Google\Protobuf\Internal; + +/** + * RepeatedFieldIter is used to iterate RepeatedField. It is also need for the + * foreach syntax. + */ +class RepeatedFieldIter implements \Iterator +{ + + /** + * @ignore + */ + private $position; + /** + * @ignore + */ + private $container; + + /** + * Create iterator instance for RepeatedField. + * + * @param RepeatedField The RepeatedField instance for which this iterator + * is created. + * @ignore + */ + public function __construct($container) + { + $this->position = 0; + $this->container = $container; + } + + /** + * Reset the status of the iterator + * + * @return void + */ + public function rewind() + { + $this->position = 0; + } + + /** + * Return the element at the current position. + * + * @return object The element at the current position. + */ + public function current() + { + return $this->container[$this->position]; + } + + /** + * Return the current position. + * + * @return integer The current position. + */ + public function key() + { + return $this->position; + } + + /** + * Move to the next position. + * + * @return void + */ + public function next() + { + ++$this->position; + } + + /** + * Check whether there are more elements to iterate. + * + * @return bool True if there are more elements to iterate. + */ + public function valid() + { + return isset($this->container[$this->position]); + } +} diff --git a/php/src/Google/Protobuf/descriptor.php b/php/src/Google/Protobuf/descriptor.php deleted file mode 100644 index b7a3a550..00000000 --- a/php/src/Google/Protobuf/descriptor.php +++ /dev/null @@ -1,600 +0,0 @@ -<?php - -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -namespace Google\Protobuf\Internal; - -use Google\Protobuf\Internal\GPBType; -use Google\Protobuf\Internal\MessageOptions; - -class FileDescriptor -{ - - private $package; - private $message_type = []; - private $enum_type = []; - - public function setPackage($package) - { - $this->package = $package; - } - - public function getPackage() - { - return $this->package; - } - - public function getMessageType() - { - return $this->message_type; - } - - public function addMessageType($desc) - { - $this->message_type[] = $desc; - } - - public function getEnumType() - { - return $this->enum_type; - } - - public function addEnumType($desc) - { - $this->enum_type[]= $desc; - } - - public static function buildFromProto($proto) - { - $file = new FileDescriptor(); - $file->setPackage($proto->getPackage()); - foreach ($proto->getMessageType() as $message_proto) { - $file->addMessageType(Descriptor::buildFromProto( - $message_proto, $proto, "")); - } - foreach ($proto->getEnumType() as $enum_proto) { - $file->getEnumType()[] = - $file->addEnumType( - EnumDescriptor::buildFromProto( - $enum_proto, - $proto, - "")); - } - return $file; - } -} - -class Descriptor -{ - - private $full_name; - private $field = []; - private $nested_type = []; - private $enum_type = []; - private $klass; - private $options; - private $oneof_decl = []; - - public function addOneofDecl($oneof) - { - $this->oneof_decl[] = $oneof; - } - - public function getOneofDecl() - { - return $this->oneof_decl; - } - - public function setFullName($full_name) - { - $this->full_name = $full_name; - } - - public function getFullName() - { - return $this->full_name; - } - - public function addField($field) - { - $this->field[$field->getNumber()] = $field; - } - - public function getField() - { - return $this->field; - } - - public function addNestedType($desc) - { - $this->nested_type[] = $desc; - } - - public function getNestedType() - { - return $this->nested_type; - } - - public function addEnumType($desc) - { - $this->enum_type[] = $desc; - } - - public function getEnumType() - { - return $this->enum_type; - } - - public function getFieldByNumber($number) - { - if (!isset($this->field[$number])) { - return NULL; - } else { - return $this->field[$number]; - } - } - - public function setClass($klass) - { - $this->klass = $klass; - } - - public function getClass() - { - return $this->klass; - } - - public function setOptions($options) - { - $this->options = $options; - } - - public function getOptions() - { - return $this->options; - } - - public static function buildFromProto($proto, $file_proto, $containing) - { - $desc = new Descriptor(); - - $message_name_without_package = ""; - $classname = ""; - $fullname = ""; - getFullClassName( - $proto, - $containing, - $file_proto, - $message_name_without_package, - $classname, - $fullname); - $desc->setFullName($fullname); - $desc->setClass($classname); - $desc->setOptions($proto->getOptions()); - - foreach ($proto->getField() as $field_proto) { - $desc->addField(FieldDescriptor::buildFromProto($field_proto)); - } - - // Handle nested types. - foreach ($proto->getNestedType() as $nested_proto) { - $desc->addNestedType(Descriptor::buildFromProto( - $nested_proto, $file_proto, $message_name_without_package)); - } - - // Handle nested enum. - foreach ($proto->getEnumType() as $enum_proto) { - $desc->addEnumType(EnumDescriptor::buildFromProto( - $enum_proto, $file_proto, $message_name_without_package)); - } - - // Handle oneof fields. - foreach ($proto->getOneofDecl() as $oneof_proto) { - $desc->addOneofDecl( - OneofDescriptor::buildFromProto($oneof_proto, $desc)); - } - - return $desc; - } -} - -function getClassNamePrefix( - $classname, - $file_proto) -{ - $option = $file_proto->getOptions(); - $prefix = is_null($option) ? "" : $option->getPhpClassPrefix(); - if ($prefix !== "") { - return $prefix; - } - - $reserved_words = array("Empty", "ECHO", "ARRAY"); - foreach ($reserved_words as $reserved_word) { - if ($classname === $reserved_word) { - if ($file_proto->getPackage() === "google.protobuf") { - return "GPB"; - } else { - return "PB"; - } - } - } - - return ""; -} - -function getClassNameWithoutPackage( - $name, - $file_proto) -{ - $classname = implode('_', array_map('ucwords', explode('.', $name))); - return getClassNamePrefix($classname, $file_proto) . $classname; -} - -function getFullClassName( - $proto, - $containing, - $file_proto, - &$message_name_without_package, - &$classname, - &$fullname) -{ - // Full name needs to start with '.'. - $message_name_without_package = $proto->getName(); - if ($containing !== "") { - $message_name_without_package = - $containing . "." . $message_name_without_package; - } - - $package = $file_proto->getPackage(); - if ($package === "") { - $fullname = "." . $message_name_without_package; - } else { - $fullname = "." . $package . "." . $message_name_without_package; - } - - $class_name_without_package = - getClassNameWithoutPackage($message_name_without_package, $file_proto); - - $option = $file_proto->getOptions(); - if (!is_null($option) && $option->hasPhpNamespace()) { - $namespace = $option->getPhpNamespace(); - if ($namespace !== "") { - $classname = $namespace . "\\" . $class_name_without_package; - return; - } else { - $classname = $class_name_without_package; - return; - } - } - - if ($package === "") { - $classname = $class_name_without_package; - } else { - $classname = - implode('\\', array_map('ucwords', explode('.', $package))). - "\\".$class_name_without_package; - } -} - -class OneofDescriptor -{ - - private $name; - private $fields; - - public function setName($name) - { - $this->name = $name; - } - - public function getName() - { - return $this->name; - } - - public function addField(&$field) - { - $this->fields[] = $field; - } - - public function getFields() - { - return $this->fields; - } - - public static function buildFromProto($oneof_proto) - { - $oneof = new OneofDescriptor(); - $oneof->setName($oneof_proto->getName()); - return $oneof; - } -} - - -class EnumDescriptor -{ - - private $klass; - private $full_name; - private $value; - - public function setFullName($full_name) - { - $this->full_name = $full_name; - } - - public function getFullName() - { - return $this->full_name; - } - - public function addValue($number, $value) - { - $this->value[$number] = $value; - } - - public function setClass($klass) - { - $this->klass = $klass; - } - - public function getClass() - { - return $this->klass; - } - - public static function buildFromProto($proto, $file_proto, $containing) - { - $desc = new EnumDescriptor(); - - $enum_name_without_package = ""; - $classname = ""; - $fullname = ""; - getFullClassName( - $proto, - $containing, - $file_proto, - $enum_name_without_package, - $classname, - $fullname); - $desc->setFullName($fullname); - $desc->setClass($classname); - - return $desc; - } -} - -class EnumValueDescriptor -{ -} - -class FieldDescriptor -{ - - private $name; - private $setter; - private $getter; - private $number; - private $label; - private $type; - private $message_type; - private $enum_type; - private $packed; - private $is_map; - private $oneof_index = -1; - - public function setOneofIndex($index) - { - $this->oneof_index = $index; - } - - public function getOneofIndex() - { - return $this->oneof_index; - } - - public function setName($name) - { - $this->name = $name; - } - - public function getName() - { - return $this->name; - } - - public function setSetter($setter) - { - $this->setter = $setter; - } - - public function getSetter() - { - return $this->setter; - } - - public function setGetter($getter) - { - $this->getter = $getter; - } - - public function getGetter() - { - return $this->getter; - } - - public function setNumber($number) - { - $this->number = $number; - } - - public function getNumber() - { - return $this->number; - } - - public function setLabel($label) - { - $this->label = $label; - } - - public function getLabel() - { - return $this->label; - } - - public function isRepeated() - { - return $this->label === GPBLabel::REPEATED; - } - - public function setType($type) - { - $this->type = $type; - } - - public function getType() - { - return $this->type; - } - - public function setMessageType($message_type) - { - $this->message_type = $message_type; - } - - public function getMessageType() - { - return $this->message_type; - } - - public function setEnumType($enum_type) - { - $this->enum_type = $enum_type; - } - - public function getEnumType() - { - return $this->enum_type; - } - - public function setPacked($packed) - { - $this->packed = $packed; - } - - public function getPacked() - { - return $this->packed; - } - - public function isPackable() - { - return $this->isRepeated() && self::isTypePackable($this->type); - } - - public function isMap() - { - return $this->getType() == GPBType::MESSAGE && - !is_null($this->getMessageType()->getOptions()) && - $this->getMessageType()->getOptions()->getMapEntry(); - } - - private static function isTypePackable($field_type) - { - return ($field_type !== GPBType::STRING && - $field_type !== GPBType::GROUP && - $field_type !== GPBType::MESSAGE && - $field_type !== GPBType::BYTES); - } - - public static function getFieldDescriptor( - $name, - $label, - $type, - $number, - $oneof_index, - $packed, - $type_name = null) - { - $field = new FieldDescriptor(); - $field->setName($name); - $camel_name = implode('', array_map('ucwords', explode('_', $name))); - $field->setGetter('get' . $camel_name); - $field->setSetter('set' . $camel_name); - $field->setType($type); - $field->setNumber($number); - $field->setLabel($label); - $field->setPacked($packed); - $field->setOneofIndex($oneof_index); - - // At this time, the message/enum type may have not been added to pool. - // So we use the type name as place holder and will replace it with the - // actual descriptor in cross building. - switch ($type) { - case GPBType::MESSAGE: - $field->setMessageType($type_name); - break; - case GPBType::ENUM: - $field->setEnumType($type_name); - break; - default: - break; - } - - return $field; - } - - public static function buildFromProto($proto) - { - $type_name = null; - switch ($proto->getType()) { - case GPBType::MESSAGE: - case GPBType::GROUP: - case GPBType::ENUM: - $type_name = $proto->getTypeName(); - break; - default: - break; - } - - $oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1; - $packed = false; - $options = $proto->getOptions(); - if ($options !== null) { - $packed = $options->getPacked(); - } - - return FieldDescriptor::getFieldDescriptor( - $proto->getName(), $proto->getLabel(), $proto->getType(), - $proto->getNumber(), $oneof_index, $packed, $type_name); - } -} |