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 33namespace Google\Protobuf\Internal; 34 35use Google\Protobuf\Internal\Descriptor; 36use Google\Protobuf\Internal\FileDescriptor; 37use Google\Protobuf\Internal\FileDescriptorSet; 38use Google\Protobuf\Internal\MessageBuilderContext; 39use Google\Protobuf\Internal\EnumBuilderContext; 40 41class 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, $use_nested = false) 59 { 60 $files = new FileDescriptorSet(); 61 $files->mergeFromString($data); 62 63 foreach($files->getFile() as $file_proto) { 64 $file = FileDescriptor::buildFromProto($file_proto); 65 66 foreach ($file->getMessageType() as $desc) { 67 $this->addDescriptor($desc); 68 } 69 unset($desc); 70 71 foreach ($file->getEnumType() as $desc) { 72 $this->addEnumDescriptor($desc); 73 } 74 unset($desc); 75 76 foreach ($file->getMessageType() as $desc) { 77 $this->crossLink($desc); 78 } 79 unset($desc); 80 } 81 } 82 83 public function addMessage($name, $klass) 84 { 85 return new MessageBuilderContext($name, $klass, $this); 86 } 87 88 public function addEnum($name, $klass) 89 { 90 return new EnumBuilderContext($name, $klass, $this); 91 } 92 93 public function addDescriptor($descriptor) 94 { 95 $this->proto_to_class[$descriptor->getFullName()] = 96 $descriptor->getClass(); 97 $this->class_to_desc[$descriptor->getClass()] = $descriptor; 98 $this->class_to_desc[$descriptor->getLegacyClass()] = $descriptor; 99 foreach ($descriptor->getNestedType() as $nested_type) { 100 $this->addDescriptor($nested_type); 101 } 102 foreach ($descriptor->getEnumType() as $enum_type) { 103 $this->addEnumDescriptor($enum_type); 104 } 105 } 106 107 public function addEnumDescriptor($descriptor) 108 { 109 $this->proto_to_class[$descriptor->getFullName()] = 110 $descriptor->getClass(); 111 $this->class_to_enum_desc[$descriptor->getClass()] = $descriptor; 112 $this->class_to_enum_desc[$descriptor->getLegacyClass()] = $descriptor; 113 } 114 115 public function getDescriptorByClassName($klass) 116 { 117 if (isset($this->class_to_desc[$klass])) { 118 return $this->class_to_desc[$klass]; 119 } else { 120 return null; 121 } 122 } 123 124 public function getEnumDescriptorByClassName($klass) 125 { 126 if (isset($this->class_to_enum_desc[$klass])) { 127 return $this->class_to_enum_desc[$klass]; 128 } else { 129 return null; 130 } 131 } 132 133 public function getDescriptorByProtoName($proto) 134 { 135 if (isset($this->proto_to_class[$proto])) { 136 $klass = $this->proto_to_class[$proto]; 137 return $this->class_to_desc[$klass]; 138 } else { 139 return null; 140 } 141 } 142 143 public function getEnumDescriptorByProtoName($proto) 144 { 145 $klass = $this->proto_to_class[$proto]; 146 return $this->class_to_enum_desc[$klass]; 147 } 148 149 private function crossLink(Descriptor $desc) 150 { 151 foreach ($desc->getField() as $field) { 152 switch ($field->getType()) { 153 case GPBType::MESSAGE: 154 $proto = $field->getMessageType(); 155 if ($proto[0] == '.') { 156 $proto = substr($proto, 1); 157 } 158 $subdesc = $this->getDescriptorByProtoName($proto); 159 if (is_null($subdesc)) { 160 trigger_error( 161 'proto not added: ' . $proto 162 . " for " . $desc->getFullName(), E_ERROR); 163 } 164 $field->setMessageType($subdesc); 165 break; 166 case GPBType::ENUM: 167 $proto = $field->getEnumType(); 168 if ($proto[0] == '.') { 169 $proto = substr($proto, 1); 170 } 171 $field->setEnumType( 172 $this->getEnumDescriptorByProtoName($proto)); 173 break; 174 default: 175 break; 176 } 177 } 178 unset($field); 179 180 foreach ($desc->getNestedType() as $nested_type) { 181 $this->crossLink($nested_type); 182 } 183 unset($nested_type); 184 } 185 186 public function finish() 187 { 188 foreach ($this->class_to_desc as $klass => $desc) { 189 $this->crossLink($desc); 190 } 191 unset($desc); 192 } 193} 194