|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * |
| 20 | + * @package thrift |
| 21 | + */ |
| 22 | + |
| 23 | +namespace Thrift\Base; |
| 24 | + |
| 25 | +use Thrift\Type\TType; |
| 26 | + |
| 27 | +/** |
| 28 | + * Base class from which other Thrift structs extend. This is so that we can |
| 29 | + * cut back on the size of the generated code which is turning out to have a |
| 30 | + * nontrivial cost just to load thanks to the wondrously abysmal implementation |
| 31 | + * of PHP. Note that code is intentionally duplicated in here to avoid making |
| 32 | + * function calls for every field or member of a container.. |
| 33 | + */ |
| 34 | +abstract class TBase { |
| 35 | + |
| 36 | + static $tmethod = array(TType::BOOL => 'Bool', |
| 37 | + TType::BYTE => 'Byte', |
| 38 | + TType::I16 => 'I16', |
| 39 | + TType::I32 => 'I32', |
| 40 | + TType::I64 => 'I64', |
| 41 | + TType::DOUBLE => 'Double', |
| 42 | + TType::STRING => 'String'); |
| 43 | + |
| 44 | + abstract function read($input); |
| 45 | + |
| 46 | + abstract function write($output); |
| 47 | + |
| 48 | + public function __construct($spec=null, $vals=null) { |
| 49 | + if (is_array($spec) && is_array($vals)) { |
| 50 | + foreach ($spec as $fid => $fspec) { |
| 51 | + $var = $fspec['var']; |
| 52 | + if (isset($vals[$var])) { |
| 53 | + $this->$var = $vals[$var]; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public function __wakeup() |
| 60 | + { |
| 61 | + $this->__construct(get_object_vars($this)); |
| 62 | + } |
| 63 | + |
| 64 | + private function _readMap(&$var, $spec, $input) { |
| 65 | + $xfer = 0; |
| 66 | + $ktype = $spec['ktype']; |
| 67 | + $vtype = $spec['vtype']; |
| 68 | + $kread = $vread = null; |
| 69 | + if (isset(TBase::$tmethod[$ktype])) { |
| 70 | + $kread = 'read'.TBase::$tmethod[$ktype]; |
| 71 | + } else { |
| 72 | + $kspec = $spec['key']; |
| 73 | + } |
| 74 | + if (isset(TBase::$tmethod[$vtype])) { |
| 75 | + $vread = 'read'.TBase::$tmethod[$vtype]; |
| 76 | + } else { |
| 77 | + $vspec = $spec['val']; |
| 78 | + } |
| 79 | + $var = array(); |
| 80 | + $_ktype = $_vtype = $size = 0; |
| 81 | + $xfer += $input->readMapBegin($_ktype, $_vtype, $size); |
| 82 | + for ($i = 0; $i < $size; ++$i) { |
| 83 | + $key = $val = null; |
| 84 | + if ($kread !== null) { |
| 85 | + $xfer += $input->$kread($key); |
| 86 | + } else { |
| 87 | + switch ($ktype) { |
| 88 | + case TType::STRUCT: |
| 89 | + $class = $kspec['class']; |
| 90 | + $key = new $class(); |
| 91 | + $xfer += $key->read($input); |
| 92 | + break; |
| 93 | + case TType::MAP: |
| 94 | + $xfer += $this->_readMap($key, $kspec, $input); |
| 95 | + break; |
| 96 | + case TType::LST: |
| 97 | + $xfer += $this->_readList($key, $kspec, $input, false); |
| 98 | + break; |
| 99 | + case TType::SET: |
| 100 | + $xfer += $this->_readList($key, $kspec, $input, true); |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + if ($vread !== null) { |
| 105 | + $xfer += $input->$vread($val); |
| 106 | + } else { |
| 107 | + switch ($vtype) { |
| 108 | + case TType::STRUCT: |
| 109 | + $class = $vspec['class']; |
| 110 | + $val = new $class(); |
| 111 | + $xfer += $val->read($input); |
| 112 | + break; |
| 113 | + case TType::MAP: |
| 114 | + $xfer += $this->_readMap($val, $vspec, $input); |
| 115 | + break; |
| 116 | + case TType::LST: |
| 117 | + $xfer += $this->_readList($val, $vspec, $input, false); |
| 118 | + break; |
| 119 | + case TType::SET: |
| 120 | + $xfer += $this->_readList($val, $vspec, $input, true); |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + $var[$key] = $val; |
| 125 | + } |
| 126 | + $xfer += $input->readMapEnd(); |
| 127 | + return $xfer; |
| 128 | + } |
| 129 | + |
| 130 | + private function _readList(&$var, $spec, $input, $set=false) { |
| 131 | + $xfer = 0; |
| 132 | + $etype = $spec['etype']; |
| 133 | + $eread = $vread = null; |
| 134 | + if (isset(TBase::$tmethod[$etype])) { |
| 135 | + $eread = 'read'.TBase::$tmethod[$etype]; |
| 136 | + } else { |
| 137 | + $espec = $spec['elem']; |
| 138 | + } |
| 139 | + $var = array(); |
| 140 | + $_etype = $size = 0; |
| 141 | + if ($set) { |
| 142 | + $xfer += $input->readSetBegin($_etype, $size); |
| 143 | + } else { |
| 144 | + $xfer += $input->readListBegin($_etype, $size); |
| 145 | + } |
| 146 | + for ($i = 0; $i < $size; ++$i) { |
| 147 | + $elem = null; |
| 148 | + if ($eread !== null) { |
| 149 | + $xfer += $input->$eread($elem); |
| 150 | + } else { |
| 151 | + $espec = $spec['elem']; |
| 152 | + switch ($etype) { |
| 153 | + case TType::STRUCT: |
| 154 | + $class = $espec['class']; |
| 155 | + $elem = new $class(); |
| 156 | + $xfer += $elem->read($input); |
| 157 | + break; |
| 158 | + case TType::MAP: |
| 159 | + $xfer += $this->_readMap($elem, $espec, $input); |
| 160 | + break; |
| 161 | + case TType::LST: |
| 162 | + $xfer += $this->_readList($elem, $espec, $input, false); |
| 163 | + break; |
| 164 | + case TType::SET: |
| 165 | + $xfer += $this->_readList($elem, $espec, $input, true); |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | + if ($set) { |
| 170 | + $var[$elem] = true; |
| 171 | + } else { |
| 172 | + $var []= $elem; |
| 173 | + } |
| 174 | + } |
| 175 | + if ($set) { |
| 176 | + $xfer += $input->readSetEnd(); |
| 177 | + } else { |
| 178 | + $xfer += $input->readListEnd(); |
| 179 | + } |
| 180 | + return $xfer; |
| 181 | + } |
| 182 | + |
| 183 | + protected function _read($class, $spec, $input) { |
| 184 | + $xfer = 0; |
| 185 | + $fname = null; |
| 186 | + $ftype = 0; |
| 187 | + $fid = 0; |
| 188 | + $xfer += $input->readStructBegin($fname); |
| 189 | + while (true) { |
| 190 | + $xfer += $input->readFieldBegin($fname, $ftype, $fid); |
| 191 | + if ($ftype == TType::STOP) { |
| 192 | + break; |
| 193 | + } |
| 194 | + if (isset($spec[$fid])) { |
| 195 | + $fspec = $spec[$fid]; |
| 196 | + $var = $fspec['var']; |
| 197 | + if ($ftype == $fspec['type']) { |
| 198 | + $xfer = 0; |
| 199 | + if (isset(TBase::$tmethod[$ftype])) { |
| 200 | + $func = 'read'.TBase::$tmethod[$ftype]; |
| 201 | + $xfer += $input->$func($this->$var); |
| 202 | + } else { |
| 203 | + switch ($ftype) { |
| 204 | + case TType::STRUCT: |
| 205 | + $class = $fspec['class']; |
| 206 | + $this->$var = new $class(); |
| 207 | + $xfer += $this->$var->read($input); |
| 208 | + break; |
| 209 | + case TType::MAP: |
| 210 | + $xfer += $this->_readMap($this->$var, $fspec, $input); |
| 211 | + break; |
| 212 | + case TType::LST: |
| 213 | + $xfer += $this->_readList($this->$var, $fspec, $input, false); |
| 214 | + break; |
| 215 | + case TType::SET: |
| 216 | + $xfer += $this->_readList($this->$var, $fspec, $input, true); |
| 217 | + break; |
| 218 | + } |
| 219 | + } |
| 220 | + } else { |
| 221 | + $xfer += $input->skip($ftype); |
| 222 | + } |
| 223 | + } else { |
| 224 | + $xfer += $input->skip($ftype); |
| 225 | + } |
| 226 | + $xfer += $input->readFieldEnd(); |
| 227 | + } |
| 228 | + $xfer += $input->readStructEnd(); |
| 229 | + return $xfer; |
| 230 | + } |
| 231 | + |
| 232 | + private function _writeMap($var, $spec, $output) { |
| 233 | + $xfer = 0; |
| 234 | + $ktype = $spec['ktype']; |
| 235 | + $vtype = $spec['vtype']; |
| 236 | + $kwrite = $vwrite = null; |
| 237 | + if (isset(TBase::$tmethod[$ktype])) { |
| 238 | + $kwrite = 'write'.TBase::$tmethod[$ktype]; |
| 239 | + } else { |
| 240 | + $kspec = $spec['key']; |
| 241 | + } |
| 242 | + if (isset(TBase::$tmethod[$vtype])) { |
| 243 | + $vwrite = 'write'.TBase::$tmethod[$vtype]; |
| 244 | + } else { |
| 245 | + $vspec = $spec['val']; |
| 246 | + } |
| 247 | + $xfer += $output->writeMapBegin($ktype, $vtype, count($var)); |
| 248 | + foreach ($var as $key => $val) { |
| 249 | + if (isset($kwrite)) { |
| 250 | + $xfer += $output->$kwrite($key); |
| 251 | + } else { |
| 252 | + switch ($ktype) { |
| 253 | + case TType::STRUCT: |
| 254 | + $xfer += $key->write($output); |
| 255 | + break; |
| 256 | + case TType::MAP: |
| 257 | + $xfer += $this->_writeMap($key, $kspec, $output); |
| 258 | + break; |
| 259 | + case TType::LST: |
| 260 | + $xfer += $this->_writeList($key, $kspec, $output, false); |
| 261 | + break; |
| 262 | + case TType::SET: |
| 263 | + $xfer += $this->_writeList($key, $kspec, $output, true); |
| 264 | + break; |
| 265 | + } |
| 266 | + } |
| 267 | + if (isset($vwrite)) { |
| 268 | + $xfer += $output->$vwrite($val); |
| 269 | + } else { |
| 270 | + switch ($vtype) { |
| 271 | + case TType::STRUCT: |
| 272 | + $xfer += $val->write($output); |
| 273 | + break; |
| 274 | + case TType::MAP: |
| 275 | + $xfer += $this->_writeMap($val, $vspec, $output); |
| 276 | + break; |
| 277 | + case TType::LST: |
| 278 | + $xfer += $this->_writeList($val, $vspec, $output, false); |
| 279 | + break; |
| 280 | + case TType::SET: |
| 281 | + $xfer += $this->_writeList($val, $vspec, $output, true); |
| 282 | + break; |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + $xfer += $output->writeMapEnd(); |
| 287 | + return $xfer; |
| 288 | + } |
| 289 | + |
| 290 | + private function _writeList($var, $spec, $output, $set=false) { |
| 291 | + $xfer = 0; |
| 292 | + $etype = $spec['etype']; |
| 293 | + $ewrite = null; |
| 294 | + if (isset(TBase::$tmethod[$etype])) { |
| 295 | + $ewrite = 'write'.TBase::$tmethod[$etype]; |
| 296 | + } else { |
| 297 | + $espec = $spec['elem']; |
| 298 | + } |
| 299 | + if ($set) { |
| 300 | + $xfer += $output->writeSetBegin($etype, count($var)); |
| 301 | + } else { |
| 302 | + $xfer += $output->writeListBegin($etype, count($var)); |
| 303 | + } |
| 304 | + foreach ($var as $key => $val) { |
| 305 | + $elem = $set ? $key : $val; |
| 306 | + if (isset($ewrite)) { |
| 307 | + $xfer += $output->$ewrite($elem); |
| 308 | + } else { |
| 309 | + switch ($etype) { |
| 310 | + case TType::STRUCT: |
| 311 | + $xfer += $elem->write($output); |
| 312 | + break; |
| 313 | + case TType::MAP: |
| 314 | + $xfer += $this->_writeMap($elem, $espec, $output); |
| 315 | + break; |
| 316 | + case TType::LST: |
| 317 | + $xfer += $this->_writeList($elem, $espec, $output, false); |
| 318 | + break; |
| 319 | + case TType::SET: |
| 320 | + $xfer += $this->_writeList($elem, $espec, $output, true); |
| 321 | + break; |
| 322 | + } |
| 323 | + } |
| 324 | + } |
| 325 | + if ($set) { |
| 326 | + $xfer += $output->writeSetEnd(); |
| 327 | + } else { |
| 328 | + $xfer += $output->writeListEnd(); |
| 329 | + } |
| 330 | + return $xfer; |
| 331 | + } |
| 332 | + |
| 333 | + protected function _write($class, $spec, $output) { |
| 334 | + $xfer = 0; |
| 335 | + $xfer += $output->writeStructBegin($class); |
| 336 | + foreach ($spec as $fid => $fspec) { |
| 337 | + $var = $fspec['var']; |
| 338 | + if ($this->$var !== null) { |
| 339 | + $ftype = $fspec['type']; |
| 340 | + $xfer += $output->writeFieldBegin($var, $ftype, $fid); |
| 341 | + if (isset(TBase::$tmethod[$ftype])) { |
| 342 | + $func = 'write'.TBase::$tmethod[$ftype]; |
| 343 | + $xfer += $output->$func($this->$var); |
| 344 | + } else { |
| 345 | + switch ($ftype) { |
| 346 | + case TType::STRUCT: |
| 347 | + $xfer += $this->$var->write($output); |
| 348 | + break; |
| 349 | + case TType::MAP: |
| 350 | + $xfer += $this->_writeMap($this->$var, $fspec, $output); |
| 351 | + break; |
| 352 | + case TType::LST: |
| 353 | + $xfer += $this->_writeList($this->$var, $fspec, $output, false); |
| 354 | + break; |
| 355 | + case TType::SET: |
| 356 | + $xfer += $this->_writeList($this->$var, $fspec, $output, true); |
| 357 | + break; |
| 358 | + } |
| 359 | + } |
| 360 | + $xfer += $output->writeFieldEnd(); |
| 361 | + } |
| 362 | + } |
| 363 | + $xfer += $output->writeFieldStop(); |
| 364 | + $xfer += $output->writeStructEnd(); |
| 365 | + return $xfer; |
| 366 | + } |
| 367 | +} |
0 commit comments