PageRenderTime 24ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/protobuf/php/src/Google/Protobuf/Internal/DescriptorPool.php

https://github.com/chromium/chromium
PHP | 193 lines | 141 code | 21 blank | 31 comment | 14 complexity | 5ce114daa89b2c9f719f99a0dd6714a5 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. <?php
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. namespace Google\Protobuf\Internal;
  32. use Google\Protobuf\Internal\Descriptor;
  33. use Google\Protobuf\Internal\FileDescriptor;
  34. use Google\Protobuf\Internal\FileDescriptorSet;
  35. use Google\Protobuf\Internal\MessageBuilderContext;
  36. use Google\Protobuf\Internal\EnumBuilderContext;
  37. class DescriptorPool
  38. {
  39. private static $pool;
  40. // Map from message names to sub-maps, which are maps from field numbers to
  41. // field descriptors.
  42. private $class_to_desc = [];
  43. private $class_to_enum_desc = [];
  44. private $proto_to_class = [];
  45. public static function getGeneratedPool()
  46. {
  47. if (!isset(self::$pool)) {
  48. self::$pool = new DescriptorPool();
  49. }
  50. return self::$pool;
  51. }
  52. public function internalAddGeneratedFile($data, $use_nested = false)
  53. {
  54. $files = new FileDescriptorSet();
  55. $files->mergeFromString($data);
  56. foreach($files->getFile() as $file_proto) {
  57. $file = FileDescriptor::buildFromProto($file_proto);
  58. foreach ($file->getMessageType() as $desc) {
  59. $this->addDescriptor($desc);
  60. }
  61. unset($desc);
  62. foreach ($file->getEnumType() as $desc) {
  63. $this->addEnumDescriptor($desc);
  64. }
  65. unset($desc);
  66. foreach ($file->getMessageType() as $desc) {
  67. $this->crossLink($desc);
  68. }
  69. unset($desc);
  70. }
  71. }
  72. public function addMessage($name, $klass)
  73. {
  74. return new MessageBuilderContext($name, $klass, $this);
  75. }
  76. public function addEnum($name, $klass)
  77. {
  78. return new EnumBuilderContext($name, $klass, $this);
  79. }
  80. public function addDescriptor($descriptor)
  81. {
  82. $this->proto_to_class[$descriptor->getFullName()] =
  83. $descriptor->getClass();
  84. $this->class_to_desc[$descriptor->getClass()] = $descriptor;
  85. $this->class_to_desc[$descriptor->getLegacyClass()] = $descriptor;
  86. foreach ($descriptor->getNestedType() as $nested_type) {
  87. $this->addDescriptor($nested_type);
  88. }
  89. foreach ($descriptor->getEnumType() as $enum_type) {
  90. $this->addEnumDescriptor($enum_type);
  91. }
  92. }
  93. public function addEnumDescriptor($descriptor)
  94. {
  95. $this->proto_to_class[$descriptor->getFullName()] =
  96. $descriptor->getClass();
  97. $this->class_to_enum_desc[$descriptor->getClass()] = $descriptor;
  98. $this->class_to_enum_desc[$descriptor->getLegacyClass()] = $descriptor;
  99. }
  100. public function getDescriptorByClassName($klass)
  101. {
  102. if (isset($this->class_to_desc[$klass])) {
  103. return $this->class_to_desc[$klass];
  104. } else {
  105. return null;
  106. }
  107. }
  108. public function getEnumDescriptorByClassName($klass)
  109. {
  110. if (isset($this->class_to_enum_desc[$klass])) {
  111. return $this->class_to_enum_desc[$klass];
  112. } else {
  113. return null;
  114. }
  115. }
  116. public function getDescriptorByProtoName($proto)
  117. {
  118. if (isset($this->proto_to_class[$proto])) {
  119. $klass = $this->proto_to_class[$proto];
  120. return $this->class_to_desc[$klass];
  121. } else {
  122. return null;
  123. }
  124. }
  125. public function getEnumDescriptorByProtoName($proto)
  126. {
  127. $klass = $this->proto_to_class[$proto];
  128. return $this->class_to_enum_desc[$klass];
  129. }
  130. private function crossLink(Descriptor $desc)
  131. {
  132. foreach ($desc->getField() as $field) {
  133. switch ($field->getType()) {
  134. case GPBType::MESSAGE:
  135. $proto = $field->getMessageType();
  136. if ($proto[0] == '.') {
  137. $proto = substr($proto, 1);
  138. }
  139. $subdesc = $this->getDescriptorByProtoName($proto);
  140. if (is_null($subdesc)) {
  141. trigger_error(
  142. 'proto not added: ' . $proto
  143. . " for " . $desc->getFullName(), E_ERROR);
  144. }
  145. $field->setMessageType($subdesc);
  146. break;
  147. case GPBType::ENUM:
  148. $proto = $field->getEnumType();
  149. if ($proto[0] == '.') {
  150. $proto = substr($proto, 1);
  151. }
  152. $field->setEnumType(
  153. $this->getEnumDescriptorByProtoName($proto));
  154. break;
  155. default:
  156. break;
  157. }
  158. }
  159. unset($field);
  160. foreach ($desc->getNestedType() as $nested_type) {
  161. $this->crossLink($nested_type);
  162. }
  163. unset($nested_type);
  164. }
  165. public function finish()
  166. {
  167. foreach ($this->class_to_desc as $klass => $desc) {
  168. $this->crossLink($desc);
  169. }
  170. unset($desc);
  171. }
  172. }