From b9b34e9b1167d89c4df8f0abffe31262aebe7a39 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 14 Jun 2017 15:57:11 -0700 Subject: Follows proper autoloading standards (#3123) * Follows proper autoloading standards - Splits PHP classes in descriptor.php into separate files - Splits MapFieldIter and RepeatedFieldIter into separate files - Moves descriptor.php to Internal/functions.php - Moves all namespaced functions into Iternal/functions.php * fixes Makefile.am for added php files * [PHP] moves all functions to GPBUtil * removes description.php from the makefile --- php/src/Google/Protobuf/Internal/Descriptor.php | 167 ++++++ .../Google/Protobuf/Internal/EnumDescriptor.php | 56 ++ .../Protobuf/Internal/EnumValueDescriptor.php | 37 ++ .../Google/Protobuf/Internal/FieldDescriptor.php | 236 ++++++++ .../Google/Protobuf/Internal/FileDescriptor.php | 90 ++++ php/src/Google/Protobuf/Internal/GPBUtil.php | 100 +++- php/src/Google/Protobuf/Internal/InputStream.php | 25 +- php/src/Google/Protobuf/Internal/MapField.php | 181 ++----- php/src/Google/Protobuf/Internal/MapFieldIter.php | 113 ++++ .../Google/Protobuf/Internal/OneofDescriptor.php | 67 +++ php/src/Google/Protobuf/Internal/RepeatedField.php | 80 --- .../Google/Protobuf/Internal/RepeatedFieldIter.php | 118 ++++ php/src/Google/Protobuf/descriptor.php | 600 --------------------- 13 files changed, 1038 insertions(+), 832 deletions(-) create mode 100644 php/src/Google/Protobuf/Internal/Descriptor.php create mode 100644 php/src/Google/Protobuf/Internal/EnumDescriptor.php create mode 100644 php/src/Google/Protobuf/Internal/EnumValueDescriptor.php create mode 100644 php/src/Google/Protobuf/Internal/FieldDescriptor.php create mode 100644 php/src/Google/Protobuf/Internal/FileDescriptor.php create mode 100644 php/src/Google/Protobuf/Internal/MapFieldIter.php create mode 100644 php/src/Google/Protobuf/Internal/OneofDescriptor.php create mode 100644 php/src/Google/Protobuf/Internal/RepeatedFieldIter.php delete mode 100644 php/src/Google/Protobuf/descriptor.php (limited to 'php/src/Google/Protobuf') 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 @@ +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 @@ +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 @@ +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 @@ +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 @@ -37,131 +37,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 @@ +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 @@ +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 @@ -40,86 +40,6 @@ namespace Google\Protobuf\Internal; 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 @@ +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 @@ -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); - } -} -- cgit v1.2.3