xref: /aosp_15_r20/external/protobuf/php/src/Google/Protobuf/Internal/DescriptorPool.php (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
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        $this->class_to_desc[$descriptor->getPreviouslyUnreservedClass()] = $descriptor;
100        foreach ($descriptor->getNestedType() as $nested_type) {
101            $this->addDescriptor($nested_type);
102        }
103        foreach ($descriptor->getEnumType() as $enum_type) {
104            $this->addEnumDescriptor($enum_type);
105        }
106    }
107
108    public function addEnumDescriptor($descriptor)
109    {
110        $this->proto_to_class[$descriptor->getFullName()] =
111            $descriptor->getClass();
112        $this->class_to_enum_desc[$descriptor->getClass()] = $descriptor;
113        $this->class_to_enum_desc[$descriptor->getLegacyClass()] = $descriptor;
114    }
115
116    public function getDescriptorByClassName($klass)
117    {
118        if (isset($this->class_to_desc[$klass])) {
119            return $this->class_to_desc[$klass];
120        } else {
121            return null;
122        }
123    }
124
125    public function getEnumDescriptorByClassName($klass)
126    {
127        if (isset($this->class_to_enum_desc[$klass])) {
128            return $this->class_to_enum_desc[$klass];
129        } else {
130            return null;
131        }
132    }
133
134    public function getDescriptorByProtoName($proto)
135    {
136        if (isset($this->proto_to_class[$proto])) {
137            $klass = $this->proto_to_class[$proto];
138            return $this->class_to_desc[$klass];
139        } else {
140          return null;
141        }
142    }
143
144    public function getEnumDescriptorByProtoName($proto)
145    {
146        $klass = $this->proto_to_class[$proto];
147        return $this->class_to_enum_desc[$klass];
148    }
149
150    private function crossLink(Descriptor $desc)
151    {
152        foreach ($desc->getField() as $field) {
153            switch ($field->getType()) {
154                case GPBType::MESSAGE:
155                    $proto = $field->getMessageType();
156                    if ($proto[0] == '.') {
157                      $proto = substr($proto, 1);
158                    }
159                    $subdesc = $this->getDescriptorByProtoName($proto);
160                    if (is_null($subdesc)) {
161                        trigger_error(
162                            'proto not added: ' . $proto
163                            . " for " . $desc->getFullName(), E_USER_ERROR);
164                    }
165                    $field->setMessageType($subdesc);
166                    break;
167                case GPBType::ENUM:
168                    $proto = $field->getEnumType();
169                    if ($proto[0] == '.') {
170                      $proto = substr($proto, 1);
171                    }
172                    $field->setEnumType(
173                        $this->getEnumDescriptorByProtoName($proto));
174                    break;
175                default:
176                    break;
177            }
178        }
179        unset($field);
180
181        foreach ($desc->getNestedType() as $nested_type) {
182            $this->crossLink($nested_type);
183        }
184        unset($nested_type);
185    }
186
187    public function finish()
188    {
189        foreach ($this->class_to_desc as $klass => $desc) {
190            $this->crossLink($desc);
191        }
192        unset($desc);
193    }
194}
195