Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 1 | <?php |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame^] | 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 |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 10 | * |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame^] | 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. |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 19 | * |
| 20 | * @package thrift |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame^] | 23 | |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 24 | /** |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame] | 25 | * Data types that can be sent via Thrift |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 26 | */ |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame] | 27 | class TType { |
| 28 | const STOP = 0; |
| 29 | const VOID = 1; |
| 30 | const BOOL = 2; |
| 31 | const BYTE = 3; |
| 32 | const I08 = 3; |
| 33 | const DOUBLE = 4; |
| 34 | const I16 = 6; |
| 35 | const I32 = 8; |
| 36 | const I64 = 10; |
| 37 | const STRING = 11; |
| 38 | const UTF7 = 11; |
| 39 | const STRUCT = 12; |
| 40 | const MAP = 13; |
| 41 | const SET = 14; |
| 42 | const LST = 15; // N.B. cannot use LIST keyword in PHP! |
| 43 | const UTF8 = 16; |
| 44 | const UTF16 = 17; |
Mark Slee | cfc0193 | 2006-09-01 22:18:16 +0000 | [diff] [blame] | 45 | } |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Message types for RPC |
| 49 | */ |
| 50 | class TMessageType { |
| 51 | const CALL = 1; |
| 52 | const REPLY = 2; |
Mark Slee | d395d57 | 2007-02-27 01:16:55 +0000 | [diff] [blame] | 53 | const EXCEPTION = 3; |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame] | 54 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 55 | |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 56 | /** |
| 57 | * NOTE(mcslee): This currently contains a ton of duplicated code from TBase |
| 58 | * because we need to save CPU cycles and this is not yet in an extension. |
| 59 | * Ideally we'd multiply-inherit TException from both Exception and Base, but |
| 60 | * that's not possible in PHP and there are no modules either, so for now we |
| 61 | * apologetically take a trip to HackTown. |
| 62 | * |
| 63 | * Can be called with standard Exception constructor (message, code) or with |
| 64 | * Thrift Base object constructor (spec, vals). |
| 65 | * |
| 66 | * @param mixed $p1 Message (string) or type-spec (array) |
| 67 | * @param mixed $p2 Code (integer) or values (array) |
| 68 | */ |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 69 | class TException extends Exception { |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 70 | function __construct($p1=null, $p2=0) { |
| 71 | if (is_array($p1) && is_array($p2)) { |
| 72 | $spec = $p1; |
| 73 | $vals = $p2; |
| 74 | foreach ($spec as $fid => $fspec) { |
| 75 | $var = $fspec['var']; |
| 76 | if (isset($vals[$var])) { |
| 77 | $this->$var = $vals[$var]; |
| 78 | } |
| 79 | } |
| 80 | } else { |
| 81 | parent::__construct($p1, $p2); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static $tmethod = array(TType::BOOL => 'Bool', |
| 86 | TType::BYTE => 'Byte', |
| 87 | TType::I16 => 'I16', |
| 88 | TType::I32 => 'I32', |
| 89 | TType::I64 => 'I64', |
| 90 | TType::DOUBLE => 'Double', |
| 91 | TType::STRING => 'String'); |
| 92 | |
| 93 | private function _readMap(&$var, $spec, $input) { |
| 94 | $xfer = 0; |
| 95 | $ktype = $spec['ktype']; |
| 96 | $vtype = $spec['vtype']; |
| 97 | $kread = $vread = null; |
| 98 | if (isset(TBase::$tmethod[$ktype])) { |
| 99 | $kread = 'read'.TBase::$tmethod[$ktype]; |
| 100 | } else { |
| 101 | $kspec = $spec['key']; |
| 102 | } |
| 103 | if (isset(TBase::$tmethod[$vtype])) { |
| 104 | $vread = 'read'.TBase::$tmethod[$vtype]; |
| 105 | } else { |
| 106 | $vspec = $spec['val']; |
| 107 | } |
| 108 | $var = array(); |
| 109 | $_ktype = $_vtype = $size = 0; |
| 110 | $xfer += $input->readMapBegin($_ktype, $_vtype, $size); |
| 111 | for ($i = 0; $i < $size; ++$i) { |
| 112 | $key = $val = null; |
| 113 | if ($kread !== null) { |
| 114 | $xfer += $input->$kread($key); |
| 115 | } else { |
| 116 | switch ($ktype) { |
| 117 | case TType::STRUCT: |
| 118 | $class = $kspec['class']; |
| 119 | $key = new $class(); |
| 120 | $xfer += $key->read($input); |
| 121 | break; |
| 122 | case TType::MAP: |
| 123 | $xfer += $this->_readMap($key, $kspec, $input); |
| 124 | break; |
| 125 | case TType::LST: |
| 126 | $xfer += $this->_readList($key, $kspec, $input, false); |
| 127 | break; |
| 128 | case TType::SET: |
| 129 | $xfer += $this->_readList($key, $kspec, $input, true); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | if ($vread !== null) { |
| 134 | $xfer += $input->$vread($val); |
| 135 | } else { |
| 136 | switch ($vtype) { |
| 137 | case TType::STRUCT: |
| 138 | $class = $vspec['class']; |
| 139 | $val = new $class(); |
| 140 | $xfer += $val->read($input); |
| 141 | break; |
| 142 | case TType::MAP: |
| 143 | $xfer += $this->_readMap($val, $vspec, $input); |
| 144 | break; |
| 145 | case TType::LST: |
| 146 | $xfer += $this->_readList($val, $vspec, $input, false); |
| 147 | break; |
| 148 | case TType::SET: |
| 149 | $xfer += $this->_readList($val, $vspec, $input, true); |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | $var[$key] = $val; |
| 154 | } |
| 155 | $xfer += $input->readMapEnd(); |
| 156 | return $xfer; |
| 157 | } |
| 158 | |
| 159 | private function _readList(&$var, $spec, $input, $set=false) { |
| 160 | $xfer = 0; |
| 161 | $etype = $spec['etype']; |
| 162 | $eread = $vread = null; |
| 163 | if (isset(TBase::$tmethod[$etype])) { |
| 164 | $eread = 'read'.TBase::$tmethod[$etype]; |
| 165 | } else { |
| 166 | $espec = $spec['elem']; |
| 167 | } |
| 168 | $var = array(); |
| 169 | $_etype = $size = 0; |
| 170 | if ($set) { |
| 171 | $xfer += $input->readSetBegin($_etype, $size); |
| 172 | } else { |
| 173 | $xfer += $input->readListBegin($_etype, $size); |
| 174 | } |
| 175 | for ($i = 0; $i < $size; ++$i) { |
| 176 | $elem = null; |
| 177 | if ($eread !== null) { |
| 178 | $xfer += $input->$eread($elem); |
| 179 | } else { |
| 180 | $espec = $spec['elem']; |
| 181 | switch ($etype) { |
| 182 | case TType::STRUCT: |
| 183 | $class = $espec['class']; |
| 184 | $elem = new $class(); |
| 185 | $xfer += $elem->read($input); |
| 186 | break; |
| 187 | case TType::MAP: |
David Reiss | 465ccc0 | 2007-11-13 21:41:29 +0000 | [diff] [blame] | 188 | $xfer += $this->_readMap($elem, $espec, $input); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 189 | break; |
| 190 | case TType::LST: |
David Reiss | 465ccc0 | 2007-11-13 21:41:29 +0000 | [diff] [blame] | 191 | $xfer += $this->_readList($elem, $espec, $input, false); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 192 | break; |
| 193 | case TType::SET: |
David Reiss | 465ccc0 | 2007-11-13 21:41:29 +0000 | [diff] [blame] | 194 | $xfer += $this->_readList($elem, $espec, $input, true); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | } |
| 198 | if ($set) { |
| 199 | $var[$elem] = true; |
| 200 | } else { |
| 201 | $var []= $elem; |
| 202 | } |
| 203 | } |
| 204 | if ($set) { |
| 205 | $xfer += $input->readSetEnd(); |
| 206 | } else { |
| 207 | $xfer += $input->readListEnd(); |
| 208 | } |
| 209 | return $xfer; |
| 210 | } |
| 211 | |
| 212 | protected function _read($class, $spec, $input) { |
| 213 | $xfer = 0; |
| 214 | $fname = null; |
| 215 | $ftype = 0; |
| 216 | $fid = 0; |
| 217 | $xfer += $input->readStructBegin($fname); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 218 | while (true) { |
| 219 | $xfer += $input->readFieldBegin($fname, $ftype, $fid); |
| 220 | if ($ftype == TType::STOP) { |
| 221 | break; |
| 222 | } |
| 223 | if (isset($spec[$fid])) { |
| 224 | $fspec = $spec[$fid]; |
| 225 | $var = $fspec['var']; |
| 226 | if ($ftype == $fspec['type']) { |
| 227 | $xfer = 0; |
Mark Slee | 645ecea | 2007-11-28 02:46:24 +0000 | [diff] [blame] | 228 | if (isset(TBase::$tmethod[$ftype])) { |
| 229 | $func = 'read'.TBase::$tmethod[$ftype]; |
| 230 | $xfer += $input->$func($this->$var); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 231 | } else { |
Mark Slee | 645ecea | 2007-11-28 02:46:24 +0000 | [diff] [blame] | 232 | switch ($ftype) { |
| 233 | case TType::STRUCT: |
| 234 | $class = $fspec['class']; |
| 235 | $this->$var = new $class(); |
| 236 | $xfer += $this->$var->read($input); |
| 237 | break; |
| 238 | case TType::MAP: |
| 239 | $xfer += $this->_readMap($this->$var, $fspec, $input); |
| 240 | break; |
| 241 | case TType::LST: |
| 242 | $xfer += $this->_readList($this->$var, $fspec, $input, false); |
| 243 | break; |
| 244 | case TType::SET: |
| 245 | $xfer += $this->_readList($this->$var, $fspec, $input, true); |
| 246 | break; |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } else { |
| 250 | $xfer += $input->skip($ftype); |
| 251 | } |
| 252 | } else { |
| 253 | $xfer += $input->skip($ftype); |
| 254 | } |
| 255 | $xfer += $input->readFieldEnd(); |
| 256 | } |
| 257 | $xfer += $input->readStructEnd(); |
| 258 | return $xfer; |
| 259 | } |
| 260 | |
| 261 | private function _writeMap($var, $spec, $output) { |
| 262 | $xfer = 0; |
| 263 | $ktype = $spec['ktype']; |
| 264 | $vtype = $spec['vtype']; |
| 265 | $kwrite = $vwrite = null; |
| 266 | if (isset(TBase::$tmethod[$ktype])) { |
| 267 | $kwrite = 'write'.TBase::$tmethod[$ktype]; |
| 268 | } else { |
| 269 | $kspec = $spec['key']; |
| 270 | } |
| 271 | if (isset(TBase::$tmethod[$vtype])) { |
| 272 | $vwrite = 'write'.TBase::$tmethod[$vtype]; |
| 273 | } else { |
| 274 | $vspec = $spec['val']; |
| 275 | } |
| 276 | $xfer += $output->writeMapBegin($ktype, $vtype, count($var)); |
| 277 | foreach ($var as $key => $val) { |
| 278 | if (isset($kwrite)) { |
| 279 | $xfer += $output->$kwrite($key); |
| 280 | } else { |
| 281 | switch ($ktype) { |
| 282 | case TType::STRUCT: |
| 283 | $xfer += $key->write($output); |
| 284 | break; |
| 285 | case TType::MAP: |
| 286 | $xfer += $this->_writeMap($key, $kspec, $output); |
| 287 | break; |
| 288 | case TType::LST: |
| 289 | $xfer += $this->_writeList($key, $kspec, $output, false); |
| 290 | break; |
| 291 | case TType::SET: |
| 292 | $xfer += $this->_writeList($key, $kspec, $output, true); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | if (isset($vwrite)) { |
| 297 | $xfer += $output->$vwrite($val); |
| 298 | } else { |
| 299 | switch ($vtype) { |
| 300 | case TType::STRUCT: |
| 301 | $xfer += $val->write($output); |
| 302 | break; |
| 303 | case TType::MAP: |
| 304 | $xfer += $this->_writeMap($val, $vspec, $output); |
| 305 | break; |
| 306 | case TType::LST: |
| 307 | $xfer += $this->_writeList($val, $vspec, $output, false); |
| 308 | break; |
| 309 | case TType::SET: |
| 310 | $xfer += $this->_writeList($val, $vspec, $output, true); |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | $xfer += $output->writeMapEnd(); |
| 316 | return $xfer; |
| 317 | } |
| 318 | |
| 319 | private function _writeList($var, $spec, $output, $set=false) { |
| 320 | $xfer = 0; |
| 321 | $etype = $spec['etype']; |
| 322 | $ewrite = null; |
| 323 | if (isset(TBase::$tmethod[$etype])) { |
| 324 | $ewrite = 'write'.TBase::$tmethod[$etype]; |
| 325 | } else { |
| 326 | $espec = $spec['elem']; |
| 327 | } |
| 328 | if ($set) { |
| 329 | $xfer += $output->writeSetBegin($etype, count($var)); |
| 330 | } else { |
| 331 | $xfer += $output->writeListBegin($etype, count($var)); |
| 332 | } |
| 333 | foreach ($var as $key => $val) { |
| 334 | $elem = $set ? $key : $val; |
| 335 | if (isset($ewrite)) { |
| 336 | $xfer += $output->$ewrite($elem); |
| 337 | } else { |
| 338 | switch ($etype) { |
| 339 | case TType::STRUCT: |
| 340 | $xfer += $elem->write($output); |
| 341 | break; |
| 342 | case TType::MAP: |
| 343 | $xfer += $this->_writeMap($elem, $espec, $output); |
| 344 | break; |
| 345 | case TType::LST: |
| 346 | $xfer += $this->_writeList($elem, $espec, $output, false); |
| 347 | break; |
| 348 | case TType::SET: |
| 349 | $xfer += $this->_writeList($elem, $espec, $output, true); |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | if ($set) { |
| 355 | $xfer += $output->writeSetEnd(); |
| 356 | } else { |
| 357 | $xfer += $output->writeListEnd(); |
| 358 | } |
| 359 | return $xfer; |
| 360 | } |
| 361 | |
| 362 | protected function _write($class, $spec, $output) { |
| 363 | $xfer = 0; |
| 364 | $xfer += $output->writeStructBegin($class); |
| 365 | foreach ($spec as $fid => $fspec) { |
| 366 | $var = $fspec['var']; |
| 367 | if ($this->$var !== null) { |
| 368 | $ftype = $fspec['type']; |
| 369 | $xfer += $output->writeFieldBegin($var, $ftype, $fid); |
| 370 | if (isset(TBase::$tmethod[$ftype])) { |
| 371 | $func = 'write'.TBase::$tmethod[$ftype]; |
Mark Slee | 2b78622 | 2007-11-13 05:35:34 +0000 | [diff] [blame] | 372 | $xfer += $output->$func($this->$var); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 373 | } else { |
| 374 | switch ($ftype) { |
| 375 | case TType::STRUCT: |
| 376 | $xfer += $this->$var->write($output); |
| 377 | break; |
| 378 | case TType::MAP: |
| 379 | $xfer += $this->_writeMap($this->$var, $fspec, $output); |
| 380 | break; |
| 381 | case TType::LST: |
| 382 | $xfer += $this->_writeList($this->$var, $fspec, $output, false); |
| 383 | break; |
| 384 | case TType::SET: |
| 385 | $xfer += $this->_writeList($this->$var, $fspec, $output, true); |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | $xfer += $output->writeFieldEnd(); |
| 390 | } |
| 391 | } |
| 392 | $xfer += $output->writeFieldStop(); |
| 393 | $xfer += $output->writeStructEnd(); |
| 394 | return $xfer; |
| 395 | } |
| 396 | |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Base class from which other Thrift structs extend. This is so that we can |
| 401 | * cut back on the size of the generated code which is turning out to have a |
| 402 | * nontrivial cost just to load thanks to the wondrously abysmal implementation |
| 403 | * of PHP. Note that code is intentionally duplicated in here to avoid making |
| 404 | * function calls for every field or member of a container.. |
| 405 | */ |
| 406 | abstract class TBase { |
| 407 | |
| 408 | static $tmethod = array(TType::BOOL => 'Bool', |
| 409 | TType::BYTE => 'Byte', |
| 410 | TType::I16 => 'I16', |
| 411 | TType::I32 => 'I32', |
| 412 | TType::I64 => 'I64', |
| 413 | TType::DOUBLE => 'Double', |
| 414 | TType::STRING => 'String'); |
| 415 | |
| 416 | abstract function read($input); |
| 417 | |
| 418 | abstract function write($output); |
| 419 | |
| 420 | public function __construct($spec=null, $vals=null) { |
| 421 | if (is_array($spec) && is_array($vals)) { |
| 422 | foreach ($spec as $fid => $fspec) { |
| 423 | $var = $fspec['var']; |
| 424 | if (isset($vals[$var])) { |
| 425 | $this->$var = $vals[$var]; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | private function _readMap(&$var, $spec, $input) { |
| 432 | $xfer = 0; |
| 433 | $ktype = $spec['ktype']; |
| 434 | $vtype = $spec['vtype']; |
| 435 | $kread = $vread = null; |
| 436 | if (isset(TBase::$tmethod[$ktype])) { |
| 437 | $kread = 'read'.TBase::$tmethod[$ktype]; |
| 438 | } else { |
| 439 | $kspec = $spec['key']; |
| 440 | } |
| 441 | if (isset(TBase::$tmethod[$vtype])) { |
| 442 | $vread = 'read'.TBase::$tmethod[$vtype]; |
| 443 | } else { |
| 444 | $vspec = $spec['val']; |
| 445 | } |
| 446 | $var = array(); |
| 447 | $_ktype = $_vtype = $size = 0; |
| 448 | $xfer += $input->readMapBegin($_ktype, $_vtype, $size); |
| 449 | for ($i = 0; $i < $size; ++$i) { |
| 450 | $key = $val = null; |
| 451 | if ($kread !== null) { |
| 452 | $xfer += $input->$kread($key); |
| 453 | } else { |
| 454 | switch ($ktype) { |
| 455 | case TType::STRUCT: |
| 456 | $class = $kspec['class']; |
| 457 | $key = new $class(); |
| 458 | $xfer += $key->read($input); |
| 459 | break; |
| 460 | case TType::MAP: |
David Reiss | bef54c0 | 2007-11-28 20:15:11 +0000 | [diff] [blame] | 461 | $xfer += $this->_readMap($key, $kspec, $input); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 462 | break; |
| 463 | case TType::LST: |
David Reiss | bef54c0 | 2007-11-28 20:15:11 +0000 | [diff] [blame] | 464 | $xfer += $this->_readList($key, $kspec, $input, false); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 465 | break; |
| 466 | case TType::SET: |
David Reiss | bef54c0 | 2007-11-28 20:15:11 +0000 | [diff] [blame] | 467 | $xfer += $this->_readList($key, $kspec, $input, true); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | } |
| 471 | if ($vread !== null) { |
| 472 | $xfer += $input->$vread($val); |
| 473 | } else { |
| 474 | switch ($vtype) { |
| 475 | case TType::STRUCT: |
| 476 | $class = $vspec['class']; |
| 477 | $val = new $class(); |
| 478 | $xfer += $val->read($input); |
| 479 | break; |
| 480 | case TType::MAP: |
| 481 | $xfer += $this->_readMap($val, $vspec, $input); |
| 482 | break; |
| 483 | case TType::LST: |
| 484 | $xfer += $this->_readList($val, $vspec, $input, false); |
| 485 | break; |
| 486 | case TType::SET: |
| 487 | $xfer += $this->_readList($val, $vspec, $input, true); |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | $var[$key] = $val; |
| 492 | } |
| 493 | $xfer += $input->readMapEnd(); |
| 494 | return $xfer; |
| 495 | } |
| 496 | |
| 497 | private function _readList(&$var, $spec, $input, $set=false) { |
| 498 | $xfer = 0; |
| 499 | $etype = $spec['etype']; |
| 500 | $eread = $vread = null; |
| 501 | if (isset(TBase::$tmethod[$etype])) { |
| 502 | $eread = 'read'.TBase::$tmethod[$etype]; |
| 503 | } else { |
| 504 | $espec = $spec['elem']; |
| 505 | } |
| 506 | $var = array(); |
| 507 | $_etype = $size = 0; |
| 508 | if ($set) { |
| 509 | $xfer += $input->readSetBegin($_etype, $size); |
| 510 | } else { |
| 511 | $xfer += $input->readListBegin($_etype, $size); |
| 512 | } |
| 513 | for ($i = 0; $i < $size; ++$i) { |
| 514 | $elem = null; |
| 515 | if ($eread !== null) { |
| 516 | $xfer += $input->$eread($elem); |
| 517 | } else { |
| 518 | $espec = $spec['elem']; |
| 519 | switch ($etype) { |
| 520 | case TType::STRUCT: |
| 521 | $class = $espec['class']; |
| 522 | $elem = new $class(); |
| 523 | $xfer += $elem->read($input); |
| 524 | break; |
| 525 | case TType::MAP: |
David Reiss | bef54c0 | 2007-11-28 20:15:11 +0000 | [diff] [blame] | 526 | $xfer += $this->_readMap($elem, $espec, $input); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 527 | break; |
| 528 | case TType::LST: |
David Reiss | bef54c0 | 2007-11-28 20:15:11 +0000 | [diff] [blame] | 529 | $xfer += $this->_readList($elem, $espec, $input, false); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 530 | break; |
| 531 | case TType::SET: |
David Reiss | bef54c0 | 2007-11-28 20:15:11 +0000 | [diff] [blame] | 532 | $xfer += $this->_readList($elem, $espec, $input, true); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | } |
| 536 | if ($set) { |
| 537 | $var[$elem] = true; |
| 538 | } else { |
| 539 | $var []= $elem; |
| 540 | } |
| 541 | } |
| 542 | if ($set) { |
| 543 | $xfer += $input->readSetEnd(); |
| 544 | } else { |
| 545 | $xfer += $input->readListEnd(); |
| 546 | } |
| 547 | return $xfer; |
| 548 | } |
| 549 | |
| 550 | protected function _read($class, $spec, $input) { |
| 551 | $xfer = 0; |
| 552 | $fname = null; |
| 553 | $ftype = 0; |
| 554 | $fid = 0; |
| 555 | $xfer += $input->readStructBegin($fname); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 556 | while (true) { |
| 557 | $xfer += $input->readFieldBegin($fname, $ftype, $fid); |
| 558 | if ($ftype == TType::STOP) { |
| 559 | break; |
| 560 | } |
| 561 | if (isset($spec[$fid])) { |
| 562 | $fspec = $spec[$fid]; |
| 563 | $var = $fspec['var']; |
| 564 | if ($ftype == $fspec['type']) { |
| 565 | $xfer = 0; |
Mark Slee | 645ecea | 2007-11-28 02:46:24 +0000 | [diff] [blame] | 566 | if (isset(TBase::$tmethod[$ftype])) { |
| 567 | $func = 'read'.TBase::$tmethod[$ftype]; |
| 568 | $xfer += $input->$func($this->$var); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 569 | } else { |
Mark Slee | 645ecea | 2007-11-28 02:46:24 +0000 | [diff] [blame] | 570 | switch ($ftype) { |
| 571 | case TType::STRUCT: |
| 572 | $class = $fspec['class']; |
| 573 | $this->$var = new $class(); |
| 574 | $xfer += $this->$var->read($input); |
| 575 | break; |
| 576 | case TType::MAP: |
| 577 | $xfer += $this->_readMap($this->$var, $fspec, $input); |
| 578 | break; |
| 579 | case TType::LST: |
| 580 | $xfer += $this->_readList($this->$var, $fspec, $input, false); |
| 581 | break; |
| 582 | case TType::SET: |
| 583 | $xfer += $this->_readList($this->$var, $fspec, $input, true); |
| 584 | break; |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | } else { |
| 588 | $xfer += $input->skip($ftype); |
| 589 | } |
| 590 | } else { |
| 591 | $xfer += $input->skip($ftype); |
| 592 | } |
| 593 | $xfer += $input->readFieldEnd(); |
| 594 | } |
| 595 | $xfer += $input->readStructEnd(); |
| 596 | return $xfer; |
| 597 | } |
| 598 | |
| 599 | private function _writeMap($var, $spec, $output) { |
| 600 | $xfer = 0; |
| 601 | $ktype = $spec['ktype']; |
| 602 | $vtype = $spec['vtype']; |
| 603 | $kwrite = $vwrite = null; |
| 604 | if (isset(TBase::$tmethod[$ktype])) { |
| 605 | $kwrite = 'write'.TBase::$tmethod[$ktype]; |
| 606 | } else { |
| 607 | $kspec = $spec['key']; |
| 608 | } |
| 609 | if (isset(TBase::$tmethod[$vtype])) { |
| 610 | $vwrite = 'write'.TBase::$tmethod[$vtype]; |
| 611 | } else { |
| 612 | $vspec = $spec['val']; |
| 613 | } |
| 614 | $xfer += $output->writeMapBegin($ktype, $vtype, count($var)); |
| 615 | foreach ($var as $key => $val) { |
| 616 | if (isset($kwrite)) { |
| 617 | $xfer += $output->$kwrite($key); |
| 618 | } else { |
| 619 | switch ($ktype) { |
| 620 | case TType::STRUCT: |
| 621 | $xfer += $key->write($output); |
| 622 | break; |
| 623 | case TType::MAP: |
| 624 | $xfer += $this->_writeMap($key, $kspec, $output); |
| 625 | break; |
| 626 | case TType::LST: |
| 627 | $xfer += $this->_writeList($key, $kspec, $output, false); |
| 628 | break; |
| 629 | case TType::SET: |
| 630 | $xfer += $this->_writeList($key, $kspec, $output, true); |
| 631 | break; |
| 632 | } |
| 633 | } |
| 634 | if (isset($vwrite)) { |
| 635 | $xfer += $output->$vwrite($val); |
| 636 | } else { |
| 637 | switch ($vtype) { |
| 638 | case TType::STRUCT: |
| 639 | $xfer += $val->write($output); |
| 640 | break; |
| 641 | case TType::MAP: |
| 642 | $xfer += $this->_writeMap($val, $vspec, $output); |
| 643 | break; |
| 644 | case TType::LST: |
| 645 | $xfer += $this->_writeList($val, $vspec, $output, false); |
| 646 | break; |
| 647 | case TType::SET: |
| 648 | $xfer += $this->_writeList($val, $vspec, $output, true); |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | $xfer += $output->writeMapEnd(); |
| 654 | return $xfer; |
| 655 | } |
| 656 | |
| 657 | private function _writeList($var, $spec, $output, $set=false) { |
| 658 | $xfer = 0; |
| 659 | $etype = $spec['etype']; |
| 660 | $ewrite = null; |
| 661 | if (isset(TBase::$tmethod[$etype])) { |
| 662 | $ewrite = 'write'.TBase::$tmethod[$etype]; |
| 663 | } else { |
| 664 | $espec = $spec['elem']; |
| 665 | } |
| 666 | if ($set) { |
| 667 | $xfer += $output->writeSetBegin($etype, count($var)); |
| 668 | } else { |
| 669 | $xfer += $output->writeListBegin($etype, count($var)); |
| 670 | } |
| 671 | foreach ($var as $key => $val) { |
| 672 | $elem = $set ? $key : $val; |
| 673 | if (isset($ewrite)) { |
| 674 | $xfer += $output->$ewrite($elem); |
| 675 | } else { |
| 676 | switch ($etype) { |
| 677 | case TType::STRUCT: |
| 678 | $xfer += $elem->write($output); |
| 679 | break; |
| 680 | case TType::MAP: |
| 681 | $xfer += $this->_writeMap($elem, $espec, $output); |
| 682 | break; |
| 683 | case TType::LST: |
| 684 | $xfer += $this->_writeList($elem, $espec, $output, false); |
| 685 | break; |
| 686 | case TType::SET: |
| 687 | $xfer += $this->_writeList($elem, $espec, $output, true); |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | if ($set) { |
| 693 | $xfer += $output->writeSetEnd(); |
| 694 | } else { |
| 695 | $xfer += $output->writeListEnd(); |
| 696 | } |
| 697 | return $xfer; |
| 698 | } |
| 699 | |
| 700 | protected function _write($class, $spec, $output) { |
| 701 | $xfer = 0; |
| 702 | $xfer += $output->writeStructBegin($class); |
| 703 | foreach ($spec as $fid => $fspec) { |
| 704 | $var = $fspec['var']; |
| 705 | if ($this->$var !== null) { |
| 706 | $ftype = $fspec['type']; |
| 707 | $xfer += $output->writeFieldBegin($var, $ftype, $fid); |
| 708 | if (isset(TBase::$tmethod[$ftype])) { |
| 709 | $func = 'write'.TBase::$tmethod[$ftype]; |
Mark Slee | 2b78622 | 2007-11-13 05:35:34 +0000 | [diff] [blame] | 710 | $xfer += $output->$func($this->$var); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 711 | } else { |
| 712 | switch ($ftype) { |
| 713 | case TType::STRUCT: |
| 714 | $xfer += $this->$var->write($output); |
| 715 | break; |
| 716 | case TType::MAP: |
| 717 | $xfer += $this->_writeMap($this->$var, $fspec, $output); |
| 718 | break; |
| 719 | case TType::LST: |
| 720 | $xfer += $this->_writeList($this->$var, $fspec, $output, false); |
| 721 | break; |
| 722 | case TType::SET: |
| 723 | $xfer += $this->_writeList($this->$var, $fspec, $output, true); |
| 724 | break; |
| 725 | } |
| 726 | } |
| 727 | $xfer += $output->writeFieldEnd(); |
| 728 | } |
| 729 | } |
| 730 | $xfer += $output->writeFieldStop(); |
| 731 | $xfer += $output->writeStructEnd(); |
| 732 | return $xfer; |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
| 736 | class TApplicationException extends TException { |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 737 | static $_TSPEC = |
| 738 | array(1 => array('var' => 'message', |
| 739 | 'type' => TType::STRING), |
| 740 | 2 => array('var' => 'code', |
| 741 | 'type' => TType::I32)); |
| 742 | |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 743 | const UNKNOWN = 0; |
| 744 | const UNKNOWN_METHOD = 1; |
| 745 | const INVALID_MESSAGE_TYPE = 2; |
| 746 | const WRONG_METHOD_NAME = 3; |
| 747 | const BAD_SEQUENCE_ID = 4; |
| 748 | const MISSING_RESULT = 5; |
| 749 | |
| 750 | function __construct($message=null, $code=0) { |
| 751 | parent::__construct($message, $code); |
| 752 | } |
| 753 | |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 754 | public function read($output) { |
| 755 | return $this->_read('TApplicationException', self::$_TSPEC, $output); |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | public function write($output) { |
| 759 | $xfer = 0; |
| 760 | $xfer += $output->writeStructBegin('TApplicationException'); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 761 | if ($message = $this->getMessage()) { |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 762 | $xfer += $output->writeFieldBegin('message', TType::STRING, 1); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 763 | $xfer += $output->writeString($message); |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 764 | $xfer += $output->writeFieldEnd(); |
| 765 | } |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 766 | if ($code = $this->getCode()) { |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 767 | $xfer += $output->writeFieldBegin('type', TType::I32, 2); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 768 | $xfer += $output->writeI32($code); |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 769 | $xfer += $output->writeFieldEnd(); |
| 770 | } |
| 771 | $xfer += $output->writeFieldStop(); |
| 772 | $xfer += $output->writeStructEnd(); |
| 773 | return $xfer; |
| 774 | } |
| 775 | } |
| 776 | |
Mark Slee | 1ecb1b0 | 2007-02-22 01:01:10 +0000 | [diff] [blame] | 777 | /** |
| 778 | * Set global THRIFT ROOT automatically via inclusion here |
| 779 | */ |
| 780 | if (!isset($GLOBALS['THRIFT_ROOT'])) { |
| 781 | $GLOBALS['THRIFT_ROOT'] = dirname(__FILE__); |
| 782 | } |
| 783 | include_once $GLOBALS['THRIFT_ROOT'].'/protocol/TProtocol.php'; |
| 784 | include_once $GLOBALS['THRIFT_ROOT'].'/transport/TTransport.php'; |
Mark Slee | dac7856 | 2007-02-21 07:35:03 +0000 | [diff] [blame] | 785 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 786 | ?> |