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