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